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;
Related
I'm new to coding, now creating an android app which has some videos under video tab/list, and my question is how can I add a Delete(with a notification), Share, Cancel button when I long press a specific video which I want to delete (from internal storage)? Guide me with examples. In totally new into this :)
minSdkVersion="11"
targetSdkVersion="25"
Searched in but didn't find any appropriate answer with examples, though ..
Eager to study :(
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.deletevideo:
// add stuff here
return true;
case R.id.sharevideo:
// edit stuff here
return true;
case R.id.cancelvideo:
// remove stuff here
return true;K
default:
return super.onContextItemSelected(item);
}
}
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item
android:id="#+id/deletevideo"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/video_delete"/>
<item
android:id="#+id/sharevideo"
android:icon="#android:drawable/ic_menu_edit"
android:title="#string/video_share"/>
<item
android:id="#+id/cancelvideo"
android:title="#string/video_Cancel"/>
</menu>
All you have to do is add contextListener:
Java File
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
return true;
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.add:
// add stuff here
return true;
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
// remove stuff here
return true;
default:
return super.onContextItemSelected(item);
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_delete" />
<item android:id="#+id/edit"
android:icon="#android:drawable/ic_menu_edit"
android:title="#string/menu_share" />
<item android:id="#+id/delete"
android:icon="#android:drawable/my_icon_delete"
android:title="#string/menu_cancle" />
</menu>
I've tried making a option menu in my activity, and I succeed. The problem now, is that the text style is bold, and I don't know how to make it normal. I tried setting textStyle, but it had no effect.
option_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/AppointmentItem"
app:showAsAction="always"
android:icon="#drawable/ic_notifications_white_24dp"
android:title="Appointments"/>
<item android:id="#+id/WeekItem"
app:showAsAction="never"
android:icon="#drawable/ic_today_24dp"
android:title="Week vooruit/achteruit"/>
<item android:id="#+id/LeerlingItem"
app:showAsAction="never"
android:icon="#drawable/ic_person_24dp"
android:title="Leerling veranderen"/>
<item android:id="#+id/InstelllingenItem"
app:showAsAction="never"
android:icon="#drawable/ic_settings_24dp"
android:title="Instellingen"/>
<item android:id="#+id/OverItem"
app:showAsAction="never"
android:icon="#drawable/ic_work_white_24dp"
android:title="Over ons"/>
</menu>
How I create the options menu in my mainactivity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu); //your file name
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.AppointmentItem:
return true;
case R.id.WeekItem:
getWeek();
return true;
case R.id.LeerlingItem:
getLeerling();
return true;
case R.id.InstelllingenItem:
getSettings();
return true;
case R.id.OverItem:
getAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
How it looks (first) and how I want it (last):
Fixed it by changing "TextAppearance.AppCompat.Widget.ActionBar.Title" to "TextAppearance.AppCompat.Widget.ActionBar.Subtitle".
<style name="MyActionBar.MenuTextStyle" parent="style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
<item name="android:textColor">#color/black</item>
<item name="android:textStyle">normal</item>
<item name="android:textSize">16dp</item>
</style>
Thanks to #Rotwang for advising me to check the styles.xml
I have this in my menu_check.xml But when I click on the check nothing happens so the Toast never show... what's the problem? Thanks a lot
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_selecciontodo"
android:title="#string/check"
android:checkable="true"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="always" />
</menu>
and this in my java class
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_check, menu);
checkBox = (CheckBox) menu.findItem(R.id.action_selecciontodo).getActionView();
checkBox.setText("Select all");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_selecciontodo:
CheckBox checkBox= (CheckBox) item.getActionView();
if (checkBox.isChecked())
Toast.makeText(getApplicationContext(), "You selected all", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
No need to app:actionViewClass="android.widget.CheckBox" just do like this
in oncreate options menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
and in onOptionsItemSelected write this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_bookmark:
if (item.isChecked()) {
item.setChecked(false);
Toast.makeText(MainActivity.this, "Un Checked", Toast.LENGTH_SHORT).show();
} else {
item.setChecked(true);
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
}
break;
}
return super.onOptionsItemSelected(item);
}
and in your menu.xml in menu folder declare menu like this
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/menu_bookmark"
android:checkable="true"
android:title="Bookmark"/>
so when you check uncheck the menu item it show toast and show checkbox checked or unchecked
Try this,
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="com.example.stackoverflow.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_check"
android:title="YOUR_TITLE"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
</menu>
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.menu_main, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.action_check);
item.setChecked(isChecked);
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_settings) {
return true;
}
if (id == R.id.action_check) {
item.setChecked(!item.isChecked());
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
Log.i("cheeck status" , "" + item.isChecked());
return true;
}
return super.onOptionsItemSelected(item);
}
this should work.
Happy coding.!!!
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;
I followed the instructions and examples on Android website to create a context menu but mine shows up completely black and i cannot change any options in it ; anyone out there had the same experience and can help me solve this issue.
FWIW, here are my class .java and the menu .xml files
<?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/red"
android:title="#string/red"
android:checked="true" />
<item android:id="#+id/blue"
android:title="#string/blue" />
<item android:id="#+id/green"
android:title="#string/green" />
<item android:id="#+id/yellow"
android:title="#string/yellow" />
<item android:id="#+id/black"
android:title="#string/black" />
<item android:id="#+id/white"
android:title="#string/white" />
<item android:id="#+id/orange"
android:title="#string/orange" />
</group>
</menu>
package com.MyProject;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
public class ColorsActivity extends Activity {
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
registerForContextMenu(v);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.red:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.blue:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.green:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.yellow:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.black:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.white:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.orange:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return super.onContextItemSelected(item);
}
}
}
Activity.registerForContextMenu (View) registers the context menu with the OS so that when the menu button is pressed and the given view is in the foreground, the callback to onCreateContextMenu is made. What you have done is register the view within the callback, making it fundamentally unreachable in your code because the view would have to be already registered in order to reach the registration you have here. registerForContextMenu should be called in one of your lifecycle startup methods, probably onResume.