Android – how to programmatically set the toolbar collapseicon color
•
Android
I want to change the color of the back button in the toolbar (circled white arrow) when the search is displayed
I managed to change the color of all other elements, and I insisted on using the back arrow color
I can set collapseicon (backward arrow drawable) from XML:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:collapseIcon=I_WANT_TO_SET_THIS_PROGRAMMATICALLY>
I set app: collapseicon to anything I want to draw, but I need to set it dynamically
None of the advice I found here suits me
Not this one:
final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable. abc_ic_ab_back_material);
upArrow.setColorFilter(myColor, PorterDuff.Mode.SRC_ATOP);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(upArrow);
Or this:
appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
Drawable d = ContextCompat.getDrawable(MyActivity.this, R.drawable.ic_back_white);
d.setColorFilter(myColor, PorterDuff.Mode.SRC_ATOP);
toolbar.setNavigationIcon(d);
// Drawable d = ContextCompat.getDrawable(MyActivity.this, R.drawable.ic_back_white);
// d.setColorFilter(myColor, PorterDuff.Mode.SRC_ATOP);
// getSupportActionBar().setHomeAsUpIndicator(d);
}
});
I can't find anything else
Can I help you?
thank you
resolvent:
Finally done
This works for me:
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
AppBarLayout appBar = (AppBarLayout) findViewById(R.id.appbar);
appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
for (int i = 0; i < toolbar.getChildCount(); i++) {
View view = toolbar.getChildAt(i);
if (view instanceof ImageButton) {
ImageButton btn = (ImageButton) view;
Drawable drawable = btn.getDrawable();
drawable.setColorFilter(new_button_color, PorterDuff.Mode.SRC_ATOP);
btn.setImageDrawable(drawable);
}
}
}
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码