I have created some app and I had like to insert to it a BottomNavigationView.
The code worked perfectly however once I changed at my gradle to androidx it stopped working.
The component in my layout (activity_about):
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation"
app:itemBackground="@color/colorWhite"
app:itemIconTint="@drawable/bottom_navigation_foreground"
app:itemTextColor="@drawable/bottom_navigation_foreground" />
The menu file is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/navigation_library"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Library"
android:icon="@drawable/ic_home_black_24dp"/>
<item
android:id="@+id/navigation_search"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Search"
android:icon="@drawable/ic_search_black_24dp"/>
<item
android:id="@+id/navigation_profile"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Profile"
android:icon="@drawable/ic_account_circle_black_24dp"/>
</menu>
The drawable file (bottom_navigation_foreground):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/colorPurpleFont" />
<item android:state_checked="false" android:color="@color/Gray" />
</selector>
and my code is as follows:
public class AboutActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_library:
startActivity(new Intent(AboutActivity.this, DiscoverActivity.class));
break;
case R.id.navigation_search:
Toast.makeText(AboutActivity.this, "Favorites", Toast.LENGTH_SHORT).show();
break;
case R.id.navigation_profile:
Toast.makeText(AboutActivity.this, "Nearby", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
}
}
Any reason it is not working since I changed to androidx?
Thank you
Using androidx you have to switch to BottomNavigationView
in the Material components library.
Add the dependency in build.gradle
dependencies {
//..
implementation 'com.google.android.material:material:1.2.1'
}
use a material theme and add in your layout:
<com.google.android.material.bottomnavigation.BottomNavigationView
.../>