The menu does not appear in the map activity - android

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.

Related

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 - Cannot see Options Menu when extends SherlockFragmentActivty

![Layout Look like this][1]
[1]strong text: http://i.stack.imgur.com/CIsz8.png
I am trying to create a menu, but I am unable to see the menu.i am able to see menu in fragments properly. but cannot see in the sherlockfragmentactivity that is my main frame for fragment.
when i click on menu they doesn't take any action.
menu code
<group android:id="#+id/dashbardmenu">
<item
android:id="#+id/abc"
android:title="ABC">
</item>
<item
android:id="#+id/setting"
android:title="Setting">
</item>
</group>
<group android:id="#+id/fragmentmenu" >
<item
android:id="#+id/form"
android:icon="#drawable/ic_send"
android:showAsAction="ifRoom"
android:title="Form">
</item>
<item
android:id="#+id/resetform"
android:title="Reset Form">
</item>
</group>
im using visibility & invisibility for particular file.
java code
1st way
public boolean onCreateOptionaMenu(Menu menu ){
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.menu.activity, menu);
menu.setGroupVisible(R.id.fragmentmenu, false);
return super.onCreateOptionsMenu(menu);
}
2nd way
public boolean onCreateOptionaMenu(Menu menu,MenuInflater inflater ){
inflater.inflate(R.menu.activity, menu);
menu.setGroupVisible(R.id.fragmentmenu, false);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected((MenuItem)item);
switch (item.getItemId()){
case R.id.abc:
//Some action
case R.id.setting:
//Some action
default:
return false;
}
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected((MenuItem)item);
switch (item.getItemId()){
case R.id.abc:
//Some action
case R.id.setting:
//Some action
default:
return true;
}
}
Where is my Mistake Anyone help me???
Did you call this?
setHasOptionsMenu(true);
Also you have a typo - method name is onCreateOptionsMenu but you have twice onCreateOptionaMenu

Android - OptionsMenu on top transparent

Im building my top menu using the following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_view, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.map_view:
Toast.makeText(getApplicationContext(), "Map view pressed",
Toast.LENGTH_LONG).show();
return true;
case R.id.camera_only:
Toast.makeText(getApplicationContext(), "Camera only pressed",
Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_view.xml
<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/camera_only"
android:showAsAction="never"
android:title="#string/camera_only"/>
</menu>
</item>
</menu>
It happens that in my main activity i want to use this menu on top, but the menu appers transparent. Im using a fragment to display a full-screen map (google maps api v2), but then i can see the map under the top menu. I would like this fragment to "auto-resize" under the menu.
How can i set menu always on top of every activity?
So, i found out what my problem was!
in styles.xml file:
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
</style> </resources>
i just needed to change the attribute windowActionBarOverlay to false, and it makes automatically the resize for the activity content.
For more details, here is the documentation

Android SubMenus not doing anything when clicked

I feel this may be a stupid question but I have no idea what to do.
I have a menu which works correctly. One of the items in the menu: "Search" brings up a submenu with different items like "Restaurant", "Cafe", etc.
Below is the XML code for creating the menu and sub menu:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_entry"
android:title="#string/new_entry"
android:icon="#drawable/add_new">
</item>
<item android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/search">
<menu>
<item android:id="#+id/five"
android:title="#string/five">
</item>
<item android:id="#+id/ten"
android:title="#string/ten">
</item>
<item android:id="#+id/fifteen"
android:title="#string/fifteen">
</item>
<item android:id="#+id/restaurant"
android:title="#string/restaurant">
</item>
<item android:id="#+id/cafe"
android:title="#string/cafe">
</item>
<item android:id="#+id/sandwich"
android:title="#string/sandwich">
</item>
</menu>
</item>
<item android:id="#+id/info"
android:title="#string/info_short"
android:icon="#drawable/info">
</item>
Then in my Activity Class I have:
// Initialise Menu.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_services, menu);
return true;
}
and:
// Create cases for each menu selection.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_entry:
createFood();
return true;
case R.id.search:
return true;
case R.id.cafe:
Log.d(TAG, "In Cafe SubMenu");
Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT);
return true;
case R.id.info:
info();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Each case in "onOptionsItemSelected" does what it is supposed to do, except "case R.id.cafe:" which is the submenu. Here it should post to the LogCat and a Toast for testing but does neither.
What am I missing?
Thanks
I have tried your code and it is working perfectly. Only the Toast is not displaying when I select 'cafe'. That is because, you did not call show() in your code. Your code is
Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT);
to show the toast on run time it should call as follows
Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT).show();
:)
I think you should remove following lines:
case R.id.search:
return true;

calling resource menu into other activity

Please help me how can i call a menu resource in other activity .
Here is code of main activity
public class ControlMenu extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#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.settings:
Intent intent = new Intent(this, ShowSettings.class);
startActivity(intent);
break;
case R.id.services: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
break;
case R.id.another:
}
return true;
}
here is menu resource
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Quit"
android:title="Quit"
android:icon="#drawable/icon" />
<item android:id="#+id/settings"
android:title="Settings"
android:icon="#drawable/icon" />
<item android:id="#+id/services"
android:title="Services"
android:icon="#drawable/icon" />
</menu>
i can call it in the other activity by writing the main activity code but for that i have rewrite the case statements as well so guide me how can i solve it out .
You should be able to add the menu code to a common Activity subclass, then make all your other Activity classes extend that common class instead of just Activity. For a simple menu this should work just fine.

Categories

Resources