-3

How to have a back button beside the app title ?

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.information);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
}

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.create_back, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch(item.getItemId()) {
            case R.id.back: // back to previous activity
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

The back button is showing on the right hand-side. How to move it to the left-hand side ?

create-back

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:icon="@mipmap/back_to"
    android:id="@+id/back"
    android:orderInCategory="100"
    android:title="Back"
    app:showAsAction="always" />
    </menu>

enter image description here

I can create a back icon, just don't know how to move it to left-hand side.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Tony
  • 2,515
  • 14
  • 38
  • 71
  • What does your create_back layout file look like? – MC10 Dec 19 '15 at 08:37
  • You don't need to create your own arrow item. Use Amit's answer. The `R.id.home` is the arrow you want on the left side and it is included in Android by default. It's called up navigation: http://developer.android.com/training/implementing-navigation/ancestral.html – MC10 Dec 19 '15 at 08:45
  • @MC10 I have to remove create_menu ? – Tony Dec 19 '15 at 08:49
  • Just remove that `back` item from the create_back.xml file. – MC10 Dec 19 '15 at 08:50
  • @MC10 not really get it @@...remove the item, let the create_back be blank ? – Tony Dec 19 '15 at 08:53
  • You don't need to create your own menu as you are trying to use Android's up navigation. – MC10 Dec 19 '15 at 08:55

3 Answers3

3

Change case R.id.back: to case R.id.home: .

your problem is solved.

updated

use this code to take icon on leftside ,

final ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.back_dark);


 @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            switch(item.getItemId()) {
                case R.id.home: // back to previous activity
                    onBackPressed();
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • I can see the icon on left-hand side now. If I remove code from onOptionsItemSelected, how do I make the button work? – Tony Dec 19 '15 at 08:58
  • I click the button, but nothing happen. The R.id must be home ? – Tony Dec 19 '15 at 09:09
  • yes , it must be home. checkout this ex for more information http://www.learn2crack.com/2014/06/android-action-bar-example.html – Amit Vaghela Dec 19 '15 at 09:15
  • I follow everything you gave, only `actionBar.setHomeAsUpIndicator(R.mipmap.back_to);` different. But when it get clicked, it does not return to previous activity – Tony Dec 19 '15 at 09:23
  • Your `onOptionsItemSelected` code does not work for me. I have found a solution. – Tony Dec 19 '15 at 09:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98404/discussion-between-amit-vaghela-and-tony). – Amit Vaghela Dec 19 '15 at 09:49
1

Use the following code in your oncreate() of Activity

ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);

And add action

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
        onBackPressed();

        Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
        break;

    }

    return true;
}
Mobile Apps Expert
  • 1,028
  • 1
  • 8
  • 11
0

In order to make the icon worked, just add this

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        onBackPressed();
        return true;
    }

instead of

@Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            switch(item.getItemId()) {
                case R.id.home: // back to previous activity
                    onBackPressed();
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }
Tony
  • 2,515
  • 14
  • 38
  • 71