Android: AppCompat 21, how to change the back icon and the overflow icon to a custom one?

冷暖自知 提交于 2019-11-29 09:47:51

Using appcompat 21 to acheive these is pretty easy.

To change the back icon, you'd have to use the support toolbar as your actionbar instead. This post by Chris Banes should get you started.

Once you're done adding the toolbar, all you have to do is:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_id);
toolbar.setNavigationIcon(R.drawable.back_icon);

To change the overflow icon on the other hand, the easiest way is to add a custom style to the main theme declaration. Something like:

<resources>
    <!-- Base application theme. -->
    <style name="Your.Theme" parent="@style/Theme.AppCompat">
        <!-- Your theme items go here -->

        <item name="actionOverflowButtonStyle">@style/OverFlow</item>
    </style>

    <!-- Styles -->
    <style name="OverFlow" parent="@android:style/Widget.AppCompat.ActionButton.Overflow">
        <item name="android:src">@drawable/overflow_icon</item>
    </style>

</resources>

Hope that helps.

I have done this way and it works for me

<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="actionOverflowButtonStyle">@style/OverFlow</item>
</style>

<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
    <item name="android:src">@drawable/custom_button</item>
</style>

Getting the views themselves is a bit tricky, but setting their icons and/or color is possible using the Toolbar's functions. Example:

toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
final Drawable navigationIcon = toolbar.getNavigationIcon();
toolbar.setNavigationIcon(getTintedDrawable(this, navigationIcon, 0xffff0000));
final Drawable overflowIcon = toolbar.getOverflowIcon();
toolbar.setOverflowIcon(getTintedDrawable(this, overflowIcon, 0xffff0000));

public static Drawable getTintedDrawable(@NonNull Context context, @NonNull Drawable inputDrawable, @ColorInt int color) {
    Drawable wrapDrawable = DrawableCompat.wrap(inputDrawable);
    DrawableCompat.setTint(wrapDrawable, color);
    DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN);
    return wrapDrawable;
}
<style name="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="homeAsUpIndicator">@drawable/back_white1</item>
</style>
Piyush

For Change Navigation Arrow check below code

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

First we need to add the content description attribute to the overflow menu icon in styles.xml file. It can be any string, it’s just needed to find the proper view later in code by it’s description.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MyTheme">
    <!-- Customize your theme here. -->
    <item name="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item>
</style>

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

Second, we implement the method responsible for finding and colorizing the Overflow Icon:

  /* It's important to set overflowDescription atribute in styles, so we cangrab the reference
* to the overflow icon. Check: res/values/styles.xml
* @param activity
* @param colorFilter
*/
private static void setOverflowButtonColor(final Activity activity, final PorterDuffColorFilter colorFilter) {
    final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_description);
    final ViewGroup decorView = (ViewGroup) activity.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()) {
                return;
            }
            TintImageView overflow=(TintImageView) outViews.get(0);
            overflow.setColorFilter(colorFilter);
            removeOnGlobalLayoutListener(decorView,this);
        }
    });
}

/***Use below code in your activity screen for apply color*/

 final PorterDuffColorFilter colorFilter= new PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
setOverflowButtonColor(this,colorFilter);

Please check below link for know more https://snow.dog/blog/how-to-dynamicaly-change-android-toolbar-icons-color/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!