0

I thought I should ask a new question for this, but for some context I was able to get the label in position thanks to the good people at How to get a label in the Android action bar

enter image description here .

So now that I've got that, I want the user to be able to tap the Administrator button and then change it to a different mode (probably just "Administrator", "User", "Guest" to start with but there may be more in the future).

How can I get a list of radio boxes to appear when the button in the top right is clicked? Ideally I want to be able to define those various modes dynamically from within the Java class so that if a new type gets added to the database it will be automatically picked up.

If anyone could point me in the right direction I'd appreciate it. I have seen a few examples from Googling, but unfortunately none of them involved the sort of customised drawable I'm using - and none of them had dynamically populated radio options either.

Thanks

Community
  • 1
  • 1
b85411
  • 9,420
  • 15
  • 65
  • 119
  • What do you mean by dynamic? You've listed 3 options, so that's a static list. – OneCricketeer Jul 14 '16 at 07:46
  • There may be more in the future. I have an Sqlite table of user types, I'd like to be able to add an entry to that table (say "Super Admin" for example) and have it automatically be added to the list of radio options here without having to go in and modify the XML manually. – b85411 Jul 14 '16 at 08:05
  • I see. In that case, you'd have to build the `Menu` object in Java and inflate it from `onOptionsItemSelected` for your label. – OneCricketeer Jul 14 '16 at 08:16

1 Answers1

0

You probably want to have a menu which contains your different items.

Do something like this to create the menu and inflate your xml:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}

And then for handling the click events:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.admin:
            switchToAdminUI();
            return true;
        case R.id.guest:
            switchToGuestUI();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

And your my_menu.xml could look something like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
           android:id="@+id/menu_overflow"
           android:icon="@drawable/abs_ic_menu_moreoverflow_material"
           android:showAsAction="always">
            <menu>
                <item
                    android:id="@+id/admin"
                    android:showAsAction="never"
                    android:icon="@drawable/ic_admin"
                    android:title="@string/admin"/>
                <item
                    android:id="@+id/guest"
                    android:showAsAction="never"
                    android:icon="@drawable/ic_guest"
                    android:title="@string/guest"/>
            </menu>
        </item>
</menu>

You can see the android:showAsAction="always" above, which means that it will always show as an action bar icon, and then you put the sub menu items in there.

Try it out and you can also read more about menus here https://developer.android.com/guide/topics/ui/menus.html

malmling
  • 2,398
  • 4
  • 19
  • 33
  • I believe OP would like to keep the label, not use an icon – OneCricketeer Jul 14 '16 at 07:45
  • He wanted to have a dynamic menu to add more items into in the future. But still, if he wants to keep the label in the action bar he can do so by not specifying the top most but instead just have the menu items as a direct child of the first . And he will get the labels in the overflow menu. – malmling Jul 14 '16 at 07:47
  • I was more referring to your use of `android:icon="@drawable ` – OneCricketeer Jul 14 '16 at 08:15
  • Title is also there :) I don't think it's a final solution tailor made for him, could be a point in the right direction, something he can try out and tweak for his needs. – malmling Jul 14 '16 at 08:26