2

I want to make Bottom Navigation Layout with Fragments. But I am not able to show the fragments in my application. I have shown many videos of fragment, tried many methods to show the fragment, but still I can't able to show the fragments in my application. please if possible then solve my problem.

package com.example.login;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Switch;

import com.example.login.ui.login.ChatFragment;
import com.example.login.ui.login.HomeFragment;
import com.example.login.ui.login.MyAddsFragment;
import com.example.login.ui.login.PostAddFragment;
import com.example.login.ui.login.ProfileFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

public class HomeActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemReselectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_home2);
   BottomNavigationView navView = findViewById(R.id.nav_view);
   navView.setOnNavigationItemReselectedListener(this);
   }

   private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
               Fragment fragment = null;
                switch (menuItem.getItemId()) {
                    case R.id.navigation_home:
                        fragment = new HomeFragment();
                        break;

                    case R.id.navigation_chat:
                        fragment = new ChatFragment();
                        break;

                    case R.id.navigation_post_add:
                        fragment = new PostAddFragment();
                        break;

                    case R.id.navigation_my_adds:
                        fragment = new MyAddsFragment();
                        break;

                    case R.id.navigation_profile:
                        fragment = new ProfileFragment();
                        break;


                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
                return true;
            }
        };

        @Override
        public void onNavigationItemReselected(@NonNull MenuItem menuItem) {

        }
    }

XML Files activity_home.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:paddingTop="?attr/actionBarSize">



<fragment
    android:id="@+id/fragment_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/mobile_navigation"
    android:layout_marginBottom="56dp"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        app:itemIconTint="@color/orange"
        app:itemTextColor="@color/black"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu" />

</RelativeLayout>
Pratik PSB
  • 177
  • 1
  • 18

3 Answers3

1

You forgot to set your listener. Add this:

navView.setOnNavigationItemSelectedListener(navListener);

Also, based on your latest edit, this is not allowed:

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();

because fragment_container is a fragment. It should be of type ViewGroup such as a LinearLayout.

user1506104
  • 6,554
  • 4
  • 71
  • 89
1

You forgot to wrote this line:

 navView.setNavigationItemSelectedListener(this);

Your new updated code is :

package com.example.login;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Switch;

import com.example.login.ui.login.ChatFragment;
import com.example.login.ui.login.HomeFragment;
import com.example.login.ui.login.MyAddsFragment;
import com.example.login.ui.login.PostAddFragment;
import com.example.login.ui.login.ProfileFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

public class HomeActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemReselectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_home2);
   BottomNavigationView navView = findViewById(R.id.nav_view);
   navView.setOnNavigationItemSelectedListener(navListener);
   navView.setOnNavigationItemReselectedListener(navView);
   }

   private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
               Fragment fragment = null;
                switch (menuItem.getItemId()) {
                    case R.id.navigation_home:
                        fragment = new HomeFragment();
                        break;

                    case R.id.navigation_chat:
                        fragment = new ChatFragment();
                        break;

                    case R.id.navigation_post_add:
                        fragment = new PostAddFragment();
                        break;

                    case R.id.navigation_my_adds:
                        fragment = new MyAddsFragment();
                        break;

                    case R.id.navigation_profile:
                        fragment = new ProfileFragment();
                        break;


                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
                return true;
            }
        };

        @Override
        public void onNavigationItemReselected(@NonNull MenuItem menuItem) {

        }
    }

And XML elements structure put like this :

<DrawerLayout>
    <FrameLayout/>
    <NavigationView/>
</DrawerLayout>
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
  • Post crash issue here.. first chaneg XML style mentioned above refer : https://stackoverflow.com/a/48007923/3974530 – InsaneCat Jan 02 '20 at 13:00
  • Your XML structure is wrong try out this demo: https://github.com/afrussel/bottom-bar_with-fragments – InsaneCat Jan 02 '20 at 13:08
1

Please try with fragment.add() method.

SHA
  • 57
  • 4