I am writing an Android app in Java, I refer to this video to make a bottom navigation bar.
As shown in the picture, when I switch to the activity that includes the bottom navigation bar, the fragment displays at home, but the icon is still displayed in the message. How can I make my icon default at Home?
This is my activity which includes the bottom navigation bar:
package com.example.a1221_test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import com.example.a1221_test.databinding.ActivitySecondBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class SecondActivity extends AppCompatActivity {
ActivitySecondBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySecondBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
replaceFragment(new c_HomeFragment());
binding.bottomNavigationView2.setOnItemSelectedListener(item -> {
switch (item.getItemId()){
case R.id.home:
replaceFragment(new c_HomeFragment());
break;
case R.id.message:
replaceFragment(new a_MessageFragment());
break;
case R.id.food:
replaceFragment(new b_FoodFragment());
break;
case R.id.closet:
replaceFragment(new d_ClosetFragment());
break;
case R.id.question:
replaceFragment(new e_QuestionFragment());
break;
}
return true;
});
}
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout,fragment);
fragmentTransaction.commit();
}
}
This is my layout
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e8cfa6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:itemIconTint="@drawable/bottom_navigation_colors"
app:itemTextColor="@drawable/bottom_navigation_colors"
app:itemTextAppearanceActive="@style/BottomNavigationViewTextStyle"
app:itemTextAppearanceInactive="@style/BottomNavigationViewTextStyle"
app:menu="@menu/bottom_nav_menu"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Call this, below replaceFragment
replaceFragment(new c_HomeFragment());
binding.bottomNavigationView2.getMenu().findItem(R.id.home).setChecked(true);