3

I searched a lot in google and found in stackoverflow one link how to change color of text using styles and themes but I dont know how to use in code.

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

Please give me snippet for better understanding. I know how to change the background of menuItem by using Factory.In that we can find the View object. Is there any facility to get menu Item. Then we can change the color of menuItem?

Gabe
  • 84,912
  • 12
  • 139
  • 238
ADIT
  • 2,388
  • 9
  • 34
  • 46

3 Answers3

2

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

@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);
    }
Jitendra
  • 1,015
  • 9
  • 24
Anoop
  • 993
  • 6
  • 16
0

@ 0101100101 just change the menu.IconmenutItem to menu.ActionMenuItemView

zerocool
  • 192
  • 2
  • 10
0

I am using the following. Put this method in your activity and call it in the onCreateOptionsMenu(Menu menu) method. You can instead of setting the backgroundResource just set a color... just type view.setbackground and see the possibilities via autocompletion ;)

/*
 * 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;
}
Beasly
  • 1,517
  • 4
  • 20
  • 30
  • i already mentioned that i knew about this code snippet.by Using this we can set background of MenuItem image/color.But I need to change the font color.How to do that? – ADIT Jan 04 '11 at 09:37
  • I think for that you will need your own custom menu. I think there isn't a smal thing to change for it... If the effort is worth it depends what for you need it ;) See here: http://stackoverflow.com/questions/3519277/how-to-change-the-text-color-of-menu-item-in-android – Beasly Jan 04 '11 at 10:14
  • i couldn't understand from this link that why I posted again for detailed explanation or description – ADIT Jan 05 '11 at 06:43
  • 2
    As far as I understood you can't just change the font color like the background. Therefore you will have to code your own I gues menuAdapter and write an XML for it. For sure there is some work to do for it, it's not easy as changing the background. I can't provide a detailed explanaition because I didn't try it myself. Maybe later I think thats "nice to have" stuff but not really important ;) – Beasly Jan 05 '11 at 06:51
  • I'm getting the following error: `java.lang.IllegalStateException: A factory has already been set on this LayoutInflater`... I'm using ActionBarSherlock, does that make any difference? – Lucas Jota Jan 12 '15 at 13:21