ActionBarCompat and NavigationDrawer
In previous posts I talked about new ActionBarCompat.
In this post we'll try to use ActionBarCompat with the NavigationDrawer.
If you have already implemented a NavigationDrawer with ActionBar, it is very easy to migrate to ActionBarCompat.
I will use code I wrote with post Creating a Navigation Drawer.
Here you can see steps:
First of all you have to add ActionBarCompat to your project.
Then:
In your Activity you have to extend ActionBarActivity.
Change getActionBar() with get getSupportActionBar().
Pay attention with invalidateOptionsMenu().
You have to change it with supportInvalidateOptionsMenu();
It is very easy!
Some last tips:
Be careful not to use attributes that work only with API 14+, like ?android:attr/listPreferredItemHeightSmall or ?android:attr/textAppearanceListItemSmall.
In this case you can use different styles using folder values-v14.
Be careful with menu width. Older devices can have very small screens. It could be a good idea to use different width, with a default 240dp, and a value of 320dp only with large screens (with folder values-sw320dp).
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicxk9cA0eqW4TI7y37kgYOfdNfRWRI5Cakfscn2RG2KBQRJiqkkZEEWDVJhVjili763DG8CTXKeWqp_XmR9e8Z04tZ5C7HSzDaePbMLwXF9qxsP8FgeSCcgTe5OAlXTRtiIezpXHOU5NyP/s1600/image1.gif)
That's all... it works...(and certainly can be improved).
You can get code from GitHub:
In this post we'll try to use ActionBarCompat with the NavigationDrawer.
If you have already implemented a NavigationDrawer with ActionBar, it is very easy to migrate to ActionBarCompat.
I will use code I wrote with post Creating a Navigation Drawer.
Here you can see steps:
First of all you have to add ActionBarCompat to your project.
Then:
In your Activity you have to extend ActionBarActivity.
Change getActionBar() with get getSupportActionBar().
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr0i0WadJ4BOyLrNqU8j5wl_P5_6ALPwoLqLfeK953yzUFS5pCGe1g_74sPg1zrcSt2Oet9o_-vlgIhQ6yf3lTyywbuGvoUc5F1JHU1WXoZYfXR6hynPgyIG5OA_9DrYbvUmxU9Kx6IAYQ/s1600/migrate1.gif)
Pay attention with invalidateOptionsMenu().
You have to change it with supportInvalidateOptionsMenu();
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1x7fjBnFNiQCqiRXd0hVZ6li6P9msBKQTzzu-Kh5i5t27QW4gLjVi-7ZBeY8Me0liV4pjAIbjJqwRyjvOA6Qjeyjq8wQKUnUVTY9XMw0ktvQah4xu0szy_uRslXhhlGGcQz8R15sxcBWV/s1600/migrate2.gif)
It is very easy!
Some last tips:
Be careful not to use attributes that work only with API 14+, like ?android:attr/listPreferredItemHeightSmall or ?android:attr/textAppearanceListItemSmall.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGxT1y8ycqlEVzopIyA3L9gTi9xyO12Q7N0J5L1PvimWSHofxoRycARrQ-J1VKpkNafw8swl9G-Hs8kZKzXDQOukuNJamq3Mr0XLcbpewoe2JDrL0FAUXAiadX2QRvh9JkAswT2Hi6c9Pw/s1600/migrate3.gif)
In this case you can use different styles using folder values-v14.
Be careful with menu width. Older devices can have very small screens. It could be a good idea to use different width, with a default 240dp, and a value of 320dp only with large screens (with folder values-sw320dp).
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicxk9cA0eqW4TI7y37kgYOfdNfRWRI5Cakfscn2RG2KBQRJiqkkZEEWDVJhVjili763DG8CTXKeWqp_XmR9e8Z04tZ5C7HSzDaePbMLwXF9qxsP8FgeSCcgTe5OAlXTRtiIezpXHOU5NyP/s1600/image1.gif)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSwWwL_mpxbtDJ71klk-HWStp9zBhODhCH-fXKmaQEgzIJKZ4S79AB5M_8rsmw6dsceHrGlj2-U9PUCGevx1mXx4jStNvhyphenhyphennecZ_ZIP7llFpZFX-bvj_D4H3VhFpAHa4e0cSuKox0zDlCO/s1600/image2.gif)
That's all... it works...(and certainly can be improved).
You can get code from GitHub:
Comments
Post a Comment