I have a menu_item and here is the code:
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.TextView"
android:title="In Stocks"
app:showAsAction="always" />
<item
android:id="#+id/action_checkbox"
app:actionViewClass="android.widget.CheckBox"
android:title="#string/action_check"
app:showAsAction="always"
/>
I am trying to catch Menu_item_clicked_event So, I write following code in Activity file
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_checkbox:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.action_search:
System.out.println("xas");
default:
return super.onOptionsItemSelected(item);
}
}
When I am pressing the action_search it's working but not for action_checkbox. Why this is happening? I am getting No exception in ADB logs. Is this is the right way to do check for Checkboxes?
app:actionViewClass should be android:actionViewClass="android.widget.CheckBox"
<item
android:id="#+id/action_checkbox"
android:actionViewClass="android.widget.CheckBox"
android:icon="#drawable/unchecked"
android:checkable="true"
android:checked="false"
android:title="#string/action_check"
app:showAsAction="always"
/>
However checkbox will not be visble. Referthis
you could have your own icon and set it android:icon=
And in class
case R.id.action_checkbox:
if(item.isChecked()){
item.setIcon(R.drawable.unchecked);
}
else {
item.setIcon(R.drawable.checked);
}
item.setChecked(!item.isChecked());
break;
Related
fist off all the code:
Following lines are in my MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
View view = findViewById(R.id.action_item_two);
//noinspection SimplifiableIfStatement
if (id == R.id.action_item_one) {
// Do thing one
return true;
}
if (id == R.id.action_item_two) {
// Do thing two
return true;
}
return super.onOptionsItemSelected(item);
}
My menu has the file res/menu/main.xml and looks like this:
<?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/action_item_one"
android:icon="#drawable/ic_help"
app:showAsAction="always"
android:title="Hilfe">
</item>
<item
android:id="#+id/action_item_two"
android:icon="#drawable/ic_forward_30_48px"
app:showAsAction="always"
android:title="Sekunden">
</item>
</menu>
In an old version of the main menu I used drawable/ic_baseline_pause_24 as icon-source to test the menu and it worked.
After this I changed the icon-sources to the final ones, but the icons where still the old one.
I deleted the old icon source (drawable/ic_baseline_pause_24 and from now on I have the following error when trying to start the app:
[datafile to the app]\app\src\main\res\menu\main.xml:4: AAPT: error: resource drawable/ic_baseline_pause_24 (aka [...]:drawable/ic_baseline_pause_24) not found.
In line 4 of old menu version stood the old icon-source but now it stands nowhere and I have no idea how to solve this error.
I tried to restart everything including (invalidate Caches and restarting Android Studio)
I hope somebody can help!
Thanks
Ok, I found the solution to my problem...
It was i syntax-error in main.xml
It has to be:
<item
android:id="#+id/action_item_one"
android:icon="#drawable/ic_help"
app:showAsAction="always"
android:title="Hilfe"/>
<item
android:id="#+id/action_item_two"
android:icon="#drawable/ic_forward_30_48px"
app:showAsAction="always"
android:title="Sekunden">
</item>
Cause this error the menu did not update
I click an Icon on the action bar to display a popup menu, then I click a popup menu item and a toast message in my Java code shows that it changes state from false to true. The problem is that when the popup menu is opened again no items are clicked. And clicking a popup menu item always shows that it changes state from false to true.
Does anyone have a code solution for this problem?
Thank you for your help.
Java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_action, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_popup) {
View menuItemView = findViewById(R.id.action_popup);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.opt1_name) {
if (item.isChecked()){
item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{
item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}
}
else if (id == R.id.opt2_date) {
if (item.isChecked()){item.setChecked(false);
Toast.makeText(getApplicationContext(), "Logic Set False= " + item, Toast.LENGTH_SHORT).show();
}
else{item.setChecked(true);
Toast.makeText(getApplicationContext(), "Logic Set True= " + item, Toast.LENGTH_SHORT).show();
}
}
return true;
}
});
popupMenu.show();
}
return super.onOptionsItemSelected(item);
}
menu_action.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".PlayerActivity">
<item android:id="#+id/action_popup" android:title="Sort Popup" android:icon="#drawable/ic_sort"
android:showAsAction="always" />
<item android:id="#+id/action_settings" android:title="Settings"
app:showAsAction="never" />
<item android:id="#+id/action_help" android:title="Help"
app:showAsAction="never" />
</menu>
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/opt1_name"
android:title="Name" />
<item
android:id="#+id/opt2_date"
android:title="Date" />
</group>
</menu>
Check the error if you examine then you must have one listview in your mainlist.xml file with id as #android:id/list
<ListView
android:id="#android:id/list"
android:layout_height="wrap_content"
android:layout_height="fill_parent"/>
I have made an option menu in my application and for some reason it works on one of my smartphones and not the other. The smartphone the option menu works on is android lollipop 5.0 the smartphone it doesn't work on is jellybean 4.1. I'm not sure if this has anything to do with it but I need the option menu to work on both phones. ANyone any ideas on why this is happening
My Code is as fallows
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_Menu) {
return true;
}
return super.onOptionsItemSelected(item);
}
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="saveourcar.soc.MainActivity">
<item
android:id="#+id/action_Menu"
android:orderInCategory="100"
android:title="Menu"
app:showAsAction="never" >
<menu>
<item
android:id="#+id/instructions"
android:title="Instructions"
android:icon="#drawable/rench"/>
<item
android:id="#+id/hotels"
android:title="Hotels"
android:icon="#drawable/hotel"/>
</menu>
</item>
</menu>
Try this:
In onCreateView method:
setHasOptionsMenuEnabled(true);
or this on menu layout:
android:showAsAction="always"
I have a search feature for recipes and I want to include search filters for different allergies or diets. I have included this as an option on the action bar on the page. However, whenever I click on a checkbox, the menu disappears and I have to go click on the orange icon again to popup the menu and then click on another option. Is there a way where the menu can stay open so I can check multiple filters without the menu closing each time?
This is an picture of my filter section.
checkable_test.xml
<item android:id="#+id/action"
android:title="#string/action_example"
android:icon="#drawable/filter"
app:showAsAction="withText|ifRoom">
<menu>
<item android:title="Allergies">
<menu>
<group android:id="#+id/allergies"
android:checkableBehavior="all">
<item android:id="#+id/no_peanut"
android:icon="#drawable/no_peanut"
android:title="No Peanuts"
android:checkable="true"/>
<item android:id="#+id/no_sesame"
android:icon="#drawable/no_sesame"
android:title="No Sesame"
android:checkable="true"/>
<item android:id="#+id/no_dairy"
android:title="No Dairy"
android:icon="#drawable/no_milk"
android:checkable="true"/>
<item android:id="#+id/gluten_free"
android:icon="#drawable/gluten_free"
android:title="No Gluten"
android:checkable="true"/>
<item android:id="#+id/no_egg"
android:icon="#drawable/no_eggs"
android:title="No Egg"
android:checkable="true"/>
<item android:id="#+id/no_soy"
android:icon="#drawable/no_soy"
android:title="No Soy"
android:checkable="true"/>
<item android:id="#+id/no_seafood"
android:icon="#drawable/no_crust"
android:title="No Seafood"
android:checkable="true"/>
</group>
</menu>
</item>
Search.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.checkable_test, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.no_peanut:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_dairy:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_egg:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_seafood:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_sesame:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_soy:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.gluten_free:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Any solutions please?
You can try changing all of the
return true;
into
return false;
Currently in my app there is a dropdown menu containing one option Settings using this icon in the upper right corner I want to swap it out with this icon and instead of causing a drop down just calling a new view on tap.
This is how i currently handle the settings menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Intent intent = new Intent(this, appSets.class);
startActivity(intent);
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
You just set android:showAsAction="always" in your res/menu/filename.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/action_settings"
android:showAsAction="always"
andoid:title="#string/action_settings"
android:icon="#drawable/ic_action_settings"/>
</menu>
and if you are using appcompatv7 library you just need to edit it a little bit
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
app:showAsAction="always"
andoid:title="#string/action_settings"
android:icon="#drawable/ic_action_settings"/>
</menu>
go to res/menu/main.xml and in item <item /> instead android:icon="#drawable/new_pic" use your drawable image