Android checkable context menu not remembering checked - android

I have a button which opens a context menu with a list of various options. Only one option can be selected at a time so I want to have a radio button next to each of them highlighting which item is currently selected. When I select an item from the context menu the radio button is selected and closes. Once I click the button to open the context menu the previously selected item is not selected.
How do I get the context menu to remember which item was previously selected?
Secondly, when the activity is first created a default option is selected. How do I set an initial default which can then be overwritten when another context menu item is selected? I can set android:checked="true" in the XML but can this be overwritten when a different item is selected?
Java:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
Button buttonView = (Button) this.findViewById(R.id.button_view);
buttonView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(R.string.menu_title);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case(R.id.item1):
if(item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
break;
case(R.id.item2):
if(item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
break;
case(R.id.item3):
if(item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/context_menu" android:checkableBehavior="single">
<item android:id="#+id/item1"
android:title="#string/context_menu_item1" />
<item android:id="#+id/item2"
android:title="#string/context_menu_item2" />
<item android:id="#+id/item3"
android:title="#string/context_menu_item3" />
</group>
</menu>
Any assistance would be greatly appreciated!

The problem is that your items don't save the state between displaying of menu.
So each time you call setChecked, it works only for the currently shown menu and resets for the next.
You should save the checked state in an external structure like a boolean array for example.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(R.string.menu_title);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
// loop for menu items
for (int i = 0; i < menu.size(); ++i) {
MenuItem mi = menu.getItem(i);
// check the Id as you wish
if (mi.getItemId() == R.id.item2) {
mi.setChecked(true);
}
}
}
In order to search a menu item, you can also use the findItem function:
MenuItem mi = menu.findItem(R.id.item2)

Well, you have to save your user input somewhere and set the checked states explicitly after inflating the layout.
By the way, you could write your item selected switch like so:
switch (item.getItemID()) {
case R.id.item1:
case R.id.item2:
case R.id.item3:
item.setChecked(!item.isChecked());
}

Related

How to show the selected item in popup menu which is inside a fragment using a check box?

I am using an image button for a popup menu to popup everything is working fine but when I select an item in the menu the item is selected and it doesn't show the selection so that I could identify the selected item.The checkbox remains unchecked even after the selection
menu_icon_img=myView.findViewById(R.id.Id_customer_over_flow);
menu_icon_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getActivity() != null) {
PopupMenu popup = new PopupMenu(getActivity(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sort_menu_items, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_name_a_z:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.select_name_z_a:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return false;
}
}
});
}
}
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<group
android:checkableBehavior="single"
>
<item
android:id="#+id/select_name_a_z"
android:title="#string/name_a_z"
android:checkable="true"
/>
<item
android:id="#+id/select_name_z_a"
android:title="#string/name_z_a"
android:checkable="true"
/>
</group>
</menu>
The problem is you are creating the popup menu from onClick of an imageview. whenever a click event occures a new instance of popup menu is being created.
to avoid this initiate the popup menu in onCreate method. And call popup.show() from onClick() method.
Toolbar menus are supposed to navigate you the specified activities or fragments on click of the pop up.I am not sure what is the problem , but once you click on any MenuItem it will navigate you to given Intent associated with the the id for example,
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_name_a_z:
Intent intent=new Intent(MainActivity.this,SecondActivity.this);
startActivity(intent);
return true;
If you are using a checkable menu item then change the below code from
case R.id.select_name_z_a:
if (item.isChecked())
item.setChecked(false);
else item.setChecked(true);
return true;
Do like
if (!item.isChecked()) item.setChecked(true);
Because item.ischecked() is false in the beginning state.
Made some changes to your above code, Try this
menu_icon_img=findViewById(R.id.Id_customer_over_flow);
popup = new PopupMenu(getApplicationContext(), menu_icon_img);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sort_menu_items, popup.getMenu());
menu_icon_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (this != null) {
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Menu menu = popup.getMenu();
for(int i = 0;i<menu.size();i++){
menu.getItem(i).setChecked(false);
}
item.setChecked(true);
return true;
}
});
}
}
});
Also for all items in menu set:
android:checkable="true"

replace of menu item position by another menu item position in same menu

