Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarDrawerToggle() icon cannot be set to the ActionBar in Android SDK 5

I have a simple problem with initializing my icon for my action bar. I am using Android SDK 5 and since android.support.v4.app.ActionBarDrawerToggle; was deprecated I imported v7 support and replaced my import with the following: android.support.v7.app.ActionBarDrawerToggle; i.e. the same package with v7.

Now when I initialize my ActionBarDrawerToggle object to associate with my DrawerLayoutobject imported from: import android.support.v4.widget.DrawerLayout; I am forced to remove one argument, which seems ok (have no idea why though!). but my icon does not go in my ActionBar. here is the my initialization for my ActionBarDrawerToggle object:

mActionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open) {

        /** Called when a drawer has settled in a completely closed state. */
        @Override
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

        }

        /** Called when a drawer has settled in a completely open state. */
        @Override
        public void onDrawerOpened(View view) {
            super.onDrawerOpened(view);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

        }
    };

I have the image in my resource namely R.drawable.ic_drawer

I cant seem to find why my icon does not appear in v7.

P.S. the code works perfectly fine, infact when I switch back to v4 import and add the extra argument for toggle object initialization (having to bare with the ugly deprecated cross line ofcourse), the icon appears but not in v7.

thanks in advance :)

like image 284
Ams Avatar asked Nov 27 '14 12:11

Ams


2 Answers

Actually if you look at javadoc of new v7.ActionBarDrawerToggle you could realize that new class doesn't provide the constructor, that takes Drawable as an argument - the 3rd argument where you've provided your Drawable is a String resourse that describes opening drawer. If you want to set a custom icon you have to use another ActionBarDrawerToggle constructor:

ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes)

In that case toolbar's navigation icon will be used as a Drawer icon

like image 85
Oleg Osipenko Avatar answered Nov 01 '22 02:11

Oleg Osipenko


Though v4.ActionBarDrawerToggle was deprecated, you can use another method to change the icon for example below:

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_drawer);
like image 1
Qian Zhang Avatar answered Nov 01 '22 04:11

Qian Zhang