Android: Navigation-Drawer on all activities

The easy way is that you should create fragments. If you are ready to for little hard thing then this is for you. It will let you have same navigation drawer in all activities. Create drawer_n_activity.xml <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent” > <FrameLayout android:id=”@+id/drawer_frame” android:layout_width=”match_parent” android:layout_height=”match_parent” /> <YourDrawer android:id=”@+id/drawer_drawer” android:layout_width=”match_parent” android:layout_height=”fill_parent” > </YourDrawer> </RelativeLayout> Your DrawerActivity.class … Read more

How to slide the ActionBar along with the NavigationDrawer

PLEASE NOTE: This answer was originally written when Android 4.4 (KitKat) was still pretty new. Since Android 5.0 and especially because of the introduction of the ToolBar this answer cannot be considered up-to-date anymore! But from a technical perspective and for those of you who want to learn about the inner workings of Android this … Read more

How to overcome this item padding in navigation drawer?

According to source code of NavigationView found here, it led me to NavigationMenuPresenter (found here) which says, every normal type in menu list inflates R.layout.design_navigation_item. So if you preview it (here) you will notice what preference it uses. <android.support.design.internal.NavigationMenuItemView xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”?attr/listPreferredItemHeightSmall” android:paddingLeft=”?attr/listPreferredItemPaddingLeft” android:paddingRight=”?attr/listPreferredItemPaddingRight” android:foreground=”?attr/selectableItemBackground” android:focusable=”true”/> So, the final step is to override the style … Read more

How to put list items at the bottom of list view in Navigation Drawer like Foursquare

You can put the ListView inside a RelativeLayout and assign android:layout_gravity=”start” to the RelativeLayout. And set android:layout_alignParentBottom=”true” property to the view you want to set to bottom of ListView. This may help you. <android.support.v4.widget.DrawerLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/drawer_layout” android:layout_width=”match_parent” android:layout_height=”match_parent” > <FrameLayout android:id=”@+id/content_frame”…../> <RelativeLayout android:id=”@+id/relative_layout” android:layout_width=”match_parent” android:layout_height=”match_parent” android:layout_gravity=”start” > <ListView android:id=”@+id/left_drawer” android:layout_width=”match_parent” android:layout_height=”match_parent” android:background=”#111″ android:choiceMode=”singleChoice” android:divider=”@android:color/transparent” android:dividerHeight=”0dp” … Read more

ClassCastException android.widget.FrameLayout$LayoutParams to android.support.v4.widget.DrawerLayout$LayoutParams

What solved this issue for me: In MainActivity, add a new field for the LinearLayout, and assign value to it in onCreate() (this part just like emaleavil suggested): private LinearLayout linearLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // … linearLayout = (LinearLayout) findViewById(R.id.linearLayout); } Then in selectItem(), when calling closeDrawer(), simply pass linearLayout as … Read more

Manage toolbar’s navigation and back button from fragment in android

Add a toolbar to your xml <android.support.v7.widget.Toolbar android:id=”@+id/toolbar” android:layout_width=”match_parent” android:layout_height=”?attr/actionBarSize” android:background=”?attr/colorPrimary” android:theme=”@style/ThemeOverlay.AppCompat.ActionBar” app:popupTheme=”@style/ThemeOverlay.AppCompat.Light”> <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Fragment title”/> </android.support.v7.widget.Toolbar> Then inside your onCreateView method in the Fragment: Toolbar toolbar = view.findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.drawable.ic_back_button); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getActivity().onBackPressed(); } });

Two Navigation Drawer on same Activity

You can use drawer layout <android.support.v4.widget.DrawerLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/drawer_layout” android:layout_width=”match_parent” android:layout_height=”match_parent”> <!– The main content view –> <FrameLayout android:id=”@+id/content_frame” android:layout_width=”match_parent” android:layout_height=”match_parent” /> <!– The navigation drawer –> <ListView android:id=”@+id/left_drawer” android:layout_width=”240dp” android:layout_height=”match_parent” android:layout_gravity=”start” android:choiceMode=”singleChoice” android:divider=”@android:color/transparent” android:dividerHeight=”0dp” android:background=”#111″/> <ListView android:id=”@+id/right_drawer” android:layout_width=”240dp” android:layout_height=”match_parent” android:layout_gravity=”end” android:choiceMode=”singleChoice” android:divider=”@android:color/transparent” android:dividerHeight=”0dp” android:background=”#111″/> </android.support.v4.widget.DrawerLayout> ALso check the documantation https://developer.android.com/training/implementing-navigation/nav-drawer.html Make sure you are using … Read more

How to open navigation drawer on button click in main fragment?

if you need open the slide: mDrawerLayout.openDrawer(Gravity.LEFT); //Edit Gravity.START need API 14 if you need close the slide mDrawerLayout.closeDrawer(Gravity.LEFT); //Edit Gravity.START need API 14 EXAMPLE my mDrawerLayout is instanced here: mDrawerLayout = (DrawerLayout)findViewById(R.id.my_drawer_layout); my slide state: mSlideState=false; if you need to know the slide menu state (closed, opened). Use this code: mDrawerLayout.setDrawerListener(new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_menu_slide, … Read more