How to set On Click Listener to menu item in android studio
You don't have to setOnClickListener for each and every menu items individually.
public boolean onOptionsItemSelected(MenuItem item)
method is handling all the clicks for a menu and using a switch
or if
condition you can find out which menu item is clicked. So all you have to do is add onClick the functionality for each item.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.navShare:
Intent shareIntent = new Intent (Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareBody = "your body here";
String shareSub = "Your subject here";
shareIntent.putExtra(Intent.EXTRA_SUBJECT, shareSub);
shareIntent.putExtra (Intent.EXTRA_TEXT, shareBody);
startActivity (Intent.createChooser (shareIntent,"Share App Locker"));
return true;
case R.id.otherItem:
// Some other methods
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Handling popup menu items click
Before showing the PopupMenu
add a listener for PopupMenu
for handling the click events.
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(),
item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
How to add click event to item on NavigationView of Android
Implement the listener in your Activity:
public class HomeActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListenersetNavigationItemSelectedListener in onCreate of Activity
NavigationView mNavigationView = (NavigationView) findViewById(R.id.account_navigation_view);
if (mNavigationView != null) {
mNavigationView.setNavigationItemSelectedListener(this);
}Override the method
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_account) {
// DO your stuff
}
}
android: menu item click event from fragment
If you want to capture the click on your item, implement
public boolean onOptionsItemSelected(MenuItem item)
And then:
If your activity includes fragments, the system first calls
onOptionsItemSelected() for the activity then for each fragment (in
the order each fragment was added) until one returns true or all
fragments have been called.
You can follow the oficial reference:
http://developer.android.com/guide/topics/ui/menus.html
Related Topics
How to Create a Resizable Rectangle with User Touch Events on Android
Import .CSV File to SQLite in Android
How to Scale/Resize Text to Fit a Textview
Android Action Bar Not Showing Overflow
Android: Mediaplayer Setvolume Function
Move Markers in Google Map V2 Android
How to Set Font Custom Font to Spinner Text Programmatically
How to Programmatically Enable and Disable Flight Mode on Android 4.2
Programmatic Views How to Set Unique Id'S
How to Add Footer to Navigationview - Android Support Design Library
Creating a Directory in /Sdcard Fails
Disable the Notification Panel from Being Pulled Down
Android Pendingintent Extras, Not Received by Broadcastreceiver
How to Use Adb to Send Touch Events to Device Using Sendevent Command
What Is the Class R in Android