I know how to change the homeAsUpIndicator in the styles xml file. The question is how to change it programmatically.
The reason I want to do it because in some views I support side navigation (sliding menu) - pressing the up/back title button, shows the side menu.
In other views I support the natural up/back botton.
Thus I would like to different indicator icons to indicate the two different logics - side navigation vs. up/back.
Please, lets not argue on the motivation of doing this. That's the given state. Thanks.
This worked for me
I write solution for this. It's not beautiful but works:
As a param put: getWindow().getDecorView()
Test on few devices (nexus 7 (4.4.2), samsung galaxy s+ (2.3.6), yaiu g3 (4.2.2)) and emulators with android 2.3.3 and 4.1.1
The solution by @matthias doesn't work on all devices, A better solution to change the
homeAsUpIndicator
is to set it @null in style and change the logo resource programmatically.Below is my code from style.xml
In code you can change the logo using
setLogo()
method.Also note that the
Android API 18
has methods to edit the homeAsUpIndicator programatically, refer documentation.Although this answer might achieve the expected behaviour, as you can read in the comments below it: "This hack does not work on some devices". I found another way according to Adneal's answer which gives me the clue and specially the right way to do.
Lower API: use id R.id.up to retrieve the related
ImageView
.API >= 14: get the relative
Parent
of HomeImageView
(android.R.id.home) and retrieve the first child which is theUpIndicator
(android.R.id.up).Then, this snippet code changes dynamically the
UpIndicator
:I have not tested on multiple devices (that's why I'm not sure this above code works for all devices), however this seems to work great in APIs mentioned in the update part here.
Note: If you don't make the difference between higher and lower APIs, you will get a NullPointerException because
R.id.up
is not available in higher API whileandroid.R.id.up
is not available in lower API.Here is working code