Reputation:
I have added BottomNavigationView. When i tap on any tab then only Fragment is get replaced, it does not showing the new selected tab Icon and Title (Home tab is always shows as selected) What is the problem?
My XML layout - MainActivity.xml
<RelativeLayout 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=".MainActivity">
<FrameLayout
android:id="@+id/FragmentContainerFrameLayout"
android:layout_above="@id/bottomNavAppBar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/bottomNavAppBar"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.bottomnavigation.BottomNavigationView
app:labelVisibilityMode="unlabeled"
android:id="@+id/bottomNavgView"
app:menu="@menu/bottom_nav"
android:background="?android:attr/windowBackground"
app:itemTextColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.appbar.AppBarLayout>
</RelativeLayout>
My java code - MainActivity.java
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
BottomNavigationView bottomNavigationView;
Fragment selectedFragment;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView=findViewById(R.id.bottomNavgView);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.homeMenu:
selectedFragment=new HomeFragment();
break;
case R.id.searchMenu:
selectedFragment=new SearchFragment();
break;
case R.id.addPostMenu:
selectedFragment=null;
startActivity(new Intent(MainActivity.this,AddPostActivity.class));
break;
case R.id.likesMenu:
selectedFragment=new LikesFragment();
break;
case R.id.profileMenu:
selectedFragment=new ProfileFragment();
break;
}
if(selectedFragment!=null)
{
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.FragmentContainerFrameLayout,selectedFragment);
transaction.commit();
}
return false;
}
}
Color file - @menu/bottom_nav
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/black" android:state_checked="true"/>
<item android:color="@android:color/darker_gray"/>
</selector>
Gradle file
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Menu file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/homeMenu"
android:title="Home"
android:icon="@drawable/ic_home"></item>
<item
android:id="@+id/searchMenu"
android:title="Search"
android:icon="@drawable/ic_search"></item>
<item
android:id="@+id/addPostMenu"
android:title="Add Post"
android:icon="@drawable/ic_add_post"></item>
<item
android:id="@+id/likesMenu"
android:title="Likes"
android:icon="@drawable/ic_like"></item>
<item
android:id="@+id/profileMenu"
android:title="Profile"
android:icon="@drawable/ic_profile"></item>
</menu>
I have not performed something out of the world, Why does this happening? Please Help me.
First menu item icon is always selected,not changing
Proper Fragment is replaced but not showing Icon and Title.
Upvotes: 0
Views: 1429
Reputation: 363537
You have to return true
in the onNavigationItemSelected
method:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
...
return true;
}
You can check the doc:
Returns
boolean
true to display the item as the selected item and false if the item should not be selected. Consider setting non-selectable items as disabled preemptively to make them appear non-interactive.
Also you are using app:labelVisibilityMode="unlabeled"
.
It means that the label is hidden for all navigation items (LABEL_VISIBILITY_UNLABELED
)
Upvotes: 2