How to add a menu to MapActivity? - android

I've got an app using MapActivity.onCreate() to initialize the map and show it on screen. Now I would like to add a menu to my app. From what I've found out I can't add a menu from MapActivity and need to use Activity (correct me if I'm wrong).
Now I have no idea how to "initialize" the map from my Activity-class.
And how would I have to fix the views, will I wrap my activity-layout around my Map-layout?

MapActivity extends a regular Android Activity, so there's nothing irregular you should need to do to create a menu.
Just override the onCreateOptionsMenu method, as shown in the developers' guide.

MapActivity extends Activity, so you should be able to add a menu.

MapActivity is a subclass of Activity, and thus you do it the same way as in any normal Activity (instructions here). I've been able to successfully create menus the same way in MapActivity as in a normal Activity.

Make sure that it doesn't extend from FragmentActivity but from AppCompatActivity!
If that's the case, the onCreateOptionsMenu method will be called and you are able to overwrite it like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu); //"menu_main" is the XML-File in res
return super.onCreateOptionsMenu(menu);
}

Related

How to set action bar and left drawer attributes for fragments according to their types

I have various types of fragment in my application and there are 3 icons on ActionBar (filter, refresh and sort) but I don't want to show all 3 icons in each of the fragments. I have to show only some of them according to the fragment.
Similar thing I want to do with left drawer. On some fragments I want to show left drawer whereas don't want to display left drawer on others.
I have a Activity class in my application on which I am attaching these fragments and currently I am handling these two things in this class and code has become mess with if-else conditions.
So right now I am checking fragment name and then setting action bar icons and left drawer attributes according to it.
Please tell me a better way to do it( preferably to handle this in Fragment itself)
Thanks
Fragments have access to their activity through getActivity() function which will return non-null activity after onAttach() is called (and before onDetach()). Once the fragment has the activity it can tell it to do whatever you were doing right in the activity manually with checks, including changing the action bar buttons.
In order to show the options depending on the fragment, you can simply do the following:
Add setHasOptionsMenu(true) to the onCreate() method of the fragment and tell the Activity to redraw its options menu.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().invalidateOptionsMenu();
}
Next override the onCreateOptionsMenu() method to inflate the options that you want for your fragment.
// No support library - support library api slightly different
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Add Fragment menu elements to Activity menu elements
inflater.inflate(R.menu.myfragmentmenu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
Finally make sure to capture all option items in the onOptionsItemSelected() method of your activity.
(Important note: make sure to replace fragments instead of adding them. Otherwise onCreateOptionsMenu() will be called for each fragment.)
In order to disable and enable the drawer, you can add the following method to your Activity and call it from your fragment:
public void toggleDrawer(boolean enabled) {
if (enabled) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerToggle.setDrawerIndicatorEnabled(true);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerToggle.setDrawerIndicatorEnabled(false);
}
}
Create a new project from a sample project called ActionBarCombat and also download this sample application here http://www.learn2crack.com/2014/06/android-sliding-navigation-drawer-example.html
I once combined the two to come up with an application has action bar attributes as well as left drawer
To structure your code a bit why don't you create some methods in your activity like displayRefreshIcon(boolean visible) in which you handle the visibly of these items.
From your fragment you can call these methods (like frangulyan suggests) through the getActivity() function.
If (getActivity() != null && getActivity() instanceof MyActivity) {
((MyActivity)getActivity()).displayRefreshIcon(true);
}
In main thread or usual it is somehow impossible to do make changes in the activity itself, because fragments are separated module who are attached with activities but not the part of them.
But there is a shortcut that is to send message (handler) to activity to update the show the respective actionbar components
(most probably if you are using this fragment only for specific activity).
There you should make a base fragment and each fragment should extend baseFragment and at onResume method you have to check instance of Fragment then according to them you can update actionBar View.
Using any type of fragment, you should just have access to the methods (you have to override them): onCreateOptionsMenu, onPrepareOptionsMenu and onOptionsItemSelected. These methods should provide you with plenty of handles to create a menu per fragment. You could create a menu layout file per fragment and handle them in the method designed to do so. The methods:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.overviewmenu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

Can a new Android activity be created using an existing layout?

