How to put back icon on a Toolbar inside a Fragment that hides the main Toolbar?

Asked

Viewed 682 times

0

I’m having a problem putting the back icon on a Toolbar inside a Fragment that is hiding the main Toolbar.

I use android studio, and created a Navigation Drawer Activity, where I work with Fragments to open menu options, and I already have a fixed Toolbar for all Fragments.

And then a new problem arose. One of the options I tried to put a Scrolling Activity, and I even managed successfully, however I was having two toolbars as in the image below:

inserir a descrição da imagem aqui

Please ignore the colors, I haven’t adjusted the layout yet.

And then I managed to hide the main Toolbar using:

@Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
}

And then it was almost perfect, this way:

inserir a descrição da imagem aqui

But now I wanted to put a back button, as in this example:

inserir a descrição da imagem aqui

It is easy to put when opening a new Activity, but in this case as it is a Toolbar inside a Fragment that is hiding another Toolbar, I do not imagine at all how it does.

Follows the layout inside the Fragment I use.

Layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="br.com.teste.app.MenuPages.FragmentTerms">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_custom"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="Declarações Legais" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="@string/terms_text" />

</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

1 answer

0


I ended up getting it sorted out. maybe in a gambit.

I added an Imagebutton with an arrow to go back.

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_custom"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="Declarações Legais">

            <ImageButton
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:paddingRight="15dp"
                android:src="@drawable/ic_arrow_white" />
        </android.support.v7.widget.Toolbar>

It may not be the best method, and I don’t even know if it’s gonna be legal on all the devices, but it’s working for now. If anyone still know another better way, please I would love to know other solutions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.