0

I follow the tutorial https://www.youtube.com/watch?v=iEXh1-KVeVc&t=213s for making bottom menu. Then i use onCreateOptionsMenu and onOptionsItemSelected to set action when click in, but it still not working. Here is my menu code:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/leftNav"
        android:icon="@drawable/ic_baseline_stars_24"
        android:title="News"/>
    <item
        android:id="@+id/home"
        android:title=""/>
    <item
        android:id="@+id/rightNav"
        android:icon="@drawable/ic_baseline_menu_24"
        android:title="Menu"/>
</menu>

MainActivity.java code:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate menu
        getMenuInflater().inflate(R.menu.bottom_nav_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.leftNav:
                // Left nav
                Toast.makeText(this,"Click left",Toast.LENGTH_LONG);
                return true;
            case R.id.rightNav:
                return true;
            case R.id.home:
                Toast.makeText(this,"Click home", Toast.LENGTH_LONG);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

activity_main xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:id="@+id/bottomAppBar"
        app:fabCradleMargin="10dp"
        app:fabCradleRoundedCornerRadius="10dp"
        app:fabCradleVerticalOffset="10dp">
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bottomNavigationView"
            android:layout_marginRight="16dp"
            app:menu="@menu/bottom_nav_menu"
            android:background="@drawable/transparent_background"/>
    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/home"
        android:src="@drawable/ic_baseline_sports_basketball_24"
        app:layout_anchor="@id/bottomAppBar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Any one can help me, i'm new with android studio. Thanks in advance

0 Answers0