switch executes both cases in options menu - android

#Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
switch(item.getItemId())
{
case R.id.item1: fukncijaD();
case R.id.item20: funkcijaOceni();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
//here is the xml for the menu
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Зачувај"
android:id="#+id/item1"></item>
<item android:id="#+id/item20" android:title="Оцени"></item>
<item android:id="#+id/item3" android:title="Пост на ФБ"></item>
</menu>
When I check the value in case R.id.item1: fukncijaD(); it goes as it should with funckcijaD(), and then continues with the second case as if there were no cases...

Put break
switch(item.getItemId())
{
case R.id.item1:fukncijaD();break;
case R.id.item20:funkcijaOceni();break;
}

Related

Toast on MenuItem not appearing and I am stuck

I created a first phase in sorting and try to take at it little by little but I failed at very start when Toast is not appearing
this is my code
`#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
inflater.inflate(R.menu.sort,menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.sort_date:
Toast.makeText(getContext(),"Sort by Date",Toast.LENGTH_SHORT).show();
return true;
case R.id.sort_category:
Toast.makeText(getContext(),"Sort by Category",Toast.LENGTH_SHORT).show();
return true;
case R.id.sort_highINC:
Toast.makeText(getContext(),"Sort by Highest Income",Toast.LENGTH_SHORT).show();
return true;
case R.id.sort_lowINC:
Toast.makeText(getContext(),"Sort by Lowest Income",Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}`
and this one is from the Menu.xml
`
<item android:id="#+id/sort_date"
android:title="Date"
app:showAsAction="never"/>
<item android:id="#+id/sort_category"
android:title="Category"
app:showAsAction="never"/>
<item android:id="#+id/sort_highINC"
android:title="Highest Income"
app:showAsAction="never"/>
<item android:id="#+id/sort_lowINC"
android:title="Lowest Income"
app:showAsAction="never"/>
`
I was expecting a toast message but it wont appear here
enter image description here

add toggle button to the actionbar

I have map activity and I am trying to add toggle button to the actionbar next to the setting menu which contains the map type but I am facing a problem: the toggle button is being added to the setting items menu and not directly to the actionbar.
How can I add this toggle button to the actionbar next to the setting menu?
map_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mapTypeNormal"
android:title="Normal"/>
<item
android:id="#+id/mapTypeSatellite"
android:title="Satellitte"/>
<item
android:id="#+id/mapTypeTerrain"
android:title="Terrain"/>
<item
android:id="#+id/mapTypeHybrid"
android:title="Hybrid"/>
<item
android:id="#+id/mapTypeNone"
android:title="None"/>
<item android:id="#+id/menu_toggle"
android:showAsAction="ifRoom"
android:icon="#drawable/off"
android:title="Share"
/>
</menu>
map activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
System.out.println("ABC MAP onCreateOptionsMenu was invoked.");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapTypeNone:
map.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
case R.id.mapTypeNormal:
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.mapTypeSatellite:
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.mapTypeTerrain:
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.mapTypeHybrid:
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
default:
break;
}
switch (item.getItemId()) {
case R.id.menu_toggle:
if (birthSort) {
// change your view and sort it by Alphabet
item.setIcon(R.drawable.on);
item.setTitle("On");
birthSort = false;
} else {
// change your view and sort it by Date of Birth
item.setIcon(R.drawable.off);
item.setTitle("Off");
birthSort = true;
}
}
return super.onOptionsItemSelected(item);
}
}
Use additional attribute always on menu item:
app:showAsAction="always"
and it can be combined with text as well:
app:showAsAction="withText|always"
Adjust your namespace accordingly. Here is a complete sample:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_sync"
android:title="#string/action_sync"
app:showAsAction="withText|always"/>
</menu>
Pay attention to the app: prefix for showAsAction and the corresponding namespace declaration.

android submenu button item access before menu clicked

I have a submenu with buttons set up in xml which works fine.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_level"
android:title="Set Level" >
<menu>
<group android:checkableBehavior="single">
<item android:id="#+id/one"
android:checked="false"
android:title="One" />
<item android:id="#+id/two"
android:title="Two" />
<item android:id="#+id/three"
android:title="Three" />
</group>
</menu>
</item>
<item android:id="#+id/menu_save"
android:icon="#drawable/icon_save"
android:title="Save" />
<item android:id="#+id/menu_about"
android:icon="#drawable/icon_bookmark"
android:title="About">
<menu>
<item android:id="#+id/copyright"
android:title="(c) John Beukema 2012">
</item>
</menu>
<item/>
</menu>
I access it as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Editor editor = PreferenceManager
.getDefaultSharedPreferences(this).edit();
switch (item.getItemId())
{
case R.id.menu_save:
return true;
case R.id.menu_recall:
return true;
case R.id.menu_level: {
return true;
}
case R.id.menu_about:
return true;
case R.id.one:
star = 1;
editor.putInt("Star", 1);
editor.commit();
item.setChecked(true);
return true;
case R.id.two:
star = 2;
editor.putInt("Star", 2);
editor.commit();
item.setChecked(true);
return true;
case R.id.three:
star = 3;
editor.putInt("Star", 3);
editor.commit();
item.setChecked(true);
return true;
case R.id.menu_delete:
return true;
case R.id.menu_preferences:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
So far no problem. But I want to load the buttons on start up from preferences.
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
star = preferences.getInt("Star", 1);
MenuItem one = (MenuItem) findViewById(R.id.one);
MenuItem two = (MenuItem ) findViewById(R.id.two);
MenuItem three = (MenuItem) findViewById(R.id.three);
switch(star)
{
case 1:
one.setChecked(true); break;
case 2:
two.setChecked(true); break;
case 3:
three.setChecked(true); break
}
}
}
This compiles but hangs. How do I address the submenu buttons?
I dont think you can do findviewbyid on menuitem. You need to set or unset the checked items in onprepareoptionsmenu

How to create Spinner Action Item in Action Bar

Anyone know how to create the submenu with radio buttons like in the screenshot?
If you want a submenu with checkable radio buttons you should use something like that:
<?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" />
<item android:id="#+id/blue"
android:title="#string/blue" />
</group>
</menu>
And then in the code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.red:
return true;
case R.id.blue:
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Getting menu clicks

Alright, this might be simple but I dunno how to do it! I have my menu defined through XML, as shown below. It loads and everything.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/home"
android:title="Home" />
<item android:id="#+id/about"
android:title="About" />
<item android:id="#+id/quit"
android:title="Quit" />
</menu>
Now, going through onOptionsItemSelected(), how do I tell which menu item is selected?
This is from an example... What would the case's be?
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "Home", Toast.LENGTH_LONG).show();
return true;
case 2:
Toast.makeText(this, "About", Toast.LENGTH_LONG).show();
return true;
case 3:
Toast.makeText(this, "Quit", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
Your case statements should use the ids defined in your xml:
case R.id.home:
....
case R.id.about:
....
case R.id.quit:
....
default:
throw new IllegalStateException("oops, forgot to code something");
the default case is just good practice imho. :)

Categories

Resources