2

So my menu items wont do anything except show the enlarging animation when clicked on. Im trying to open a new activity with them, I have toast attached to one to see if does anything at all and I'm getting nothing. Is this a common issue?

minSdkVersion 17 targetSdkVersion 27

Layout

<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

The layout is using, android.support.constraint.ConstraintLayout

Menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/home"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_apps"
        android:icon="@drawable/apps"
        android:title="@string/title_apps" />

    <item
        android:id="@+id/navigation_profile"
        android:icon="@drawable/profile"
        android:title="@string/title_profile" />

    <item
        android:id="@+id/navigation_logout"
        android:icon="@drawable/logout"
        android:title="@string/title_logout" />

</menu>

Java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation, menu);
        return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement

    if (id == R.id.navigation_home) {

        Intent navHome = new Intent(this, WordpressActivity.class);
        this.startActivity(navHome);
        return true;
    }

    if (id == R.id.navigation_apps) {
        Toast.makeText(this, "apps is Clicked", Toast.LENGTH_LONG).show();
        return true;
    }

    if (id == R.id.navigation_profile) {
        Intent navProf = new Intent(this, AccountActivity.class);
        this.startActivity(navProf);
        return true;
    }

    if (id == R.id.navigation_logout) {
        Intent navLog = new Intent(this, LogoutActivity.class);
        this.startActivity(navLog);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Thank you in advance for any help or direction to what the issue may be. I'm new to using the menu item click intent and I really don't understand why its not working.

SonhnLab
  • 321
  • 1
  • 11
ValhallaSkies
  • 61
  • 1
  • 12

1 Answers1

4

onOptionsItemSelected is not meant to handle action on BottomNavigationView. Action on MenuItem in BottomNavigationView should be done as follows:

BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView
            .OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement

            if (id == R.id.navigation_home) {

                Intent navHome = new Intent(MainActivity.this, WordpressActivity.class);
                MainActivity.this.startActivity(navHome);
                return true;
            }

            if (id == R.id.navigation_apps) {
                Toast.makeText(MainActivity.this, "apps is Clicked", Toast.LENGTH_LONG).show();
                return true;
            }

            if (id == R.id.navigation_profile) {
                Intent navProf = new Intent(MainActivity.this, AccountActivity.class);
                MainActivity.this.startActivity(navProf);
                return true;
            }

            if (id == R.id.navigation_logout) {
                Intent navLog = new Intent(MainActivity.this, LogoutActivity.class);
                MainActivity.this.startActivity(navLog);
                return true;
            }
            return false;

    });
SonhnLab
  • 321
  • 1
  • 11
Sagar
  • 23,903
  • 4
  • 62
  • 62
  • setOnNavigationItemSelectedListener is cannot resolve, and the returns are return outside method. – ValhallaSkies Jul 17 '18 at 02:29
  • @ValhallaSkies there was an extra `}`. Just remove it. I have updated my answer – Sagar Jul 17 '18 at 02:39
  • 1
    @ValhallaSkies the solution of Sagar is right. I think you can check on this: https://developer.android.com/reference/android/support/design/widget/BottomNavigationView – SonhnLab Jul 17 '18 at 02:41
  • I must be missing something here. If i put it outside of onCreate, the setOnNavigationItemSelectedListener cannot resolve. If I put it within onCreate, the, startActivity cannot resolve method. Im so confused. – ValhallaSkies Jul 17 '18 at 02:46
  • It must be in `onCreate()` Just press `alt+enter` and ensure all the imports are correct. – Sagar Jul 17 '18 at 02:47
  • I only have the option to, Create Method or Rename reference. – ValhallaSkies Jul 17 '18 at 02:50
  • I found the issue, the "this.startActivity" removed the this.. works amazing now. You're great man. Thank you! – ValhallaSkies Jul 17 '18 at 02:52
  • @ValhallaSkies no problem. great! Remember to upvote and mark answer as accepted if it was helpful, so that others can benefit too. – Sagar Jul 17 '18 at 02:53
  • I guess now I need to figure out how to make the active activity nav icon selected. Ha! – ValhallaSkies Jul 17 '18 at 03:17