I am trying to create a context menu for card view.Initial menu like
fig 1: initial context menu
and i need to replace it like fig 2: replaced context menu
.When i click on disable menu the card view will be disabled,and the disable menu should replace with enable menu
call invalidateOptionsMenu() after click on menu item to change menu item title.
Boolean IsEnable = false;
#Override
public boolean onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuItem reminderstatus = (MenuItem) menu.findItem(R.id.reminderstatus);
if (IsEnable) {
reminderstatus.setTitle("Disable");
} else {
reminderstatus.setTitle("Enable");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
if (item.getItemId() == R.id.reminderstatus) {
if (IsEnable) {
IsEnable = false;
} else {
IsEnable = true;
}
invalidateOptionsMenu();
}
}
Actually you don't need to create Different Menu item for Enable and Disable , take a single menu-item , just change the text of menu item , from their Status (Enable or Disable).
hope you are saving status that currently it is Enable or Disable.
#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);
// Select a menu item then change it's title (text)
MenuItem mi = (MenuItem) menu.findItem(R.id.YOUR_MENU_ID);
if(CHECK_YOUE_CURRENT_STATUS_HERE){
//SET YOUR CURRENT STATUS ACCORDINGLY CURRENT STATUS (ENABLE /DISABLE)
mi.setTitle("Enable/Disable");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
if (item.getItemId() == R.id.reminderstatus) {
if (YOUR_CURRENT_STATUS) {
YOUR_CURRENT_STATUS = false;
} else {
YOUR_CURRENT_STATUS = true;
}
invalidateOptionsMenu();
//this method refresh your Context menu view so basically call
// your onCreateContextMenu once again which will check for your
// Status and set accordingly it.
}
}
Just check this sample code implement it correctly will work for you.

Context Floating Menu

I have created a Context Floating Menu like this:
I added a header too (it is not shown in this picture).
It works perfect but i want to change :
Background
Color/drawable between the header and the first item
The color of the header
And other settings
Can someone show me an example in the styles.xml file how to change some of those settings?
EDIT: To be more specific i will show my code:
Here i register my view for the context menu:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListItemView demoItem1 = (ListItemView) findViewById(R.id.demoItem1);
registerForContextMenu(demoItem1);
}
Here i create and inflate the menu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("List Actions");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
Here are the options if clicked:
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.dublicate:
return true;
case R.id.edit:
return true;
case R.id.delete:
return true;
case R.id.rename:
return true;
default:
return super.onContextItemSelected(item);
}
}
Here is the context_menu.xml :
<item
android:id="#+id/dublicate"
android:title="#string/context_menu_item_dublicate">
</item>
<item
android:id="#+id/edit"
android:title="#string/context_menu_item_edit"/>
<item
android:id="#+id/delete"
android:title="#string/context_menu_item_delete"/>
<item
android:id="#+id/rename"
android:title="#string/context_menu_item_rename"/>

Android - Checkable Context Menu Not Working

Here is the preparation code:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.language_menu, menu);
MenuItem menuItem = menu.findItem(R.id.arabic);
if (UtilityPreferenceManager.getSelectedLanguage() == UtilityPreferenceManager.LanguageArabic) {
menuItem.setChecked(true);
} else {
menuItem.setChecked(false);
}
menuItem = menu.findItem(R.id.english);
if (UtilityPreferenceManager.getSelectedLanguage() == UtilityPreferenceManager.LanguageEnglish) {
menuItem.setChecked(true);
} else {
menuItem.setChecked(false);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.arabic:
if (UtilityPreferenceManager.getSelectedLanguage() != UtilityPreferenceManager.LanguageArabic) UtilityPreferenceManager.changeLanguage();
return true;
case R.id.english:
if (UtilityPreferenceManager.getSelectedLanguage() != UtilityPreferenceManager.LanguageEnglish) UtilityPreferenceManager.changeLanguage();
return true;
default:
return super.onContextItemSelected(item);
}
}
and here is the menu xml:
<group android:checkableBehavior="single">
<item android:id="#+id/arabic"
android:title="#string/arabic" />
<item android:id="#+id/english"
android:title="#string/english" />
</group>
The problem is that the menu always appears with English selected. I am sure that the language preference is saved correctly in preferences. In fact, the if condition is working fine, but it seems there is something overriding the selected menu item after finishing onCreateContextMenu

Android selecting a word in either a TextView or EditView

I am trying to figure out an easy way for a user to select a word, preferably by long pressing on the word in a TextView. Basically, I have a TextView filled with text and I would like the user to have the ability to long press the word and then display a contextmenu so I can execute a database search? Is this possible? I can also switch to an EditText as long as I can make it look like a TextView. Make sense?
Thanks.
Very simple.
First create your TextView and registerForContextMenu():
private AdapterContextMenuInfo info;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.txtbtn);
text.setText("Click Me!");
registerForContextMenu(text);
}
Then build your ContextMenu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
info = (AdapterView.AdapterContextMenuInfo)menuInfo;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
String phone="555-555-555";
String toDial="tel:"+phone.toString();
Uri uri = Uri.parse(toDial);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
return true;
default:
return super.onContextItemSelected(item);
}
}
context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/call"
android:title="CALL" />
</menu>

Categories

Resources