I have read the instructions at the android developers page's in order to get the Checkable menu items:
http://developer.android.com/guide/topics/ui/menus.html
this is my xmlmenu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="all">
<item android:id="#+id/regu"
android:title="#string/Regulatory" />
<item android:id="#+id/warn"
android:title="#string/Warning" />
<item android:id="#+id/temp"
android:title="#string/Temporary" />
<item android:id="#+id/bicy"
android:title="#string/Bicycle" />
</group>
</menu>
And here is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.regu:
if (item.isChecked())
{
item.setChecked(false);
currAvailableOptions++;
}
else if(0 != currAvailableOptions)
{
item.setChecked(true);
currAvailableOptions--;
}
return true;
case R.id.warn:
if (item.isChecked())
{
item.setChecked(false);
currAvailableOptions++;
}
else if(0 != currAvailableOptions)
{
item.setChecked(true);
currAvailableOptions--;
}
return true;
case R.id.temp:
if (item.isChecked())
{
item.setChecked(false);
currAvailableOptions++;
}
else if(0 != currAvailableOptions)
{
item.setChecked(true);
currAvailableOptions--;
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_menu, menu);
return true;
}
The problem is when I clicked one item, the menu item disappeared. It wouldn't have to stay visible in order to check other menu items?
Any idea?
Greetings
Checkable items appear only in submenus or context menus.
And with submenu they (Google) means:
Submenu A floating list of menu items that appears when the user
touches a menu item that contains a nested menu.
Since your menu items are not submenu items, it will not work
I know this is not a direct answer to your question but please consider the following code instead of your switch, it might help you find the problem.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.regu:
case R.id.warn:
case R.id.temp:
if (item.isChecked())
currAvailableOptions++;
else if(currAvailableOptions != 0)
currAvailableOptions--;
item.setChecked(!item.isChecked());
return true;
default:
return super.onOptionsItemSelected(item);
}
}
What is currAvailableOptions? I looked at the article you linked to and there wasn't anything about that in there. It would seem that all you need to do is check:
if (item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
or at least that's what the tutorial says. Perhaps you should give it another read? Hope this helps.
You should probably add break; statements after each case:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
item.setChecked(!item.isChecked());
break;
case R.id.item2:
item.setChecked(!item.isChecked());
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Related
i have a note taking activity where i would like to be able to edit and save (basically overwrite) the same note.
Once i click the edit MenuItem, I would like it to hide and then show the save MenuItem.
I can get the edit MenuItem to be invisible but i cant get the save MenuItem to show. i keep getting a null pointer exception.
here's my edit_question_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/edit_question"
android:icon="#drawable/ic_edit_black_24dp"
android:title="Edit"
app:showAsAction="ifRoom"
android:visible="true">
</item>
<item
android:id="#+id/save_question"
android:icon="#drawable/ic_save_black_24dp"
android:title="Edit"
app:showAsAction="ifRoom"
android:visible="false">
</item>
</menu>
activity.java file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.edit_question_menu, menu);
MenuItem itemSave = menu.findItem(R.id.save_question);
itemSave.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_question:
saveQuestion();
return true;
case R.id.edit_question:
item.setVisible(false);
// MenuItem save_Question_MenuItem = findViewById(R.id.save_question);
// save_Question_MenuItem.setVisible(true);
enableEditMode();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void enableEditMode(){
MenuItem saveButton = findViewById(R.id.save_question);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_black_24dp);
questionEditText = findViewById(R.id.questionEditTextID);
mPostAnswerButton.setEnabled(false);
mPostAnswerButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
mCommentButton.setEnabled(false);
mCommentButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
saveButton.setVisible(true); **ERROR HAPPENS HERE**
}
Any and all help is appreciated. This seems fairly simple but I cant find a way to get it to work.
Add flag to your activity that indicates where the save MenuItem should be visible or not:
private boolean mShowSaveIcon
Override onPrepareOptionsMenu (UPDATE):
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.save_question);
item.setVisible(mShowSaveIcon);
menu.findItem(R.id.edit_question).setVisible(!mShowSaveIcon); // you can use negation of the same flag if one and only one of two menu items is visible; or create more complex logic
return true;
}
In the click handler, change flag value and request invalidation:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_question:
mShowSaveIcon = false;
break;
case R.id.edit_question:
item.setVisible(false);
enableEditMode();
mShowSaveIcon = true;
break;
}
invalidateOptionsMenu();
return true;
}
Change handler:
private void enableEditMode(){
/// MenuItem saveButton = findViewById(R.id.save_question); // <--- NO THIS LINE
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_black_24dp);
questionEditText = findViewById(R.id.questionEditTextID);
mPostAnswerButton.setEnabled(false);
mPostAnswerButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
mCommentButton.setEnabled(false);
mCommentButton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
/// saveButton.setVisible(true); **ERROR HAPPENS HERE** // <--- NO THIS LINE
}
When I'm opening the overflow menu, at first I see just an empty layout, and only after that the menu with text appears:
The same thing happens when the overflow menu is closing:
Creating this menu seems to be standard:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_cp:
return true;
case R.id.action_settings:
return true;
case R.id.action_used_libraries:
return true;
case R.id.action_help_and_feedback:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
why does it happen and how to get rid of this lag?
Try removing <item name="android:background">#color/yourgreencolor</item> from both your ThemeOverlay.AppTheme.ActionBar and ThemeOverlay.AppTheme.PopupMenu style, it's rendering a background first. I used parent="ThemeOverlay.AppCompat.Light" to have my desired white background, maybe that could help you find out how to have a green background despite this issue. It's definitively possible to have custom colors without this, I've seen some.
I have this simple PopupMenu, but when I click on an item, the item doesn't get checked. Why?
In documentation is:
Menu items in the Icon Menu (from the options menu) cannot display a checkbox or radio button.
Radio button is showing, but only state is not changing...
Java
public void showSortPopup() {
View menuItemView = findViewById(R.id.action_sort);
PopupMenu popup = new PopupMenu(this, menuItemView);
popup.inflate(R.menu.sort);
popup.getMenu().findItem(R.id.sort_def).setChecked(true);
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.sort_def:
sortOrder = CardCursorContract.CardCursor.DEFAULT_SORT;
mCardsFragment.setSortOrder(sortOrder);
savePref();
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
break;
case R.id.sort_asc:
sortOrder = CardCursorContract.CardCursor.ALPHABETICAL_ASC_SORT;
mCardsFragment.setSortOrder(sortOrder);
savePref();
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
break;
case R.id.sort_desc:
sortOrder = CardCursorContract.CardCursor.ALPHABETICAL_DESC_SORT;
mCardsFragment.setSortOrder(sortOrder);
savePref();
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
break;
default:
break;
}
return false;
}
});
popup.show();
}
XML-File
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="#+id/sort_def"
android:title="#string/action_sort_def"
android:orderInCategory="1"
android:showAsAction="always" />
<item android:id="#+id/sort_asc"
android:title="#string/action_sort_name"
android:orderInCategory="2"
android:showAsAction="always" />
<item
android:id="#+id/sort_desc"
android:title="#string/action_sort_name_desc"
android:orderInCategory="3"
android:showAsAction="always"/>
</group>
Screen
First of all you can simplify the if checked else statement to
item.setChecked(!item.isChecked())
That way it will always toggle it from true → false and the other way around.
But the problem lies in the fact that you have radiobuttons, what the statement above does is makes the group checked, but what you want is to have the item checked.
To get the behaviour youre looking for you can use item.getSubmMenu() and then use the setChecked method on the particular subMenuItem you want.
For example:
//This will refer to the default, ascending or descending item.
MenuItem subMenuItem = item.getSubMenu().getItem(INDEX_OF_ITEM);
//Check or uncheck it.
subMenuItem.setChecked(!subMenuItem.isChecked());
**Swap These Lines in every case **
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
To
if (item.isChecked()) item.setChecked(true);
else item.setChecked(false);
Hi Below is the code I am using to create option menu in my FragmentActivity :-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Menu options to set and cancel the alarm.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm(this);
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.cancel_action:
alarm.cancelAlarm(this);
return true;
}
return false;
}
Will anybody tell me why it's not working? It is not affecting app but nothing is happening when I click the option menu button from device. Please Help to resolve this.
Thanks in advance!
Below is my main.xml :-
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/start_action"
android:showAsAction="ifRoom|withText"
android:title="#string/start_text" />
<item android:id="#+id/cancel_action"
android:showAsAction="ifRoom|withText"
android:title="#string/cancel_text" />
</menu>
Return item within Switch case like. ITs Work For me.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm(this);
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.cancel_action:
alarm.cancelAlarm(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Although this question is old but to close it here is what i believe OP is missing in the code
In the
onCreateOptionsMenu
return it with the super as super.onCreateOptionsMenu(menu);
and in the
onOptionsItemSelected
return it with the super as super.onOptionsItemSelected(item);
all the return type is boolean so you will know it worked correctly when it returns true. Its like simillar to super.onCreate(savedInstancestate).
Change
return false;
to
return super.onOptionsItemSelected(item);
as
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm(this);
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.cancel_action:
alarm.cancelAlarm(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Edit:
Also you have to add the following to your Fragment
setHasOptionsMenu(true);
I want to add a new button to the ActionBar. When I click it, it does a specific Action. So I don't want a button that, after being pressed, opens a sub-menu (like the classic 3-dot menu).
I created a new button, this:
<item android:id="#+id/action_refresh"
android:icon="#drawable/refresh"
android:title="#string/refresh_string"
android:showAsAction="always"/>
and it's shown on the ActionBar, but if I click it, naturally, it doesn't do anything.
How can I manage to get an Action just pressing it?
Thanks!
You Need to override onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_refresh:
//do your stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Same as Homo sapiens's answer, but with an if-structure.
Add this method to your Activity class:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.action_refresh)
{
// do your stuff
return true;
}
else if (item.getItemId() == R.id.otherItem)
{
// do other stuff
return true;
}
// ...
else
{
return super.onOptionsItemSelected(item);
}
}