Button opens wrong menu

I am currently making a program which opens a menu with a traditional menu button. There two different buttons: first should open the left side menu and the second is made for opening settings. The problem is both these buttons open the same menu. Please help me to make things so that each button opens its own menu.

I've made the main part of the app with a youtube video about these buttons. But there is a problem with them.

The following is the snapshot of menu that is opened with left side button:

Menu that opens left side button

And the following is the look of the app

The look of the app

And the following is the snapshot of the same menu opened with second button

The second button opens the same menu

Here is my code:

package com.danielliakhovetskyi.mainactivity;

import android.content.ClipData;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity {

    Menu menu;
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle actionBarDrawerToggle;
    NavigationView navigationView;
    MenuItem maths;
    private boolean menuItemsAssigned = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Objects.requireNonNull(getSupportActionBar()).setBackgroundDrawable(new ColorDrawable
                (Color.parseColor("#872be3"))); //making ActionBar light-coloured
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.abs_layout);


        drawerLayout = findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        navigationView = findViewById(R.id.navview);
        navigationView.setItemTextAppearance(R.style.WithFont);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            Toast.makeText(MainActivity.this, "Default is clicked", Toast.LENGTH_SHORT).show();
            return super.onOptionsItemSelected(item);
        } else {
            return false;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.navigation_menu, menu);
       /* this.menu = menu;

        maths = menu.findItem(R.id.maths);
        Logger.getGlobal().log(Level.INFO, "Maths Clicked");
        Toast.makeText(this, "" + maths, Toast.LENGTH_SHORT).show();
        maths.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                maths.setTitle("Maths");
                Toast.makeText(MainActivity.this, "WORKS " + maths, Toast.LENGTH_SHORT).show();
                Logger.getGlobal().log(Level.INFO, "Maths Clicked");
                return false;
            }
        });
        menuItemsAssigned = true;*/
        return true;
    }

}

Upvotes: 0

Views: 51

Answers (1)

forpas
forpas

Reputation: 164164

In onCreateOptionsMenu() there is this line:

getMenuInflater().inflate(R.menu.navigation_menu, menu);

which apparently is wrong because it inflates the menu for the navigation drawer as the action bar menu.
Replace R.menu.navigation_menu with the menu for the action bar.

Upvotes: 1

Related Questions