0

I want to change color of overflow icon, overflow menu texts and background color of overlow menu programmatically. solutions in Google are done with xml files generally. But I want to do it programmatically because user can change these colors. Also I found these answers 1, 2 but they do not work for me. outViews array always empty. How can I change these colors?

I tried these :

        menu.add("overflow");

        final String overflowDescription = getActivity().getString(R.string.accessibility_overflow);
        final ViewGroup decorView = (ViewGroup) getActivity().getWindow().getDecorView();
        final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
        {
            @Override
            public void onGlobalLayout()
            {
                final ArrayList<View> outViews = new ArrayList<View>();
                decorView.findViewsWithText(outViews, overflowDescription,
                        View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
                if (outViews.isEmpty())
                {
                    Log.d(TAG, "empty array");
                    return;
                }
                ImageView overflow = (ImageView) outViews.get(0);
                overflow.setColorFilter(Color.CYAN);
                removeOnGlobalLayoutListener(decorView, this);
            }
        });

        menu.add("overflow");

        // The content description used to locate the overflow button
        final String overflowDesc = getString(R.string.accessibility_overflow);
        // The top-level window
        final ViewGroup decor = (ViewGroup) getActivity().getWindow().getDecorView();

        final ArrayList<View> outViews = new ArrayList<>();
        // Traverse the view-hierarchy and locate the overflow button
        decor.findViewsWithText(outViews, overflowDesc,
                View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        // Guard against any errors
        if (outViews.isEmpty()) {
            Log.d(TAG,"empty array");
            return;
        }
        // Do something with the view
        final ImageButton overflow = (ImageButton) outViews.get(0);
        overflow.setImageResource(R.drawable.sil);

string.xml

<string name="accessibility_overflow">overflow</string>

style.xml

<style name="Widget.ActionButton.Overflow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
    <item name="android:contentDescription">@string/accessibility_overflow</item>
</style>

<style name="Your.Theme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item>
</style>
Community
  • 1
  • 1
utarid
  • 1,642
  • 4
  • 24
  • 38

0 Answers0