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.
Related
Hello guys i want to display action with text and images on action bar, but i only get text in overflow menu
my menu.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_show_ir_list"
android:showAsAction="ifRoom"
android:title="Cancel"/>
<item
android:id="#+id/phone"
android:title="Wowio"
android:icon="#drawable/logo"
android:showAsAction="ifRoom"
/>
<!--android:icon="#drawable/info"-->
<item
android:id="#+id/computer"
android:title="Done"
android:showAsAction="ifRoom"
/>
</menu>
in my main class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected About App", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected About Developer", Toast.LENGTH_SHORT).show();
break;
case R.id.action_show_ir_list:
Toast.makeText(getBaseContext(), "Volume Set To Max", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
Here every thing is coming in over flow menu nothing is coming on action bar
I tried every thing on internet and nothing is working
What should i do to show them on ActionBar?
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:[yourapp]="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:orderInCategory="10"
[yourapp]:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
instead of [yourapp] type your app name
I am trying to create a menu with this option None, Normal, Terrain, Satellite, Hybrid (Google map) in the map activity to enable the users to select their desired map type but the menu in not being shown in the Map activity.
I have tried to add super.onCreateOptionsMenu(menu); to the onCreateOptionsMenu before inflater but without success.
How can I fix it?
Map activity:
public class Map extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener{
#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;
}
return super.onOptionsItemSelected(item);
}
}
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"/>
</menu>
Check what your MapActivity.java is extending.
If it is extends FragmentActivity then change it to extends AppCompatActivity.
and in your styles make sure this ..
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
</style>
Add it in the Build.gradle (Module:app)
inside the dependencies
dependencies {
..
compile 'com.android.support:appcompat-v7:22.2.0'
}
Extend "AppCompatActivity" and add "setSupportActionBar(toolbar)" inside the activity onCreate.
I made an Navigation-Drawer on the left and a menu on the right but my menu on the right (3 dots menu) is over the blue line...
I had the same problem with my Title text-view but i simply made his background-color transparent to fix it. Sadly i haven't been able to do the same with my menu.
Is there a way to make my menu background transparent or a work around to make my blue line go over it?
Here is how i made my menu:
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_view, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Menu options
switch (item.getItemId()) {
case R.id.map_view:
Toast.makeText(getApplicationContext(), "Map view pressed",
Toast.LENGTH_LONG).show();
//startActivity(GoogleMapsActivity.class);
return true;
case R.id.infrastucture_only:
Toast.makeText(getApplicationContext(),
"Infrastucture only pressed", Toast.LENGTH_LONG).show();
return true;
case R.id.infrastucture_map:
Toast.makeText(getApplicationContext(),
"Infrastucture + map pressed", Toast.LENGTH_LONG).show();
return true;
case R.id.camera_only:
Toast.makeText(getApplicationContext(), "Camera only pressed",
Toast.LENGTH_LONG).show();
return true;
case R.id.camera_infrastucture:
Toast.makeText(getApplicationContext(),
"Infrastucture + Camera pressed", Toast.LENGTH_LONG).show();
//startActivity(MetaioActivity.class);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/dots_menu"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="#string/dots_menu">
<menu>
<item
android:id="#+id/map_view"
android:icon="#drawable/ic_action_map"
android:showAsAction="never"
android:title="#string/map"/>
<item
android:id="#+id/infrastucture_only"
android:showAsAction="never"
android:title="#string/infrastucture_only"/>
<item
android:id="#+id/infrastucture_map"
android:showAsAction="never"
android:title="#string/infrastucture_map"/>
<item
android:id="#+id/camera_only"
android:showAsAction="never"
android:title="#string/camera_only"/>
<item
android:id="#+id/camera_infrastucture"
android:showAsAction="never"
android:enabled="false"
android:title="#string/camera_infrastucture"/>
</menu>
</item>
</menu>
Thank you :)
I fixed the problem. I forgot to go to my style.xml file and add the transparency to my custom style like this:
<style name="MyActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
</style>
Now i don't know why when i open the menu it's not transparent
http://i821.photobucket.com/albums/zz140/miguel_melo_43/openWindow_zpse164f5b7.png
Is there a better way to make the menu so i can customize like i did with my navigation drawer?
http://i821.photobucket.com/albums/zz140/miguel_melo_43/NavDrawer_zpscaf04f64.png
I am new to android and stuck at the point where i have to detect clicks on submenus that are defined in XML file
my XML file is :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ccard_menu1"
android:title="Select from Profile?"
></item>
<item android:id="#+id/ccard_menu2"
android:title="Add Field"
>
<menu >
<item android:id="#+id/submenu1"
android:title="Add Products"
></item>
<item android:id="#+id/submenu2"
android:title="Add Clients"
></item>
<item android:id="#+id/submenu3"
android:title="Add a Custom Field">s</item>
</menu>
</item>
</menu>
how do i detect clicks on "submenu 1,2,3" in onOptionsItemSelected method?
how do i have to structure the switch case?
I you are looking for something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.submenu1:
// do something
return true;
case R.id.submenu2:
//do something else
return true;
// etc..
default:
return super.onOptionsItemSelected(item);
}
}
Please correct me if I am mistaken.
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