Umesh Kumar Saraswat
Umesh Kumar Saraswat

Reputation: 768

How to change the icon navigation drawer closed or opened

My question is very clear that i want to change the Up icon When NavigationDrawer open and closed. So please tell me how to Change the upp icon in anction bar. I have tried so much but i m not able to do. ii have used custom layout to set the title. Thanks in advance

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.center_action_bar_text);
        actionBar.setTitle("dvds");
        View view = actionBar.getCustomView();
        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText("zfdgfdg");
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);


        ---------------------------------
        drawerListView.setAdapter(drawerListAdapter);
        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);


        drawerToggle = new ActionBarDrawerToggle(HomeActivity.this,
                drawerLayout, R.drawable.abc_ic_clear_search_api_holo_light, R.string.drawer_open,
                R.string.drawer_close){

                    @Override
                    public void onDrawerClosed(View drawerView) {
                        super.onDrawerClosed(drawerView);
                    }

                    @Override
                    public void onDrawerOpened(View drawerView) {
                        super.onDrawerOpened(drawerView);
                    }
        };
        drawerLayout.setDrawerListener(drawerToggle);
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.onOptionsItemSelected(item)) {
             return true;
           }
        return super.onOptionsItemSelected(item);
    }

I want to use this image when drawer open I want to use this image when drawer closed

Upvotes: 0

Views: 937

Answers (2)

tej shah
tej shah

Reputation: 3095

i have other one answer

just add this method:-

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        //boolean drawerOpen1 = mDrawerLayout.isDrawerOpen(this.slider);
        //menu.findItem(R.id.sliding).setVisible(!drawerOpen);
        if(drawerOpen)
        {
            menu.findItem(R.id.sliding).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
        }
        else {
            menu.findItem(R.id.sliding).setIcon(R.drawable.menuicon);
        }
        //menu.findItem(R.id.sliding).setIcon(R.drawable.ic_launcher);
        //menu.findItem(R.id.action_settings).setVisible(!drawerOpen1);
        return super.onPrepareOptionsMenu(menu);
    }

for that method call you need to use this tow method

invalidateOptionsMenu();
// calling onPrepareOptionsMenu() to hide action bar icons

when menu is open

    public void onDrawerOpened(View drawerView) {
        getActionBar().setTitle(mDrawerTitle);
        //mDrawerLayout.openDrawer(Gravity.END);
        // calling onPrepareOptionsMenu() to hide action bar icons
        invalidateOptionsMenu();
    }

when menu is close

public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                //menu.findItem(R.id.sliding).;
                //mDrawerLayout.closeDrawer(Gravity.END);
                // calling onPrepareOptionsMenu() to show action bar icons

                invalidateOptionsMenu();
            }

Upvotes: 1

max59
max59

Reputation: 606

Add this method to your code:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

Upvotes: 1

Related Questions