Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomNavigationView using androidx

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

like image 566
benb Avatar asked Sep 06 '19 21:09

benb


People also ask

How do I add more than 5 items in BottomNavigationView?

According to Google's Material Design specification, BottomNavigationView does not support more than 5 items. You can use another library for BottomNavigationView since you can't achieve this with Google's library.

What is bottom navigation activity?

↳ com.google.android.material.bottomnavigation.BottomNavigationView. Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation. Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap.


2 Answers

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
 .../>
like image 65
Gabriele Mariotti Avatar answered Oct 20 '22 05:10

Gabriele Mariotti


i have solved same problem by replacing

<android.support.design.widget.BottomNavigationView

>

change it to and also implement the dependencies too

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="56dp"
        >

in build.gradle

    implementation 'com.google.android.material:material:1.2.0'

refer android documentation https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView

like image 24
RvSingh3213 Avatar answered Oct 20 '22 03:10

RvSingh3213