Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Bar Back Button is not showing in Android

Hi I have created an activity which extends ActionBarActivity & using material theme in my application. In the Action Bar, Back button is not showing.

I didn't find why it is not showing. Any help ?

public class RegistrationActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);

        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_background_light));
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!--Support Library compatibility-->
    <item name="actionBarStyle">@style/MyTheme.ActionBarStyle</item>
</style>

<!-- ActionBar styles -->
<style name="MyTheme.ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!--Support Library compatibility-->
    <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>

<style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@android:color/white</item>
</style>

AndroidManifest.xml

    <activity
        android:name=".RegistrationActivity"
        android:label="@string/title_activity_registration" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".HomeScreenActivity" />
    </activity>

Thanks in advance.

like image 938
N Sharma Avatar asked Oct 29 '14 18:10

N Sharma


People also ask

Where is action bar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


2 Answers

If the Jorgesys's solution not worked for you. Try overriding the onOptionsItemSelected method.

public class MyActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        if (id == android.R.id.home)
        {
            onBackPressed();
            return true;
        }
        else
        {
            return super.onOptionsItemSelected(item);
        }
    }
}
like image 199
Muhammad Saqib Avatar answered Sep 22 '22 12:09

Muhammad Saqib


add the property

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

to show the "back button"

like image 31
Jorgesys Avatar answered Sep 19 '22 12:09

Jorgesys