Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add back button to action bar

I have been trying to add a back button to the action bar.

I want my view to look like this: enter image description here

I want to add the back button in the left of the action bar.

I added this code

ActionBar actionBar = getActionBar();  actionBar.setDisplayHomeAsUpEnabled(true); 

but it doesn't work.

How can I fix this?

like image 664
haythem souissi Avatar asked Aug 22 '12 10:08

haythem souissi


People also ask

How do I customize my action bar?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you add action items to the action bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


1 Answers

After setting actionBar.setHomeButtonEnabled(true);

Add the following code:

@Override public boolean onOptionsItemSelected(MenuItem item) {     switch (item.getItemId()) {         case android.R.id.home:             // app icon in action bar clicked; goto parent activity.             this.finish();             return true;         default:             return super.onOptionsItemSelected(item);     } } 
like image 154
Danesh Avatar answered Sep 28 '22 03:09

Danesh