Search code examples
javaandroidandroid-fragmentsfragmentbottomnavigationview

Android Java Bottom Navigation Bar Fragment not showing


I'm trying to implement a bottom navigation bar, however even though all the functions are called, the fragment view is not shown. Here is my code, please point out what I'm doing wrong or missing. I'm only testing it for one fragment at the time. Below given code is for the activity, a fragment and the activity layout.

Main Activity

public class mActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.m_activity);
    bottomNavigationView = findViewById(R.id.navigationbar);
    bottomNavigationView.setSelectedItemId(R.id.profilenav);
    bottomNavigationView.setOnNavigationItemSelectedListener(listener);
    loadFragment(new HomeFragment());
}

private final BottomNavigationView.OnNavigationItemSelectedListener listener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @SuppressLint("NonConstantResourceId")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.explorenav:
                //Load explore fragment here
                break;
            case R.id.profilenav:
                loadFragment(new HomeFragment());
                Log.d("HomeFragment","Selected");
                return true;
        }
        return false;
    }
};

private void loadFragment(Fragment fragment) {
    // load fragment
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.m_fragment, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
    Log.d("mActivity","loadfragment()");
}

Fragment

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d("HomeFragment","onCreateView Start");
        super.onCreateView(inflater,container,savedInstanceState);
        View v= inflater.inflate(R.layout.home_activity, container, false);
    return v;
}

m_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/includenav"
        layout="@layout/bottom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:layout_gravity="bottom"
        />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        android:id="@+id/m_fragment"
        />
</androidx.appcompat.widget.LinearLayoutCompat>

bottom_navigation_bar.xml The code for the included layout:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/navigationbar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    android:foreground="?attr/selectableItemBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.994"
    app:menu="@menu/navigation" />
    </androidx.constraintlayout.widget.ConstraintLayout>

This is the main code file, However the layout is not updated in the m_fragment frame layout.


Solution

  • you are using linear layout for m_activity and not specifying the orientation for it so the default is horizontal, and your first view (the include) taking the whole width (match parent), So I suggest to you using the frame layout or constraint layout to get your desired

    suggested solution:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <FrameLayout
            android:id="@+id/m_fragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="3dp"
            app:layout_constraintBottom_toTopOf="@id/includenav"
            app:layout_constraintTop_toTopOf="parent" />
    
        <include
            android:id="@+id/includenav"
            layout="@layout/bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>