I wanna change default ActionBar homeAsUp indicator (drawable) in my AppCompat Toolbar. How to achieve that? Only default arrow shows up.
styles (same for other API\
Just add this line to your styles.xml
<item name="navigationIcon">@drawable/ic_back_arrow</item>
i just found out that it actually works toolbar.setNavigationIcon(R.drawable.ic_action_back) and here is the code to make it work.
@Override
protected void onCreate(Bundle savedInstanceState) {
.....
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_action_back);
Post the following code in onOptionsItemSelected(MenuItem item)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
......
if (id == android.R.id.home) {
startActivity(new Intent(this, MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
Note Mainactivity in this code is the current activity i want my back or home button to go to. I hope this helps
To change icon just call at runtime:
toolbar.setNavigationIcon(R.drawable.home);
Trick with styles/themes not working.
How to enable homeAsUp or call setDisplayHomeAsUpEnabled() on standalone toolbar with appcompat v21
Unfortunately, if this style line is used in a commonly shared AppCompat ToolBar/App bar with other activities in the App, including the main activity, the "navigationIcon" specified will be automatically shown, unlike the standard default HomeAsUpIndicator, which is not shown unless explicitly enabled as desired in an Activity, typically as follows: (in the onCreate())
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
With the above style line, since it behaves in an opposite manner, in a similar fashion, the indicator needs to be explicitly disabled if it is not desired to be shown, as on the main activity, as follows: (in the onCreate())
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
(as observed on Android 4.0.4, 4.3, and 4.4.2 phones)
I tried setHomeAsUpIndicator(int resId)
,it works.
private void initToolbar ( Toolbar mToolbar ) {
mToolbar.setTitleTextColor ( getResources ().getColor ( R.color.main_title ) );
setSupportActionBar ( mToolbar );
ActionBar actionbar = getSupportActionBar ();
actionbar.setDisplayHomeAsUpEnabled ( true );
actionbar.setHomeAsUpIndicator ( R.drawable.ic_action_back );
}
But mToolbar.setNavigationIcon(R.drawable.ic_action_back)
doesn't work :(
If android.support.v7.app.ActionBarDrawerToggle used together with DrawerLayout and Toolbar you can change homeAsUp icon with the following code:
//set home as up indicator
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_up_indicator);
//remove home as up indicator
mDrawerToggle.setHomeAsUpIndicator(null);
to show homeAsUpIndicator indicator instead of home indicator do following:
mDrawerToggle.setDrawerIndicatorEnabled(false);
Docs:
ActionBarDrawerToggle#setHomeAsUpIndicator ActionBarDrawerToggle#setDrawerIndicatorEnabled