Programmatically change Android Action bar homeAsUpIndicator navigation icon

Action bar has default Navigation icon i.e arrow, when we move from one activity to another, if you enable setDisplayHomeAsUpEnabled property then back arrow appear. here is a simple technique to change that icon programmatically.

Homeasupindicator_slider_icon
homeasupindicator_arrow

By default 3 values folder comes, each of these folder will have a style.xml file. you have to make changes in all of them, so that you can see result in all device running on Froyo, ICS, kitkat

Styles_files_values_folder

Here i have created 2 styles with two custom icons. First style is default one & shows slider icon, second one is back arrow.

 <resources>  
   <!--  
     Base application theme for API 11+. This theme completely replaces  
     AppBaseTheme from res/values/styles.xml on API 11+ devices.  
   -->  
   <style name="AppBaseTheme" parent="Theme.AppCompat.Light">  
     <!-- API 11 theme customizations can go here. -->  
     <item name="android:homeAsUpIndicator">@drawable/ic_navigation_drawer</item>  
       <item name="homeAsUpIndicator">@drawable/ic_navigation_drawer</item>  
   </style>  
     <style name="CustomActivityTheme" parent="AppBaseTheme">  
      <item name="android:homeAsUpIndicator">@drawable/ic_action_previous_item</item>  
       <item name="homeAsUpIndicator">@drawable/ic_action_previous_item</item>  
   </style>  
 </resources>  

Include customactivityTheme for second activity in manifest file, it shows a back arrow.

  <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="21" />  
   <application  
     android:allowBackup="true"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name=".FirstActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
     <activity android:name="SecondActivity"  
       android:theme="@style/CustomActivityTheme"></activity>  
   </application>  

Add the below code in your activity to see new home as up indicator icon.

 getSupportActionBar().setDisplayHomeAsUpEnabled(true);  

Click event of homeAsUpIndicator will be handled in onOptionsItemSelected method, refer  below example

Firstactivity.java

 public class FirstActivity extends ActionBarActivity {  
      Button mSecondActivity;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.first_activity);  
           mSecondActivity = (Button)findViewById(R.id.ID_FIRST_BUTTON);  
           mSecondActivity.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View arg0) {  
                     // TODO Auto-generated method stub  
                     StartSecondActivity();  
                }  
           });  
           getSupportActionBar().setDisplayHomeAsUpEnabled(true);  
      }  
      public void StartSecondActivity()  
      {  
           Intent secondActivity = new Intent(this, SecondActivity.class);  
           this.startActivity(secondActivity);  
      }  

SecondActivity.java

 public class SecondActivity extends ActionBarActivity{  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.second_activity);  
           getSupportActionBar().setDisplayHomeAsUpEnabled(true);  
      }  
       @Override  
           public boolean onOptionsItemSelected(MenuItem item) {  
                switch (item.getItemId()) {  
                case android.R.id.home:  
                     finish();  
                     break;  
                }  
                return false;  
       }  
 }  

first_activity.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Now you are in first activity, check out home as up indicator icon"  
     android:id="@+id/id_first_activity_text" />  
   <Button       
     android:layout_centerVertical="true"  
     android:layout_centerInParent="true"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/ID_FIRST_BUTTON"  
     android:text="Tap on Me to see the change"   
     />  
 </RelativeLayout>  

second_activity.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical" >  
     <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Home as up indicator icon is changed"/>  
 </LinearLayout>