Guilherme
Guilherme

Reputation: 7949

Toolbar - add the up button

I am trying to use the Toolbar instead of the ActionBar, but I can't figure out how to add the up button to return to the previous activity. I couldn't find any method that could relate to it.

How do I add the up button?

Upvotes: 25

Views: 18499

Answers (9)

RBT
RBT

Reputation: 25917

In case when previous activity is always same for a given activity then up/back button can be achieved with the help of parentActivityName attribute. It can be mentioned in AndroidManifest.xml file as shown below:

<activity android:name=".DetailActivity" android:parentActivityName=".MainActivity">
     <meta-data android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
</activity>

Let's say DetailActivity was opened from MainActivity. So when you are on DetailActivity then tool bar will automatically show a left pointing arrow (Refer screenshot):

enter image description here

When we click on left pointing arrow then MainActivity gets displayed.

Upvotes: 4

santosh pokhrel
santosh pokhrel

Reputation: 21

if you are using newer version of android studio:

first declare toolbar of androidx not android

 <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorAccent"
        app:popupTheme="@style/Theme.Sunshine.PopupOverlay" />

second, get a reference to the toolbar in Activity.java and use following code;

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar ab= getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

Upvotes: 0

axar
axar

Reputation: 539

You have to add these line back button show automatically

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

on back, button click automatically back to activity

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

Upvotes: 2

Amr
Amr

Reputation: 1312

if you are using navigation component, then adding up button in fragment would be like this:
in your fragment.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presentation.recipeitem.RecipeDetailsFragment">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintTop_toTopOf="parent"
        android:elevation="4dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />
.
.

then in your fragment you would do this
inside onViewCreated(..)

val navController = findNavController()
val appBarConfiguration = AppBarConfiguration(navController.graph)
viewBinding.toolbar.setupWithNavController(navController, appBarConfiguration)

if you need to add title then use this in onResume

    viewBinding.toolbar.title = "my title"

Upvotes: 0

M. Reza Nasirloo
M. Reza Nasirloo

Reputation: 16614

If you're wondering why clicking on up button doesn't work with fragments, you need to set up a navigation listener as well, not sure why Google hasn't enabled it by default:

protected fun setupToolbar(toolbar: Toolbar) {
    (activity as AppCompatActivity).run {
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener { onBackPressed() }
    }
}

Upvotes: 9

gsysko
gsysko

Reputation: 988

If you want to do this in XML, you can use...

<android.support.v7.widget.Toolbar
            app:navigationIcon="?homeAsUpIndicator"
            ...

Upvotes: 9

Abhinav Puri
Abhinav Puri

Reputation: 4284

You can add your own 'up' button in toolbar, after all it is just a ViewGroup.

You can customize toolbar as much as you want, in your toolbar.xml, or wherever you have defined android.support.v7.widget.Toolbar in your layout add your 'up' button like given below :

<android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/toolbar"
   android:minHeight="?attr/actionBarSize"
   android:layout_height="?attr/actionBarSize"
   android:background="@drawable/color_toolbar"
   android:layout_width="match_parent">

   <ImageButton
       android:id="@+id/upButton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:onClick="uphandler"
       android:src="@drawable/backbutton"
       android:layout_gravity="end"/>

</android.support.v7.widget.Toolbar>

Now, define uphandler function in your activity to listen to this up button :

public void uphandler(View v){
       this.finish();    // This will kill current activity, and if previous activity is still opened in background, it will come in front.
}

Upvotes: 2

Jan Omacka
Jan Omacka

Reputation: 1840

I guess what you are looking for is something like this:

Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_detail);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Or in case of using in Fragment:

Toolbar toolbar = (Toolbar) view.findViewById(R.id.app_bar_detail);
((ActionBarActivity) getActivity()).setSupportActionBar(toolbar);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

This will show up your Action Bar inside of your toolbar, but don't worry everything will fit together well. The last you have to do if you dont want any shadow under your action bar or any background of it is change your theme in vaules/styles.xml.

<style name="AppThmeme.Base" parent="Theme.AppCompat.NoActionBar">

Upvotes: 34

stkent
stkent

Reputation: 20128

Calling getSupportActionBar().setHomeButtonEnabled(true); should still work I think, as long as you have already called setSupportActionBar(toolbar);

Upvotes: 2

Related Questions