XDProgrammer
XDProgrammer

Reputation: 861

How to add the back arrow in the action bar?

I was wondering how I could add this arrow in the action bar. I have already an action bar but can only add icons at the right hand side and is it possible to center the label?

enter image description here

Upvotes: 3

Views: 12588

Answers (5)

Mete
Mete

Reputation: 2884

For actionbar arrow icon:

In Activity:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

In Fragment:

 ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Catching the click (at activity):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        finish();

        return true;
    }
    return super.onOptionsItemSelected(item);
}   

Upvotes: 9

XDProgrammer
XDProgrammer

Reputation: 861

Thank very much guys, no wonder I could not get it, it needs a different set-up, I thought its something you can manipulate on menu xmls

Yes, I extend mine to AppCompatActivity and got the right result using your suggestions:

<activity
        aandroid:name=".TestActivity"
        android:label="@string/title_activity_test">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.testapp.MainActivity" />
    </activity>

then call

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Upvotes: 0

Hadi Satrio
Hadi Satrio

Reputation: 4292

First, you'd have to define the parent Activity for the one you'd like to display back button in. You do this via the manifest.

Do this in your AndroidManifest.xml somewhere within the application tag:

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

That done, all you'd need to do is to call this on your target Activity, right within its onCreate() method:

getActionBar().setDisplayHomeAsUpEnabled(true);

..or, if you happen to use AppCompat library in your project:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

You can refer to the official docs for up navigation here for further details.

Upvotes: 10

Ye Myat Thu
Ye Myat Thu

Reputation: 187

You can set back arrow on Action Bar by calling

getActionBar().setDisplayHomeAsUpEnabled(true);

on your activity onCreate() method.

Note: if you're using AppCompatActivity, use getSupportActionBar() instead of getActionBar();

Upvotes: 1

Gaurav Jindal
Gaurav Jindal

Reputation: 227

You can write this two lines in your java file:

getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true)

;

Upvotes: 1

Related Questions