I used the following hack to change the homeAsupIndicator programmatically.
int upId = Resources.getSystem().getIdentifier("up", "id", "android");
if (upId > 0) {
ImageView up = (ImageView) findViewById(upId);
up.setImageResource(R.drawable.ic_action_bar_menu);
up.setPadding(0, 0, 20, 0);
}
But this is not working on most new phones (HTC One, Galaxy S3, etc). Is there a way that can be changed uniformly across devices. I need it to be changed only on home screen. Other screens would have the default one. So cannot use the styles.xml
You can achieve this in an easier way. Try to can change the homeAsUpIndicator attribute of actionBarStyle in your theme.xml and styles.xml.
If you want some padding, just add some white space in your image.
Also you can define the logo in manifest in attribute android:logo of and tags and set in theme that you want to use logo instead of app icon in the action bar.
The question was to change dynamically the
Up Home Indicator
, although this answer was accepting and it is aboutThemes
andStyles
. I found a way to do this programmatically, according to Adneal's answer which gives me the clue and specially the right way to do. I used the below snippet code and it works well on (tested) devices with APIs mentioned here.For lower APIs, I use
R.id.up
which is not available on higher API. That's why, I retrieve this id by a little workaround which is getting the parent ofhome
button (android.R.id.home) and its first child (android.R.id.up):Note: If you don't use the SDK condition, you will get some
NullPointerException
.You can try this:
If you need change the position of the icon, you must create a drawable file containing a "layer-list" like this:
actionbar_indicator.xml
All you need to do is to use this line of code:
This will change the icon with the up indicator. To disable it later, just call this function again and pass
false
as the param.If someone uses the library support-v7 appcompat, you can directly call this method:
In other case you can use this solution: