Reputation: 184
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>
Upvotes: 2
Views: 889
Reputation: 7086
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.
Upvotes: 1
Reputation: 2161
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>
Upvotes: 1