It seems that a new layout must be created when a new Android activity is created with the wizard in Eclipse. Whenever I create a new Android activity using an existing layout, I have to create a dummy layout, change the layout in onCreate() with setContentView(), then delete the dummy layout.
What is the best way to do this?
Edited Post: If you click File > New > Other, you can choose "Android > Android Activity".
Click next, and fill in the right data. If you reach the "Preview" part, you can select the changes that must be performed. I called the new activity "SecondActivity", which means the layout would file would be called "second_activity.xml". If you uncheck this file in the list, it won't create this file. Then just change your setContentView to the file you want.
You are relying too much on eclipse wizards. Be a programmer. Right click on package add new "class" Give it a name. Extend Activity. Override onCreate methods. In set content view use the layout already created.
Edit: Here are exact instructions
Right click on you package. Click New. Select Class.
Give your class a name, click Ok.
package com.example.fakeapp;
public class FakeActivity {
}
Now extend Activity add in onCreate and onCreateOptionsMenu Use the layout that you need in set content view.
package com.example.fakeapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class FakeActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity); //use whatever layout you want.
}
#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;
}
}
Add to the manifest between the tags dont forget to create your title in your res/strings.
<activity
android:name="com.example.fakeapp.Fakeactivity"
android:label="#string/title_activity_fakeactivity" >
</activity>
I did not mean to sound arrogant. What i meant to say was learn what the wizard is doing so you can recreate it and not rely on it to do everything for you. If you are afraid of editing the manifest then that is something that you need to learn.

handle click on iconmenu item

I see that it's possible to handle a tap on a icon menĂ¹ item or by implementing
onOptionsItemSelected
inside the acivity, or by using
onMenuItemClickListener
like onclick listener on a button. When is better to use the fist one method, and when the second one?
Because for my opinion, using an external listener makes more modular the code, but create a new class, but using the first way don't create new class, but makes code less modular...
There are use cases other than the ones outlined below, but I'm putting in the general cases that come up regularly.
onOptionsItemSelected
If you're using Fragments, you may want to use onOptionsItemSelected and consider adding menu items to the Action Bar the way that is described in Adding items to the Action Bar.
What this describes is implementing onCreateOptionsMenu inside your Fragment. To make this happen, you must call setHasOptionsMenu in onCreate.
protected void onCreate(Bundle savedInstanceState) {
this.setHasOptionsMenu(true);
}
Setting this will actually make the Activity call onCreateOptionsMenu which allows you to add the menu items.
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
// add items corresponding to this Fragment
menu.add(...);
return true;
}
The reason I recommend this is that it allows you to put more of the menu handling code into your Fragment instead of the Activity to figure out which Fragment to call, etc.
In this case, clicking the menu item will call onOptionsItemSelected inside of your Fragment which I suggest.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.my_id1:
dothing1();
return true;
case R.id.my_id2:
dotghing2();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
More of a long winded answer, but this is the way to handle menu clicks inside your Fragment.
onMenuItemClickListener
In the case of onMenuItemClickListener, this is used when you DON'T want to use the pre-ready method above and implement your own.
What I mean by that is you implement OnMenuItemClickListener and generate the methods in the interface. You then assign the menu to call the Activity that implemented this where as the above option assumes what Activity to use based on the pre-ready implementation of the Activity to Fragment relationship.
If you are targeting API 14 or greater (ICS or above) you could implement an ActionProvider. If that's not an option then you could implement a base activity that will always populate the menu and handle any menu clicks using onOptionsItemSelected. This is a good approach to implement "About" or "Settings" menu items through all your activities.

Android: How to add menu items to a ActionBarSherlock MapActivity

I am using ActionBarSherlock with ActionBarSherlock-Plugin-Maps in my project. I would like to add menu items to the actionbar of my MapActivity as it is possible for a standard activity that inherits from SherlockActivity. The following code sample shows how to create an icon.
public class CustomSherlockActivity extends SherlockActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Save")
.setIcon(R.drawable.ic_compose)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
However, I can not use the same method in my MapActivity that extends SherlockMapActivity. The method SherlockMapActivity#onCreateOptionsMenu is defined final. Here is the source code of SherlockMapActivity. How then, am I supposed to add actions to the actionbar?
You have the wrong Menu class imported. Make sure you are importing the one from the com.actionbarsherlock.view package.

DRY options menus on Android

I'm learning about creating Options Menus for Android apps.
In the guide it has the following tip for staying DRY with menus:
Tip: If your application contains
multiple activities and some of them
provide the same Options Menu,
consider creating an activity that
implements nothing except the
onCreateOptionsMenu() and
onOptionsItemSelected() methods. Then
extend this class for each activity
that should share the same Options
Menu. This way, you have to manage
only one set of code for handling menu
actions and each descendant class
inherits the menu behaviors.
This appears problematic. If the Activitys that need to share the same options inherit from different classes, what should my OptionsMenuActivity inherit from? I read that Java does not support multiple inheritance, so how do you get around this?
Your activity that has the code for options menu should extend the Activity class.
public class YourRootActivity extends Activity {
// Any other stuff that you want for all activities
public boolean onCreateOptionsMenu(Menu menu){
// your main options menu
}
}
Now for the classes that need this menu, make them extend the activity that we created above.
class Activity1 extends YourRootActivity {
}
In case you want slight modifications in your options menu in the subclasses, you can overwrite the onCreateOptionsMenu method in those classes.

Categories

Resources