-1

I'm kind of new to Android. I added menu icons into my application. Here is my code.

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="1"
        android:showAsAction="never" />

    <item
        android:id="@+id/action_example"
        android:title="Logout"
        android:orderInCategory="2"
        android:showAsAction="withText|ifRoom" />

    <item
        android:id="@+id/addLoc"
        android:title=""
        android:orderInCategory="1"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_location"
        android:onClick="gotoLocation"/>

    <item
        android:id="@+id/dealsPic"
        android:title=""
        android:orderInCategory="2"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_deals"
        android:onClick="doThis"/>

    <item
        android:id="@+id/profPicture"
        android:title=""
        android:orderInCategory="3"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_prof"
        android:onClick="userProfile"
        />

    <item
        android:id="@+id/namePerson"
        android:title="Nathalia Smith"
        android:orderInCategory="4"
        app:showAsAction="ifRoom"
        android:onClick="userProfile"/>
</menu>

After added theses menu icons in to my Action Bar menu icons appeared. but, when I Expand my Navigation drawer, icons just disappear and when I close it icons appear.

What I need is to appear these icons whenever the drawer is opened.

Here is my onCreate.

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle(); //

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFA500"))); // change the color of header

        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // enabling action bar app icon and behaving it as toggle button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.app_name,R.string.app_name)
        {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }
            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();          }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getSupportActionBar().setIcon(R.drawable.ic_deals);
    }

Here is my onDrawerClosed and onDrawerOpened

  mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.app_name,R.string.app_name)
        {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }
            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();          }
        };

hat I need is to appear these icons whenever the drawer is opened. Someone please help.

Thanks in advance. :)

Manuli
  • 1,203
  • 4
  • 20
  • 43
  • 1
    Look at the comments in the `onDrawerClosed()` and `onDrawerOpened()` methods. – Mike M. Apr 07 '16 at 04:56
  • try to remove invalidateOptionsMenu() method inside onDrawerClosed() and onDrawerOpened() method – Mohit Suthar Apr 07 '16 at 04:58
  • you saved my ass Mike M. Can you please post an answer. So, I can accept it. :) Because you are the one who told me first. :) – Manuli Apr 07 '16 at 05:05
  • It's cool. No worries. You can just accept one of the posted answers, if you feel that one of them is an adequate answer. Thanks, though. Cheers! – Mike M. Apr 07 '16 at 05:21
  • Okay Thanks. I feel silly that I didn't even read the comments in the code. :):D – Manuli Apr 07 '16 at 06:01

2 Answers2

1

Remove invalidateOptionsMenu(); from below code as:

       mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_menu, R.string.app_name,R.string.app_name)
                {
                    public void onDrawerClosed(View view) {
                        getSupportActionBar().setTitle(mTitle);

                    }
                    public void onDrawerOpened(View drawerView) {

                       getSupportActionBar().setTitle(mDrawerTitle);

                   }
                };

Also if you want always show the icons use app:showAsAction="always" like below:

<item
        android:id="@+id/dealsPic"
        android:title=""
        android:orderInCategory="2"
        app:showAsAction="always"
        android:icon="@drawable/ic_deals"
        android:onClick="doThis"/>
Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

invalidateOptionsMenu() is used to say Android, that contents of menu have changed, and menu should be redrawn. For example, you click a button which adds another menu item at runtime, or hides menu items group. In this case you should call invalidateOptionsMenu(), so that the system could redraw it on UI. This method is a signal for OS to call onPrepareOptionsMenu(), where you implement necessary menu manipulations. Furthermore, OnCreateOptionsMenu() is called only once during activity (fragment) creation, thus runtime menu changes cannot be handled by this method.

Source of above information:

http://stackoverflow.com/questions/27984041/android-correct-use-of-invalidateoptionsmenu

Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33