How to programmatically change the color and size of a menu item?

I searched a lot on google and found one link on stackoverflow on how to change the color of text using styles and themes, but I don't know how to use it in the code.

<style name="TextAppearance.Widget.IconMenu.Item" parent="@android:style/TextAppearance.Small"> <item name="android:textColor">#ff0000</item> </style> 

Please give me a snippet for a better understanding. I know how to change the menuItem background using Factory. In this we can find the View object. Is it possible to get a menu item. Then can we change the color of the menuItem?

+3
android
Jan 4 2018-11-11T00:
source share
3 answers

try this code to change the background color and text ....

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.my_menu, menu); getLayoutInflater().setFactory(new Factory() { @Override public View onCreateView(String name, Context context, AttributeSet attrs) { if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) { try { LayoutInflater f = getLayoutInflater(); final View view = f.createView(name, null, attrs); new Handler().post(new Runnable() { public void run() { // set the background drawable view.setBackgroundResource(R.drawable.my_ac_menu_background); // set the text color ((TextView) view).setTextColor(Color.WHITE); } }); return view; } catch (InflateException e) { } catch (ClassNotFoundException e) { } } return null; } }); return super.onCreateOptionsMenu(menu); } 
+2
Aug 19 '12 at 13:20
source share
β€” -

I am using the following. Put this method in your activity and call it in the onCreateOptionsMenu method (menu). Instead of setting backgroundResource, you can just set the color ... just enter view.setbackground and view the options through autocomplete;)

 /* * IconMenuItemView is the class that creates and controls the options menu * which is derived from basic View class. So We can use a LayoutInflater * object to create a view and apply the background. */ protected void setMenuBackground() { Log.d(TAG, "Enterting setMenuBackGround"); getLayoutInflater().setFactory(new Factory() { public View onCreateView(String name, Context context, AttributeSet attrs) { if (name .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) { try { // Ask our inflater to create the view LayoutInflater f = getLayoutInflater(); final View view = f.createView(name, null, attrs); /* * The background gets refreshed each time a new item is * added the options menu. So each time Android applies * the default background we need to set our own * background. This is done using a thread giving the * background change as runnable object */ new Handler().post(new Runnable() { public void run() { view .setBackgroundResource(R.drawable.row_blue_menu); } }); return view; } catch (InflateException e) { } catch (ClassNotFoundException e) { } } return null; } }); } 

.

  @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.start_menue, menu); setMenuBackground(); return true; } 
0
Jan 04 2018-11-11T00:
source share

@ 0101100101 just change menu.IconmenutItem to menu.ActionMenuItemView

0
Jun 30 '15 at 2:26
source share



All Articles