What should I pass for root when inflating a layout to use for a MenuItem’s ActionView?

I would simply do it like this: menuItem.setActionView(R.layout.action_view_layout); Let Android inflate the view for you. If you need to do some extra changes on this ImageView call ImageView imageView = (ImageView) menuItem.getActionView(); Update In order to cater to your curiosity. That is what folks from Google do under the hood: public MenuItem setActionView(int resId) { …

Read more

How to display menu item with icon and text in AppCompatActivity

@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_patient_home_screen, menu); menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile))); menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.mipmap.add_user), getResources().getString(R.string.action_add_user))); menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.mipmap.switch_profile), getResources().getString(R.string.action_switch_profile))); menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.mipmap.logout), getResources().getString(R.string.action_sign_out))); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle …

Read more

Mutually exclusive checkable menu items?

This may not be what you’re looking for, but you could write an extension for the MenuItem class that allows you to use something like the GroupName property of the RadioButton class. I slightly modified this handy example for similarly extending ToggleButton controls and reworked it a little for your situation and came up with …

Read more

Android ActionBar MenuItem LowerCase

Solution for native ActionBar implementation: <?xml version=”1.0″ encoding=”utf-8″?> <resources> <style name=”MyTheme” parent=”android:Theme.Holo”> <item name=”android:actionMenuTextAppearance”>@style/MyMenuTextAppearance</item> </style> <style name=”MyMenuTextAppearance” parent=”android:TextAppearance.Holo.Widget.ActionBar.Menu”> <item name=”android:textAllCaps”>false</item> </style> </resources> If you are using ActionBarSherlock there are two different approaches: 1) Create boolean resource abs__config_actionMenuItemAllCaps and set it to false: <?xml version=”1.0″ encoding=”utf-8″?> <resources> <bool name=”abs__config_actionMenuItemAllCaps”>false</bool> </resources> 2) Or create theme with overriden …

Read more

Android adding a submenu to a menuItem, where is addSubMenu()?

Sometimes Android weirdness is really amazing (and amusing..). I solved it this way: a) Define in XML a submenu placeholder like this: <item android:visible=”true” android:id=”@+id/m_area” android:titleCondensed=”Areas” android:title=”Areas” android:icon=”@drawable/restaur” android:enabled=”true”> <menu> <item android:id=”@+id/item1″ android:title=”Placeholder”></item> </menu> </item> b) Get sub menu item in OnCreateOptionsMenu, clear it and add my submenu items, like this: public boolean onCreateOptionsMenu(Menu menu) …

Read more

Android Change Navigation Drawer Menu Items Text programmatically

You can change the title of Navigation Menu Item programmatically by adding following lines in MainActivity.java file. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); … //other stuff here … NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); // get menu from navigationView Menu menu = navigationView.getMenu(); // find MenuItem you want to change MenuItem nav_camara = menu.findItem(R.id.nav_camara); …

Read more

How to add “active” class to wp_nav_menu() current menu item (simple way)

Just paste this code into functions.php file: add_filter(‘nav_menu_css_class’ , ‘special_nav_class’ , 10 , 2); function special_nav_class ($classes, $item) { if (in_array(‘current-menu-item’, $classes) ){ $classes[] = ‘active ‘; } return $classes; } More on wordpress.org: Highlight Current Page in WordPress 3.0 Menus Adding .active class to active menu item