How to add Up button on Action Bar?

In the previous posts, we discussed how to add Action Bar and handle action items clicks. In this post, I will teach you how to add ‘Up’ or Navigation button on Action Bar.

Navigation button appears along with the icon in the action bar with the caret symbol.

For Ex: GMail Application with ‘Up’ button

Action Bar Up Button

Prerequisite:

I would recommend you to take a look at the previous posts so that you can follow this post with much ease, here are the links:

What is Action Bar?

How to add Action Bar in Android Applications?

How to handle clicks on Action Bar action items?

You need to create a simple Android application with Action Bar along with few activities to add navigation among them. Take a help from this post How to handle clicks on Action Bar action items? where you can find steps to create application with Action Bar and also it discusses about handling clicks on Action items.

You can download source code from here if you don’t want to create Application from scratch, otherwise please proceed with below listings.

Steps to add Up navigation button in Action Bar are:

  1. Enable the app icon as an Up button using setDisplayHomeAsUpEnabled() method
  2. Specify the parent activity in the manifest file

Enable the app icon as an Up button

Call setDisplayHomeAsUpEnabled() method in onCreate() method of the activity for which you want to enable Up button as shown below.

// If your minSdkVersion is below 11 use:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher use:
getActionBar().setDisplayHomeAsUpEnabled(true);

Specify the parent activity in the manifest file

When the parent activity is always the same for the child activity in all scenarios:

Declare parent activity for the child activity in the manifest, the action bar automatically takes User to parent activity when the user presses the Up button.

Beginning in Android 4.1 (API level 16), you can declare the parent with the parentActivityName attribute in the element as shown below.

<activity
    android:name="com.prgguru.example.ProductActivity"
    android:label="@string/title_activity_product"                 
    android:parentActivityName="com.prgguru.example.MainActivity">
</activity>

To support older devices with the support library, also include a element that specifies the parent activity as the value for android.support.PARENT_ACTIVITY as shown below

<activity
    android:name="com.prgguru.example.ProductActivity"
    android:label="@string/title_activity_product">
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
         android:name="android.support.PARENT_ACTIVITY"
         android:value="com.prgguru.example.MainActivity" />
 </activity>

When the parent activity is not same for the child activity in different scenarios:

If parent activity is different, you need to handle it based on the action item ID as shown below. Action Bar’s up button ID is ‘android.R.id.home’.

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
       if(Condition 1){     
          Intent parentIntent1 = new Intent(this,ParentActivity1.class);
	  startActivity(parentIntent1);
	  return true;
       }else if(Condition 2){  
          Intent parentIntent2 = new Intent(this,ParentActivity2.class);
	  startActivity(parentIntent2);
	  return true;
       }else{ 
          Intent parentIntent3 = new Intent(this,ParentActivity3.class);
	  startActivity(parentIntent3);
	  return true;
       }
    }
    return super.onOptionsItemSelected(item);
}

Note: android.R.id.home is supported in API level 11 or higher.

Enable Action Bar Up button in Product and Contactus activities

Open ProductActivity.java and ContactusActivity.java, enable Up button by including below code:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
//getActionBar().setDisplayHomeAsUpEnabled(true);

Add parent activity for Product and Contactus activities in AndroidManifest

Open AndroidManifest.xml and update activity tags of Product and Contactus activities with below code:

 <activity
            android:name="com.prgguru.example.ProductActivity"
            android:label="@string/title_activity_product" android:parentActivityName="com.prgguru.example.MainActivity">
            <!-- Parent activity meta-data to support 4.0 and lower -->
             <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.prgguru.example.MainActivity" />
        </activity>
        <activity
            android:name="com.prgguru.example.ContactusActivity"
            android:label="@string/title_activity_contactus"  android:parentActivityName="com.prgguru.example.MainActivity">
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.prgguru.example.MainActivity" />
        </activity>

[pglinkadssmall1]

Demo

That’s all. It’s time to test our code.

Run the application using emulator or device by right clicking on the project >> Run as >> Android applicaiton >> Choose emulator or device.



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android  is the installation file similar to exe in windows.

[pglinkadssmall]

If you feel this article is helpful and interesting please spread a word about it to your friends and colleagues by sharing the article in Facebook or Twitter.


Share

You are always welcome to provide your comments and feedback from comment box.

Next Tutorial >> How to hide and show Action Bar?

Reference
Action Up Button Integration
[pgwriteforus]
[pgfeedback]

Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares