Skip to content Skip to sidebar Skip to footer

Change Custom Submenu Background Color With Java Code

In my home activity, I have a toolbar with a custom Overflow menu and I want it to change color to be accorded with toolbar background color (cause toolbar background color can cha

Solution 1:

So...no one helped me...so I had to work alone...hours and hours...to finally find the solution :

@OverridepublicViewonCreateView(String name, Context context, AttributeSet attrs)  {
    // Do you own inflater stuff here // Check for menu items (Options menu AKA menu key) if (name.equalsIgnoreCase("android.support.v7.view.menu.ListMenuItemView")) {
        try { 
            // Ask our inflater to create the view 
            final View view = LayoutInflater.from(context).createView(name, null, attrs); 
            // Kind of apply our own background newHandler().post(newRunnable() {
                publicvoidrun() {
                    if (!Common.compatible(Common.color, 0xFF000000)) {
                        try {
                            ((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFFFFFFFF);
                        } catch (ClassCastException e) {

                        }
                    } else {
                        try {
                            ((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFF000000);
                        } catch (ClassCastException e) {

                        }
                    }
                    view.setBackgroundColor(Common.color);
                }
            });
            return view;
        } catch (InflateException e) {

        } catch (ClassNotFoundException e) {

        }
    }
    returnnull; 
}

If background is dark...so textColor is white...and if background is light, textColor is black.

Below are two screens with different toolbar color : Red toolbarGreen toolbar

Hope it helps someone else than me, Darkball60 (and if you like the answer...feel free to upvote :))

Post a Comment for "Change Custom Submenu Background Color With Java Code"