I'm trying to create a menu with filters on a map, the idea is to select multiple elements (like the screenshoot). But the problem is that every time I press an option, the menu is automatically hidden.
So I can only tap one checkbox everytime I open the menu. This behaviour is not the best because user has to open N times the menu to select N filters.
Any suggestion or advice about how to solve this? Thanks!
And this is my code (reduced to be more readable):
map_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_filtro"
app:showAsAction="always" >
<menu>
<group android:checkableBehavior="all">
<item
android:id="#+id/nav_ciencia"
android:icon="#drawable/ciencia"
android:title="#string/filtro_ciencia"
app:showAsAction="never"
/>
<item
android:id="#+id/nav_comercio"
android:icon="#drawable/comercio"
android:title="#string/filtro_comercio"
app:showAsAction="never"
/>
<item
android:id="#+id/nav_cultura"
android:icon="#drawable/cultura"
android:title="#string/filtro_cultura"
app:showAsAction="never"
/>
<item
android:id="#+id/nav_deporte"
android:icon="#drawable/deporte"
android:title="#string/filtro_deportes"
app:showAsAction="never"
/>
</group>
</menu>
</item>
</menu>
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map_drawer, menu);
SharedPreferences prefs = getSharedPreferences(Constants.pref_name, Context.MODE_PRIVATE);
MenuItem item_ciencia = menu.findItem(R.id.nav_ciencia);
item_ciencia.setChecked(prefs.getBoolean(Constants.pref_nav_ciencia, false));
MenuItem item_comercio = menu.findItem(R.id.nav_comercio);
item_comercio.setChecked(prefs.getBoolean(Constants.pref_nav_comercio, false));
MenuItem item_cultura = menu.findItem(R.id.nav_cultura);
item_cultura.setChecked(prefs.getBoolean(Constants.pref_nav_cultura, false));
MenuItem item_deporte = menu.findItem(R.id.nav_deporte);
item_deporte.setChecked(prefs.getBoolean(Constants.pref_nav_deporte, false));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
SharedPreferences prefs = getSharedPreferences(Constants.pref_name, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
if (id == R.id.action_settings) {
return true;
}else if (id == R.id.nav_ciencia) {
item.setChecked(!item.isChecked());
editor.putBoolean(Constants.pref_nav_ciencia, item.isChecked());
editor.commit();
return true;
} else if (id == R.id.nav_comercio) {
item.setChecked(!item.isChecked());
editor.putBoolean(Constants.pref_nav_comercio, item.isChecked());
editor.commit();
return true;
} else if (id == R.id.nav_cultura) {
item.setChecked(!item.isChecked());
editor.putBoolean(Constants.pref_nav_cultura, item.isChecked());
editor.commit();
return true;
} else if (id == R.id.nav_deporte) {
item.setChecked(!item.isChecked());
editor.putBoolean(Constants.pref_nav_deporte, item.isChecked());
editor.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
Related
I have options menu in my toolbar with radibutton item :
<item
android:id="#+id/map_menu"
android:icon="#drawable/ic_layer"
android:orderInCategory="102"
app:showAsAction="always"
android:title="#string/action_settings">
<menu>
<group
android:id="#+id/map_types_group"
android:checkableBehavior="single" >
<item
android:id="#+id/map_terrain"
android:orderInCategory="1"
app:showAsAction="ifRoom"
android:title="#string/map_terrain"/>
<item
android:id="#+id/map_normal"
android:orderInCategory="2"
android:checked="true"
app:showAsAction="ifRoom"
android:title="#string/map_normal"/>
<item
android:id="#+id/map_hybrid"
android:orderInCategory="3"
app:showAsAction="ifRoom"
android:title="#string/map_hybrid"/>
</group>
</menu>
</item>
I want to restore selected radiobutton when orientation change happened in onSaveInstanceState,onRestoreInstanceState but i can't understand how to get selected button from radiogroup in options menu.
Here is a fully working and tested example. With this code in place, no matter how many times you rotate the screen, the currently selected item will persist.
First, create these instance variables to keep track of the state of the menu and have a name for the preference you will be saving in the Bundle:
private final static String MENU_SELECTED = "selected";
private int selected = -1;
MenuItem menuItem;
The saveInstanceState() method should save off the currently selected value:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt(MENU_SELECTED, selected);
super.onSaveInstanceState(savedInstanceState);
}
Update the currently selected item in onOptionsItemSelected():
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Log.d("settings", "id: " + id);
return true;
}
if (id == R.id.map_terrain){
Log.d("menuitem", "terrain id: " + id);
selected = id;
item.setChecked(true);
return true;
}
if (id == R.id.map_normal){
Log.d("menuitem", "normal id: " + id);
selected = id;
item.setChecked(true);
return true;
}
if (id == R.id.map_hybrid){
Log.d("menuitem", "hybrid id: " + id);
selected = id;
item.setChecked(true);
return true;
}
return super.onOptionsItemSelected(item);
}
In onCreate(), load the saved data if it exists:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null){
selected = savedInstanceState.getInt(MENU_SELECTED);
}
}
And then re-select the previously selected item in onCreateOptionsMenu():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if (selected == -1){
return true;
}
switch (selected){
case R.id.map_terrain:
menuItem = (MenuItem) menu.findItem(R.id.map_terrain);
menuItem.setChecked(true);
break;
case R.id.map_normal:
menuItem = (MenuItem) menu.findItem(R.id.map_normal);
menuItem.setChecked(true);
break;
case R.id.map_hybrid:
menuItem = (MenuItem) menu.findItem(R.id.map_hybrid);
menuItem.setChecked(true);
break;
}
return true;
}
MenuItem in onOptionsItemSelected has item.isChecked() method, just use it. You can store one boolean field(it wouldn't be a bad thing in my opinion) and change it whenever change in radiog group occurs
Then you can have your id by simply calling:
if(item.isChecked()) {
your_id_field = item.getItemId()
}
Create a variable eg: menu_selection to store the id of the menu item selected. Initially you can define 0
private int menu_selection = 0;
Save and restore the value of the variable using onSaveInstanceState and onRestoreInstanceState
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the id of radio button selected in the menu
savedInstanceState.putInt("selection", menu_selection);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
menu_selection = savedInstanceState.getInt("selection");
}
In onOptionsItemSelected , assign the id of the menu item selected item.getItemId() to the variable
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if (item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
menu_selection = item.getItemId();
}
switch (item.getItemId()) {
case R.id.map_terrain:
//some action here
return true;
case R.id.map_normal:
//some action here
return true;
case R.id.map_hybrid:
//some action here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In onCreateOptionsMenu , find the menu item identified with the value in the variable and check it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
if (menu_selection != 0) {
MenuItem selected = (MenuItem) menu.findItem(menu_selection);
selected.setChecked(true);
}
return true;
}
How can I set initial value of checkbox item part of a menu? When I start an Activity I want to set a boolean value saved in Shared Preferences.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginActivity">
<item
android:id="#+id/checkbox"
android:title="#string/checkbox"
android:orderInCategory="10"
android:showAsAction="never"
android:visible="true"
android:checkable="true"
android:checked="true"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.checkbox){
if(item.isChecked()){
item.setChecked(false);
}else{
item.setChecked(true);
}
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PrefsConsts.CHECKBOX,item.isChecked());
editor.commit();
}
return true;
}
I can always know what was the value, but I donĀ“t know how to set the new one.
First of all, get rid of the checked value in the layout
<item
android:id="#+id/action_check"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
Now you can set it in the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
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) {
int id = item.getItemId();
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();
return true;
}
return super.onOptionsItemSelected(item);
}
How to customize the menus in this image. can anyone tell the solution
I want to display four submenus(FB, Google+, Twitter and SeeAll) If i Touch SeeAll a dialog has to popup which consist of more(FB, Google+, Twitter, Linkedin, NetLog, etc..,)
Code :
Menu.xml:-
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
Activity:-
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
ShareActionProvider myShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_TEXT, "Whatever message you want to share");
myIntent.setType("text/plain");
myShareActionProvider.setShareIntent(myIntent);
return true;
Try This :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_location_found"
android:clickable="true"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"
android:title="Share">
<menu>
<item
android:id="#+id/facebook"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Facebook">
</item>
<item
android:id="#+id/twitter"
android:orderInCategory="2"
android:showAsAction="never"
android:title="Twitter">
</item>
<item
android:id="#+id/gplus"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Google Plus">
</item>
<item
android:id="#+id/seeall"
android:orderInCategory="4"
android:showAsAction="never"
android:title="See All">
</item>
</menu>
</item>
</menu>
Activity (Java Code):-
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.facebook:
Toast.makeText(getApplicationContext(), "Testing", 1000).show();
// write the code here, what you want the action when user click
// facebook submenu....
break;
case R.id.twitter:
break;
case R.id.gplus:
break;
case R.id.seeall:
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
}
Kotlin Code : -
class MainActivity:Activity() {
protected fun onCreate(savedInstanceState:Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onCreateOptionsMenu(menu:Menu):Boolean {
val inflater = getMenuInflater()
inflater.inflate(R.menu.main, menu)
return super.onCreateOptionsMenu(menu)
}
fun onOptionsItemSelected(item:MenuItem):Boolean {
when (item.getItemId()) {
R.id.facebook ->
Toast.makeText(getApplicationContext(), "Testing", 1000).show()
R.id.twitter ->
{ //code
}
R.id.gplus ->
{ //code
}
R.id.seeall ->
{ //code
}
else -> return true
}// write the code here, what you want the action when user click
// facebook submenu....
return super.onOptionsItemSelected(item)
}
}
I'm totally new in android and I'm doing an app that change the background color when I press a button. I have already done that but now I want to add a menu item which will start the mode with or without buttons.
I was trying to do this but I don't know if I'm doing good.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item){
switch(item.getItemId()) {
case R.id.action_settings:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Hidden").
setPositiveButton("OK", null).
create().
show();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}
}
and XML
<menu>
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Hidden"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="2"
android:showAsAction="never"
android:title="Visible"/>
<item
android:id="#+id/action_exit"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Cancel"/>
</menu>
First edit menu items id: from android:id="#+id/action_settings" to android:id="#+id/action_hidden"; from android:id="#+id/action_settings" to android:id="#+id/action_visible";
from android:id="#+id/action_exit" to android:id="#+id/action_cancel";
XML file:
<menu>
<item
android:id="#+id/action_hidden"
android:title="Hidden"/>
<item
android:id="#+id/action_visible"
android:title="Visible"/>
<item
android:id="#+id/action_cancel"
android:title="Cancel"/>
</menu>
Then use this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater menuInflater = getMenuInflater();
getMenuInflater().inflate(R.menu.YOUR_ACTIVITY!!!!, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_hidden:
//code for action_hidden
return true;
case R.id.action_visible:
//code for action_visible
return true;
case R.id.menu_cancel:
//code for menu_cancel
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When my menu is clicked two times onoptionitemselected is called. how to stop it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.docmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.upload:
Log.e("testing", "called");
return true;
case R.id.back:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
my menu xml is
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/upload"
android:icon="#drawable/menu_upload"
android:title="#string/upload" />
<item android:id="#+id/back"
android:icon="#drawable/menu_back"
android:title="#string/back" />
</menu>
when upload icon is selected. In log testing called is printed two times.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.Aboutus:
final Dialog d1 = new Dialog(Welcome.this);
d1.setContentView(R.layout.aboutus);
d1.show();
break;
And make sure that you have created folder under res named menu. and make new menu.xml file
and put code like this in menu.xml file as follows:
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/Aboutus"
android:title="About Us" android:icon="#drawable/ic_menu_about_us" />
<item android:id="#+id/Settings"
android:title="Settings" android:icon="#drawable/ic_menu_settings"/>
<item android:id="#+id/help"
android:title="Help" android:icon="#drawable/ic_menu_help" />
onOptionsItemSelected return true is ok
Try this code...
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_settings:
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_LONG).show();
break;
case R.id.my_settings:
Toast.makeText(getApplicationContext(), "Home Page", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), "Exit", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
And create a new xml in menu folder and apply this code.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="#+id/my_settings"
android:title="#string/my_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>