I cant click on item inside my menu. My menu opens and everything is displayed normally, but nothing happens when I click. I don't know why and searched for information but it doesn't help me. This my XML code profile__detail_menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:icon="@drawable/ic_baseline_more_vert_24"
android:title="">
<menu>
<item
android:id="@ id/change_name"
android:title="Change name"
android:icon="@drawable/ic_baseline_edit_24"
/>
<item
android:id="@ id/new_photo"
android:title="New photo"
android:icon="@drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="@ id/log_out"
android:title="Log out"
android:icon="@drawable/ic_baseline_login_purp_24"
/>
</menu>
</item>
</menu>
This my Java code.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.profile__detail_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
Toast.makeText(Profile.this,"Something", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.change_name:
Toast.makeText(this, "change_name selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.new_photo:
Toast.makeText(this, "new_photo selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.log_out:
Toast.makeText(this, "log_out selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
CodePudding user response:
The thing which I feel is wrong is your menu XML file. For some reason you have a menu tag inside a menu tag. So there is sort of a sub menu if you get my point? But you're not inflating that actually.
Modify the XML as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YourActivityName"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@ id/change_name"
android:title="Change name"
app:showAsAction="always|withText"
android:icon="@drawable/ic_baseline_edit_24"
/>
<item
android:id="@ id/new_photo"
android:title="New photo"
app:showAsAction="always|withText"
android:icon="@drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="@ id/log_out"
android:title="Log out"
app:showAsAction="always|withText"
android:icon="@drawable/ic_baseline_login_purp_24"
/>
</menu>
I have updated your menu as per you code. If you need, add more items to it.