Display Back Button on Action Bar

How to display back button on action bar in android another activity

Just add code this in the onCreate method of your [CurrentActivity].java file.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

And this line of code will just add a back button in your Action Bar, but nothing would happen after tapping that right now.

And add this in your [CurrentActivity].java, this will add the working of that button:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:

Intent intent = new Intent(CurrentActivity.this, MainActivity.class);
startActivity(intent);
finish();
return true;

default:
return super.onOptionsItemSelected(item);
}
}

And replace CurrentActivity to your activity name and replace MainActivity to the activity you want to send user after pressing back button

Display Back Arrow on Toolbar

If you are using an ActionBarActivity then you can tell Android to use the Toolbar as the ActionBar like so:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

And then calls to

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

will work. You can also use that in Fragments that are attached to ActionBarActivities you can use it like this:

((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);

If you are not using ActionBarActivities or if you want to get the back arrow on a Toolbar that's not set as your SupportActionBar then you can use the following:

mActionBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//What to do on back clicked
}
});

If you are using android.support.v7.widget.Toolbar, then you should add the following code to your AppCompatActivity:

@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}

How to set a custom back button in ActionBar?

Use setHomeAsUpIndicator() method instead

actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);

Add back button to action bar

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);
}
}

How to get Back Button in Actionbar with new Jetpack navigation

try this :-

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your stuff here
}
});

second option is;-

@Override
public boolean onSupportNavigateUp() {
return super.onSupportNavigateUp();
// do your stuff here
}

How to enable back press button on the right side of action bar?

What you did is enabled the action-bar's back functionality on click/touch event. If you want a button at the right of the action bar, the best/easy thing you can do is to add an overflow menu, for which you can set-up any icon you want.

There are lots of tutorials on how to do this (for ex. http://www.techotopia.com/index.php/Creating_and_Managing_Overflow_Menus_on_Android).

Essential points are as follows.

  1. Create the layout/items for the overflow menu (filename should match with the one in the 2nd step).

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
    <item
    android:id="@+id/menu_settings"
    android:orderInCategory="1"
    android:showAsAction="never"
    android:icon="@drawable/overflow_menu_icon"
    android:title="@string/menu_settings" />
    </menu>
  2. Init the overflow inside the onCreateOptionsMenu() function, where activity_menu_app is the name of the .xml file created in the previous step.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_menu_app, menu);
    return true;
    }
  3. Catch the touch events of the menu items inside onOptionsItemSelected() function.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_settings:
    // do your stuff here
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
    }

Android - Remove back button from action bar for a particular fragment

you can hide the button on the wanted fragment like this

@Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar(). setDisplayHomeAsUpEnabled(false);
}

@Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar(). setDisplayHomeAsUpEnabled(true);
}


Related Topics



Leave a reply



Submit