1

I am using Appcompat 7 in my project for Toolbar with navigation toggle. Everything works except the requirement to change the color of DrawerArrowToggle icon dynamically when each activity or fragment changes.

My styles.xml file code is as follows:

<style name="NavigationTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#FFFFF</item>
        <item name="colorPrimaryDark">#F2F2F2</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>


<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">false</item>
        <item name="color">#FFFFFF</item>
    </style>


In the above styles file I have used DrawerArrowToggle color as White, but my requirement is to change into some other color in runtime. I have not posted any code since I am completely stuck and no where I could find even a single piece of code for my requirement.

Community
  • 1
  • 1
Chandru
  • 5,954
  • 11
  • 45
  • 85
  • Just use (in kotlin) ` toolbar.navigationIcon?.apply { if (this is DrawerArrowDrawable) color = /*yourColor*/ } ` I know it is a late answer but just in case someone needs it in future. – Mohamed Alaa Oct 08 '19 at 17:26

1 Answers1

3

I'm not sure if this could work, I haven't tested myself.

First obtain view reference of the navigation icon:

 public static View getToolbarNavigationIcon(Toolbar toolbar){
        //check if contentDescription previously was set
        boolean hadContentDescription = TextUtils.isEmpty(toolbar.getNavigationContentDescription());
        String contentDescription = !hadContentDescription ? toolbar.getNavigationContentDescription() : "navigationIcon";
        toolbar.setNavigationContentDescription(contentDescription);
        ArrayList<View> potentialViews = new ArrayList<View>();
        //find the view based on it's content description, set programatically or with android:contentDescription
        toolbar.findViewsWithText(potentialViews,contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        //Nav icon is always instantiated at this point because calling setNavigationContentDescription ensures its existence 
        View navIcon = null;
        if(potentialViews.size() > 0){
            navIcon = potentialViews.get(0); //navigation icon is ImageButton
        }
         //Clear content description if not previously present
        if(hadContentDescription)
            toolbar.setNavigationContentDescription(null);
        return navIcon;
     }

Once you get the view reference apply ColorFilter to the drawable in this case the ActionBarDrawerToggle icon:

View navigationIcon = getToolbarNavigationIcon(mToolbar);
Drawable navDrawable = navigationIcon.getDrawable();
if(navDrawable != null){
   navDrawable.setColorFilter(newColor, PorterDuff.Mode.MULTIPLY);
}
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • Clearing the Content Description is a bad idea from an accessibility stand point. Perhaps instead you could use the view's tag to store something to indicate it has already been changed? – Cassie Jun 29 '15 at 04:39
  • Indeed. Clearing contentDescription in this snippet will happen if it was not set previously. – Nikola Despotoski Jun 29 '15 at 07:43