Skip to content Skip to sidebar Skip to footer

Change Menuitem Text Color Programmatically

So I have a menu item, that's defined as: Copy

}

The trick here is to force the menu to be invalidated in the activity's onCreate event (thereby causing the onPrepareMenuOptions to be called sooner than it would normally). Inside this method, we can locate the menu item and style as required.

Solution 2:

@RRP give me a clue ,but his solution does not work for me. And @Box give a another, but his answer looks a little not so cleaner. Thanks them. So according to them, I have a total solution.

privatestaticvoidsetMenuTextColor(final Context context, final Toolbar toolbar, finalint menuResId, finalint colorRes) {
    toolbar.post(newRunnable() {
        @Overridepublicvoidrun() {
            ViewsettingsMenuItem=  toolbar.findViewById(menuResId);
            if (settingsMenuItem instanceof TextView) {
                if (DEBUG) {
                    Log.i(TAG, "setMenuTextColor textview");
                }
                TextViewtv= (TextView) settingsMenuItem;
                tv.setTextColor(ContextCompat.getColor(context, colorRes));
            } else { // you can ignore this branch, because usually there is not the situationMenumenu= toolbar.getMenu();
                MenuItemitem= menu.findItem(menuResId);
                SpannableStrings=newSpannableString(item.getTitle());
                s.setSpan(newForegroundColorSpan(ContextCompat.getColor(context, colorRes)), 0, s.length(), 0);
                item.setTitle(s);
            }

        }
    });
}

Solution 3:

In order to change the colour of menu item you can find that item, extract the title from it, put it in a Spannable String and set the foreground colour to it. Try out this code piece

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    MenuInflaterinflater= getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    MenuItemmColorFullMenuBtn= menu.findItem(R.id.action_submit); // extract the menu item hereStringtitle= mColorFullMenuBtn.getTitle().toString();
    if (title != null) {
        SpannableStrings=newSpannableString(title);
        s.setSpan(newForegroundColorSpan(Color.parseColor("#FFFFFF")), 0, s.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); // provide whatever color you want here.
        mColorFullMenuBtn.setTitle(s);
    }
   returnsuper.onCreateOptionsMenu(menu);
}

Solution 4:

It only becomes a text view after inspection, its real class is ActionMenuItemView, on which we can further set the text color like this:

publicstaticvoidsetToolbarMenuItemTextColor(final Toolbar toolbar,
                                               final@ColorResint color,
                                               @IdResfinalint resId) {
    if (toolbar != null) {
        for (inti=0; i < toolbar.getChildCount(); i++) {
            finalViewview= toolbar.getChildAt(i);
            if (view instanceof ActionMenuView) {
                finalActionMenuViewactionMenuView= (ActionMenuView) view;
                // view children are accessible only after layout-ing
                actionMenuView.post(newRunnable() {
                    @Overridepublicvoidrun() {
                        for (intj=0; j < actionMenuView.getChildCount(); j++) {
                            finalViewinnerView= actionMenuView.getChildAt(j);
                            if (innerView instanceof ActionMenuItemView) {
                                finalActionMenuItemViewitemView= (ActionMenuItemView) innerView;
                                if (resId == itemView.getId()) {
                                    itemView.setTextColor(ContextCompat.getColor(toolbar.getContext(), color));
                                }
                            }
                        }
                    }
                });
            }
        }
    }
}

Solution 5:

I spent a lot of hours on this and finally got it into work. There is easy solusion for Android 6 and 7 but it doesn't work on Android 5. This code works on all of them. So, if you are doing it in Kotlin this is my suggestion:

overridefunonCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.my_menu, menu)
        setToolbarActionTextColor(menu, R.color.black)
        this.menu = menu
        returntrue
}

privatefunsetToolbarActionTextColor(menu: Menu, color: Int) {
        val tb = findViewById<Toolbar>(R.id.toolbar)
        tb?.let { toolbar ->
            toolbar.post {
                val view = findViewById<View>(R.id.my_tag)
                if (view is TextView) {
                    view.setTextColor(ContextCompat.getColor(this, color))
                } else {
                    val mi = menu.findItem(R.id.my_tag)
                    mi?.let {
                        val newTitle: Spannable = SpannableString(it.title.toString())
                        val newColor = ContextCompat.getColor(this, color)
                        newTitle.setSpan(ForegroundColorSpan(newColor),
                                0, newTitle.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                        it.title = newTitle
                    }
                }
            }
        }
}

Post a Comment for "Change Menuitem Text Color Programmatically"