Android - Checkable Context Menu Not Working - android

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

Related

How I can get a weblink in textview - Android

I tired some codes but I couldn't fix it. My manu.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=".MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_feedback"
app:showAsAction="never" />
How I can get a weblink in this xml? Thanks a lot.
Example for a ContextMenu
First override onCreateContextMenu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(/*your menu id*/, menu);
}
Then override the onContextItemSelected an open a webview or the play store app:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()){
case action_settings: {
//your code
return true;
}
default:
return super.onContextItemSelected(item);
}
}

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"/>

Error in using menu item to switch between activities in Android

I am trying to use a menu item to switch between activities in my application. Unfortunately, when I tap on the menu item, it does nothing.
Here is the activity.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.main, menu);
return true;
}
// Called when the user selects a contextual menu item
#Override
public boolean onContextItemSelected(MenuItem item) {
//Handles Item Selection.
switch (item.getItemId()) {
case R.id.action_switch_natural:
Intent a = new Intent(this, Natural_Display.class);
startActivity(a);
return true;
default:
return super.onContextItemSelected(item);
}
}
Here is the code for main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_switch_natural"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_switch_natural"/>
</menu>
What am I doing wrong?
onContextItemSelected(MenuItem item) should be onOptionsItemSelected(MenuItem item), as seen here. Don't forget to change super.onContextItemSelected(item) to super.onOptionsItemSelected(item).
I think you have to override onCreatecontextMenu before onContextItemSelected.
try this.
#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);
}

Android: ContextMenu and ItemSelected in Context Menu

This is the onCreate and oncontextitemslected code
#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);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
Toast toast;
if(item.getItemId() == R.id.context_menu_edit)
{
Log.d("ContextCheck","EDIT!");
toast = Toast.makeText(this, "Edit!", Toast.LENGTH_SHORT);
toast.show();
}
if(item.getItemId() == R.id.context_menu_delete)
{
Log.d("ContextCheck","DELETE!");
toast = Toast.makeText(this, "Delete!", Toast.LENGTH_SHORT);
toast.show();
}
return super.onContextItemSelected(item);
}
and before that is i used the method registerForContextMenu(event_list) where event_list is a ListView , no i don't know why when ever i click an item from the context menu, it doesn't do anything, it won't show the toast and won't log into the logcat... is the item.getItemId() same for OptionsMenu and ContextManu?.. i don't know what is wrong with my code..
PS
the context menu is called inside a dialog box in a listview
Here is your solution, if you don't mind creating the menu items in your class. The keyword was definitely your PS, meaning your listview is in a dialog.
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//MenuInflater inflater = getMenuInflater();
//inflater.inflate(R.menu.context_menu, menu);
MenuItem delete = menu.add("delete");
MenuItem add = menu.add("add");
add.setIcon(android.R.drawable.ic_menu_upload); //adding icons
delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.d("ContextCheck","EDIT!");
Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
return true;
}
});
add.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.d("ContextCheck","EDIT!");
Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
return true;
}
});
}
You do not even need the onContextItemSelected method.
You need to return true in the onCreateOptionsMenu as detailed in the documentation:
Returns
You must return true for the menu to be displayed; if you
return false it will not be shown.
So you can chacnge your code to this:
#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);
return true;
}
UPDATE:
I have things return on the options menu with a switch case in a onOptionsItemSelected vs onContextItemSelected
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.emaildev:
email();
break;
case R.id.share:
Share();
break;
}
return true;
}
The icons and ids are in my menu.xml

Android checkable context menu not remembering checked

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());
}

Categories

Resources