1

I want to set On Click Listener on menu item while i click on that menu then directly show dialog or any other activity, but how...?

Button btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new View.OnClickListener() {
      @Override
        public void onClick(View v) {
            UpdateChecker.checkForDialog(Home.this);
        }
    });
Tanim
  • 45
  • 1
  • 5
  • Please go through in this link:- [https://stackoverflow.com/questions/57745695/void-android-widget-button-setonclicklistenerandroid-view-viewonclicklistener/57745812#57745812](https://stackoverflow.com/questions/57745695/void-android-widget-button-setonclicklistenerandroid-view-viewonclicklistener/57745812#57745812) – Manoj Kumar Sep 03 '19 at 09:10

5 Answers5

1

You create your menu options:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your_menu, menu);
    return true;
}

Then you use the following for click events on menu items:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.button1:
        UpdateChecker.checkForDialog(Home.this);
        return true;
    case R.id.option_two:
        //do something else
        return true;
...
    default:
        return super.onOptionsItemSelected(item);
    }
}

Then choose the dialog that suits your style from the developers website e.g a standard alert dialog is:

new AlertDialog.Builder(context)
    .setTitle("Your Title here")
    .setMessage("Your message here")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // Do something
        }
     })
    .setNegativeButton(android.R.string.no, null)
    .setIcon(android.R.drawable.your_custom_drawbale)
    .show();
Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
  • inside .. case R.id.option_one... I want to use click listener with "UpdateChecker.checkForDialog(Home.this);" this line from my question . how – Tanim Sep 03 '19 at 09:33
  • @Tanim Updated my answer. You don't need to set a separate click listener for each button. onOptionsItemSelected() will handle all the click events – Nikos Hidalgo Sep 03 '19 at 10:26
1

This will Work:

In your menu.xml:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:id="@+id/rate_us"
            android:title="Rate us"/>
</menu>

In Main activity:

class MainActivity : AppCompatActivity(){


 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
 }


 override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menuInflater.inflate(R.menu.settings, menu);
            return super.onCreateOptionsMenu(menu) 
 }
 override fun onOptionsItemSelected(item: MenuItem): Boolean {
            val id = item.itemId
            when (id) {
                R.id.rate_us -> {
                      ///YOUR CODE 
                      //
                  return true 
                }
              else -> return 
                  super.onOptionsItemSelected(item) 
              } 
  }


 }
F_Z
  • 581
  • 3
  • 16
0

in your menu xml add this

<item
android:onClick="clickME">
</item>

in your activity create a void

private void clickME(MenuItem menuItem){

}
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21
0

At first you need to implement NavigationView.OnNavigationItemSelectedListener in your activity.

then, inside onCreate() method you need to initialize the navigation menu:

navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

also you need to @overwrite onNavigationItemSelected(MenuItem item) like this:

@overwrite
public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_item_1) {
            //do whatever you want
        } else if (id == R.id.nav_item_2) {
            //do whatever you want
        } else if (id == R.id.nav_item_3) {
           //do whatever you want
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
}
0
 bottomNavig.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            if (menuItem.getItemId() == R.id.wallet) {

                menuItem.setChecked(true);
            } else if (menuItem.getItemId() == R.id.home) {

                menuItem.setChecked(true);
            }
            return false;
        }
    });
Mehrdad
  • 1,477
  • 15
  • 17