Bottomnavigationview hides when switching from a tab with collapsing toolbar

Viewed 1476

I have a MainActivity XML that contains a bottomNavigationView (bar) with 5 tabs. Each tab calls a different fragment. Switching from a fragment with a collapsing toolbar in its XML (labelled A in picture) to a fragment with a plain fragment (labelled B in picture) causes the bottomNavigationView (bar) to hide partially off the screen.

Is there a way of preventing this?

Screen with fragment containing collapsing toolbar.

Link: screen with fragment containing collapsing toolbar

Screen of another tab with a plain fragment.

Link: screen of another tab with a plain fragment

Fragment Class containing code for collapsing toolbar

 public class Profile extends Fragment {

    public Profile() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //setHasOptionsMenu(true);

        View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

        Toolbar toolbar = rootView.findViewById(R.id.toolbar);

        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        CollapsingToolbarLayout collapsingToolbar = rootView.findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle("test");

        return rootView;
    }

Corresponding xml containing collapsing toolbar code

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

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

            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/ani_dog_one"
                android:contentDescription="whut"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

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

Plain fragment class

public class Review extends Fragment {

 public Review() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_review, container, false);
        //setHasOptionsMenu(true);
        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.search_bar_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

XML for plain fragment class contains just a plain FrameLayout

MainActivity

public class MainActivity extends AppCompatActivity {

private Intent intent;

private android.support.v4.app.FragmentManager manager;
private android.support.v4.app.FragmentTransaction transaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    manager = getSupportFragmentManager();          
    transaction = manager.beginTransaction();      

    Discover discoverFragment = new Discover();
    transaction.replace(R.id.container, discoverFragment, discoverFragment.getTag()).commit();

    setupBottomNavigationView();

}

private void setupBottomNavigationView() {

BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottom_navigation);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
BottomNavigationViewHelper.enableNavigation(this, bottomNavigationViewEx);

}

public void goToOptions(MenuItem menu) {

 intent = new Intent(this, Options.class);
overridePendingTransition(R.anim.left_in, R.anim.right_out);
startActivity(intent);
}

}

XML for MainActivity

    <LinearLayout 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:orientation="vertical"
        tools:context="com.example.android.project_qwer.MainActivity">

    <!-- main fragments goes here -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </FrameLayout>

    <!-- bottom navigation view -->
    <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_navigation_menu" >

    </com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>

</LinearLayout>
5 Answers

Try to put

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

as the root of all the other fragments you are loading from the BottomNavigationView.

If you use a custom toolbar in the profile fragment,you put this code to onCreateView method:

((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

I had the same problem and this was solved.

Remove android:fitsSystemWindows="true" from root CoordinatorLayout.

Related