Helgegren February 2016

Display week in menu

I want to be able to display the number of the week in the action bar inside a picture. My first attempt was to display the title in the icon but it shows either title or icon not both at the same time. Next I tried to create different pictures that could been used for icon containing the number of the week. But i can't find a way to change the icon.

Anybody have an idea what i can do?

This is the code for the menu

<item android:id="@+id/action_week"
  android:title="p"
  android:showAsAction="always"
  android:icon="@drawable/vecka0"/>
<item
  android:id="@+id/menu_overflow"
  android:icon="@drawable/menu_large"
  android:showAsAction="always"
  android:title="@string/Vmeny">
  <menu>
     <item android:id="@+id/action_confirm"
        android:title="@string/confirm_text"
        android:showAsAction="ifRoom" />
     <item android:id="@+id/action_switch_company"
        android:title="@string/Vswitch_company"
        android:showAsAction="never"/>
     <item android:id="@+id/action_logout"
        android:title="@string/logout_text"
        android:showAsAction="ifRoom"/>

  </menu>
</item>

And here is the menu that i want

My Menu

Answers


Spirrow February 2016

maybe you could create a global Menu variable (if you are going to use it later if not make it local) and initialize it on the onCreateOptionsMenu().

private Menu menu;

Inside the onCreateOptionsMenu():

this.memu = menu;

And then get the item you want of your menu and change it:

menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_launcher));


Jayanth February 2016

Shows icon not text

For this in your item tag mention.

<item android:id="@+id/action_week"
 android:title="p"
 android:showAsAction="always|withText" //this would show icon and text both at atime
 android:icon="@drawable/vecka0"/>

Changing Icons Dynamicaly

you need to have reference to menu at the time of creation of menu first.

private Menu mMenu;
@Override
public boolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity, menu);
// Save the menu reference
mMenu = menu;
return super.onCreateOptionsMenu(menu);
}

// For example - Call when you need to change icon
 private void setActionIcon(boolean showWithBadge){
   MenuItem item = mMenu.findItem(R.id.ITEM_ID);
    if(mMenu != null){
        if(showWithBadge){
           item.setIcon(R.drawable.IC_WITH_BADGE);           
          }
           else {
            item.setIcon(R.drawable.IC_WITHOUT_BADGE);
         }
     }
 }

Post Status

Asked in February 2016
Viewed 1,087 times
Voted 8
Answered 2 times

Search




Leave an answer