Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change home as up resource programmatically?

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.

like image 228
AlikElzin-kilaka Avatar asked May 06 '13 08:05

AlikElzin-kilaka


2 Answers

int upId = Resources.getSystem().getIdentifier("up", "id", "android");
if (upId > 0) {
    ImageView up = (ImageView) findViewById(upId);
    up.setImageResource(R.drawable.ic_drawer_indicator);
}
like image 59
Matthias Robbers Avatar answered Sep 20 '22 20:09

Matthias Robbers


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

<style name="Theme.HomeScreen" parent="AppBaseTheme">
  <item name="displayOptions">showHome|useLogo</item>
  <item name="homeAsUpIndicator">@null</item>
  <item name="android:homeAsUpIndicator">@null</item>
</style>

In code you can change the logo using setLogo() method.

getSupportActionBar().setLogo(R.drawable.abc_ic_ab_back_holo_light); //for ActionBarCompat
getActionBar().setLogo(R.drawable.abc_ic_ab_back_holo_light); //for default actionbar for post 3.0 devices

Also note that the Android API 18 has methods to edit the homeAsUpIndicator programatically, refer documentation.

like image 29
Lalith B Avatar answered Sep 23 '22 20:09

Lalith B