Eric Teixeira
Eric Teixeira

Reputation: 353

How to Put the Back Button in Left of my ActionBar

I'm building and finishing a Android App and this is one of the main things I need to Fix it up.

So, I have a Action Bar styled like this:

<resources>
    <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    </style>

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#F6E6E7</item>
        <item name="android:textSize">10sp</item>
    </style>

    <style name="CustomTabWidget">
        <item name="android:textSize">15sp</item>
    </style>


</resources>

And this is the layout where i put the button

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->

    <item android:id="@+id/voltar"
        android:title="Voltar"
        android:icon="@drawable/abc_ic_ab_back_holo_light"
        android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->

</menu>

this Button Tittled Voltar is at the Right Side of the Action Bar.. What Should I Do to put this on Left Side?

Thank you for read this

Upvotes: 4

Views: 8663

Answers (3)

Ian Pinto
Ian Pinto

Reputation: 2299

Declaring getActionBar().setDisplayHomeAsUpEnabled(true); in the onCreate of your activity is right.

However, onOptionsItemSelected implementation suggested by Nayan in the post above can be improved and should be handled as per code below:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        NavUtils.navigateUpFromSameTask(this);
//            finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

The id android.R.id.home represents the Home or Up button. Declaring getActionBar().setDisplayHomeAsUpEnabled(true); will cause the UP (Back/Left Arrow) button to be show. Use NavUtils to allow users to navigate up one level in the application structure.

For more details, see the Navigation pattern on Android Design: http://developer.android.com/design/patterns/navigation.html#up-vs-back

Upvotes: 5

Nayan
Nayan

Reputation: 1605

You need to do two things. First declare this in the onCreate of your activity

getActionBar().setDisplayHomeAsUpEnabled(true);

Then implement the onOptionsItemSelected so that it handles the home as up button click event

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home ) {
        finish();
        return true;
    }
    // other menu select events may be present here

    return super.onOptionsItemSelected(item);
}

Upvotes: 6

Ahmed Hegazy
Ahmed Hegazy

Reputation: 12605

Back Button is supported in android by default. You don't need to create the button manually. Just in the Activity's onCreate() method

getActionBar().setDisplayHomeAsUpEnabled(true);

Upvotes: 1

Related Questions