0

I tried

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.v(
                this.getClass().getName() + "!!!", 
                new Exception().getStackTrace()[0].getMethodName()
                );
        MenuItem m_item = (MenuItem)menu.findItem(R.id.action_settings);
        if(m_item != null)
            m_item.setTitle("Back to test");
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.settings, menu);
        return true;
    }

but it always gets null.And also,onCreate seems to have it as null as well. Is there a function that i can modify the text on it during runtime??? And if so, is there an easy way to find it?Right Up Corner, I want to change the text on "Settings" during runtime

tartaruga_casco_mole
  • 1,086
  • 3
  • 21
  • 29
  • What do you want to do? – natario Apr 22 '15 at 23:37
  • Your code makes no sense, you instantiate `m_item` and do not use it anywhere afterwards – rafaelc Apr 22 '15 at 23:44
  • @miav I want to change the text on Settings and maybe add some more options. But I can't get the function which it's called after inflating(or right before). I've updated the post. – tartaruga_casco_mole Apr 22 '15 at 23:48
  • @RafaelCardoso I used in **m_item.setTitle("Back to test");** – tartaruga_casco_mole Apr 22 '15 at 23:50
  • Thats not how it works. You created a variable and set stuff to it, but then you never associate it anywhere. It is the same as doing nothing. In order to add options to this menu, you have to inflate it using a XML file inside `menu` folder – rafaelc Apr 22 '15 at 23:52
  • @RafaelCardoso Yeah, but then the text on it won't be able to be changed during runtime right? Is there a way that i can do that? – tartaruga_casco_mole Apr 22 '15 at 23:56
  • Take a look at this http://stackoverflow.com/questions/8279981/how-can-i-change-action-bar-actions-dynamically – rafaelc Apr 23 '15 at 00:02

2 Answers2

1

Instead of

getMenuInflater().inflate(R.menu.settings, menu);

do something like:

getMenuInflater().inflate(R.menu.menu_custom, menu);

where res/menu/menu_custom.xml is something like:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/search"/>

    <item android:id="@+id/menu_settings"
          android:icon="@drawable/ic_action_settings"
          android:title="@string/settings" />
</menu>

This will give you two items in the dropdown. It looks like your res/menu/settings.xml only has one item in it.

Andrew Cross
  • 1,921
  • 2
  • 19
  • 32
  • But how can i change the text on it during **runtime** ? – tartaruga_casco_mole Apr 22 '15 at 23:58
  • If you want to change the values on runtime you can save a reference to the menu when you create it and call setTitle accordingly (http://stackoverflow.com/questions/7066657/android-how-to-dynamically-change-menu-item-text-outside-of-onoptionsitemssele). Alternatively if you want to be switch menus completely based on something that happened, you can call invalidateOptionsMenu(); and onCreateOptionsMenu() will be called again. You could then do a switch statement to figure out which menu to inflate. – Andrew Cross Apr 23 '15 at 00:04
1

You should add items in the menu xml. You can find it on the folder res/menu. There you have all the menus available for inflation.

I suppose that you have only one. This is how it could look with added options:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/searchMain"    
    android:icon="@drawable/ic_action_search"
    app:showAsAction="ifRoom|withText"
    android:title="Search"/>
<item
    android:id="@+id/searchBarcodeScan"
    android:icon="@drawable/ic_launcher"
    app:showAsAction="always|withText"
    android:title="Scanner"/>
<item
    android:id="@+id/seeList"
    android:icon="@drawable/ic_launcher"
    app:showAsAction="ifRoom|withText"
    android:title="See list"/>
<item
    android:id="@+id/settings"
    android:icon="@drawable/ic_settings"
    app:showAsAction="ifRoom|withText"
    android:title="Settings"/>
</menu>

Then in your activity you can react to the option selected by overriding this method and doing things inside it:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.searchMain:
        doSomething();
        return true;

    case R.id.searchBarcodeScan:
        doSomething2();
        return true;

    case R.id.seeList:
        doSomething3();
        return true;

    case R.id.settings:
        doSomething4();
        return true;
    }
    return super.onOptionsItemSelected(item);
}       

EDIT In order to change menus in runtime, you should call the method invalidateOptionsMenu(). This method will force the recreation of the menu and this time onPrepareOptionsMenu method will be called. You should override it in your Activity this way:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if(someCondition){
        getMenuInflater().inflate(R.menu.main_menu, menu);
    }
    else if(someOtherCondition){
        getMenuInflater().inflate(R.menu.other_menu, menu);
    }
    return true;
}
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
  • Thank you! I didn't know it creates a menu folder. But my problem is, how can i change the text on it during runtime? Since, **onOptionsItemSelected** will be called after the option is selected, it won't be change if use setTitle here, right? – tartaruga_casco_mole Apr 22 '15 at 23:55