Search code examples
androidandroid-fragmentsbottomnavigationviewandroid-bottomnav

Implementing fragments with the default BottomNavigationView


I made a simple app with the default bottom nav view. Now the codes are correct and the app is building but when I launch it, I have a blank fragment whatever menu item i click.

MainActivity: package com.ali.mydesign;

 import android.os.Bundle;
 import android.support.annotation.NonNull;
 import android.support.design.widget.BottomNavigationView;
 import android.support.v7.app.AppCompatActivity;
 import android.view.MenuItem;
 import android.widget.TextView;
 import android.support.v4.app.Fragment;

public class MainActivity extends AppCompatActivity {

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


    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    Fragment navFragment = null;
        switch (item.getItemId()) {
            case R.id.nav_1:
                navFragment = new HomeFragment();
                return true;
            case R.id.nav_2:
                navFragment = new SecondFragment();
                return true;
            case R.id.nav_3:
                navFragment = new ThirdFragment();
                return true;
        }
        return false;
     }
   };
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/navigation"/>

</LinearLayout>

HomeFragment:

public class HomeFragment extends Fragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_1, container, false);
    return v;
}
}

fragment_1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginTop="?attr/actionBarSize">

 <ScrollView
    android:id="@+id/svLog"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="6dp"
            app:cardBackgroundColor="#FFF"
            app:cardCornerRadius="0dp"
            app:cardElevation="4dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFF"
                    android:orientation="vertical"
                    android:padding="32dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Text"
                        android:textColor="#FFF"
                        android:textStyle="bold"
                        android:textSize="18dp"
                        android:layout_marginBottom="32dp"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="tips"
                        android:textColor="#FFF"
                        android:textSize="16dp"
                        />

                </LinearLayout>

            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>

 </ScrollView>

 </RelativeLayout>

How can I correctly implement the fragments to show content?

SOLVED: Thanks to Gabrielle for his answer, I got it solved by replacing the return true with break, then adding getSupport.. like this:

Fragment navFragment = null;

        switch (item.getItemId()) {
            case R.id.nav_1:
                navFragment = new HomeFragment();
                break;
            case R.id.nav_2:
                navFragment = new SecondFragment();
                break;
            case R.id.nav_3:
                navFragment = new ThirdFragment();
                break;
        }

        getSupportFragmentManager().beginTransaction().replace(R.id.content, navFragment).commit();
        return true;
     }

   };

To set default fragment i used

navigation.setSelectedItemId(R.id.nav_1);

after setContentView()


Solution

  • in your method onNavigationItemSelected() you forgot something like:

    if (navFragment != null){
      getSupportFragmentManager().beginTransaction().replace(R.id.content, navFragment).commit();
    }