Android 5.0 for design navigation for KitKat

I see that Android has introduced new navigation box icons, a box icon and a back arrow icon. How can we use this in applications supported by Kitkat. See the latest version of Newsstand on Google for the latest navigation box and animation icons. How can we do this?

I tried setting minSDK to 19 and complileSDK to 21, but it used the old style icons. Is it self-fulfilling?

+61
android android-5.0-lollipop navigation-drawer
Oct 20 '14 at 23:58
source share
4 answers

You need to use the new toolbar in appcompat v21 and the new ActionBarDrawerToggle , which is also in this library.

Add the gradle dependency to your gradle file:

 compile 'com.android.support:appcompat-v7:21.0.0' 

Your activity_main.xml layout will look something like this:

 <!--I use android:fitsSystemWindows because I am changing the color of the statusbar as well--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_parent_view" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:fitsSystemWindows="true"> <include layout="@layout/toolbar"/> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Main layout --> <FrameLayout android:id="@+id/main_fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- Nav drawer --> <fragment android:id="@+id/fragment_drawer" android:name="com.example.packagename.DrawerFragment" android:layout_width="@dimen/drawer_width" android:layout_height="match_parent" android:layout_gravity="left|start" /> </android.support.v4.widget.DrawerLayout> </LinearLayout> 

Your toolbar layout will look something like this:

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" app:theme="@style/ThemeOverlay.AppCompat.ActionBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary"/> 

Your activity should extend from:

 ActionBarActivity 

When you find your views (box and toolbar) in action, set the toolbar as a support action bar and set setDrawerListener:

 setSupportActionBar(mToolbar); mDrawerToggle= new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar, R.string.app_name, R.string.app_name); mDrawerLayout.setDrawerListener(mDrawerToggle); 

After that, you just need to take care of the menu items and the state of drawerToogle:

  @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.menu_main,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public void onBackPressed() { if(mDrawerLayout.isDrawerOpen(Gravity.START|Gravity.LEFT)){ mDrawerLayout.closeDrawers(); return; } super.onBackPressed(); } 

The implementation is the same as before the toolbar, and you get free arrow animation. No headaches. For more information, follow below:

If you want to display the box on the toolbar and in the status bar, see this question .

EDIT: Use the NavigationView from the support design library. A tutorial to learn how to use here: http://antonioleiva.com/navigation-view/

+130
Oct 24 '14 at 15:35
source share

The answer is no longer useful. Leaving it here for historical purposes only, since Android publishing time has not been realized :)




Now there are many libraries that can do this.

Choice 1 - https://github.com/neokree/MaterialNavigationDrawer

Other

+18
Dec 27 '14 at 6:20
source share

If you want a real navigation box with a material design style (defined here )
I have implemented a custom library that does just that,
You can find it here.

+2
Nov 18 '14 at 23:25
source share

Support for top comment along with newly created main_content layout. I just override the included content layout with DrawerLayout. Keep in mind that your drawerlayout should have this layout_behavior: appbar_scrolling_view_behavior

location of the top container https://github.com/juanmendez/jm_android_dev/blob/master/01.fragments/06.fragments_with_rx/app/src/main/res/layout/activity_recycler.xml#L17

content layout enabled https://github.com/juanmendez/jm_android_dev/blob/master/01.fragments/06.fragments_with_rx/app/src/main/res/layout/content_recycler.xml#L9

see the navigation box !!

0
Oct 26 '15 at 10:15
source share



All Articles