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.
Related
I've made a Toolbar in the Activity xml file (snipped for brevity)
<Toolbar
android:id="#+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:title="#string/password_manager"
android:logo="#android:drawable/ic_menu_view"
android:background="#color/colorPrimary"/>
I did try other flavors of toolbar - androidx.appcompat..., ...appbar.MaterialToolbar, and the one I came across the most often android.support.v7.widget.Toolbar. These crash my app.
I made a menu xml that has this (snipped for brevity):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/hmenu_createnew"
android:checkable="false"
android:enabled="true"
android:title="#string/create_new"
app:showAsAction="always" />
</menu>
At this point everything looked and worked fine. Now - I want to dynamically control the menu items. I added the override:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
super.onPrepareOptionsMenu( menu );
this.menu = menu;
return true;
}
And here's where it's gotten frustrating. The above override was not called. I did a ton of searching and came across one person who suggested a change to the Activity class declaration.
He said instead of 'Extends Activity' change to 'Extends AppCompatActivity'
I checked my class declaration and it already was AppCompat. On a whim I changed it to Activity.
With this change I can see now in debugger my override is getting called. Yeay!
But my menu is nowhere to be found!.
In my activity.class are these few lines (snipped for brevity):
protected void onCreate(Bundle savedInstanceState) {
toolbar = (Toolbar) findViewById(R.id.mytoolbar);
setActionBar( toolbar );
toolbar.inflateMenu( R.menu.home_mainmenu );
}
Since that no longer worked I removed toolbar.inflateMenu(), replaced it with getMenuInflater() and put it in the override, as below.
#Override
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu( menu );
getMenuInflater().inflate( R.menu.home_mainmenu, menu );
return false;
}
No joy.
I also tried this:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
super.onPrepareOptionsMenu( menu );
getMenuInflater().inflate( R.menu.home_mainmenu, menu );
this.menu = menu;
return false;
}
Still no joy.
In the two overrides above I also tried substituting getMenuInflater.... for toolbar.inflateMenu...
No joy.
What I want is a regular toolbar with a right justified standard button with the three vertical dots that displays a menu when clicked.
At this point I am clueless so let me close by saying THANK YOU in advance to the angel out there who has the solution.
Does anyone else think this is needlessly difficult?..... Again, TIA.
I suggest applying androidX migration. So Toolbar package will be
androidx.appcompat.widget.Toolbar and for parent activity androidx.appcompat.app.AppCompatActivity
Make sure that the Activity theme is not inherited from Theme.AppCompat.NoActionBar
use setSupportActionBar(toolbar) method not setActionBar in onCreate or inside overridden setContentView method
Inflate custom menu before super.onCreateOptionsMenu(menu):
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(/*menu res id*/)
this.menu = menu;
return super.onCreateOptionsMenu(menu)
}
With such setup it should work
Well I eventually did figure it out. To repeat a bit, I am using this library:
import android.widget.Toolbar;
because I want to use Activity - not AppCompatActivity
so the xml declaration looks like this: <Toolbar ..... />
Because we are using Toolbar we must remove the default ActionBar that Android gives us. This requires an edit of the app manifest.
Anywhere you find "AppTheme.ActionBar" change to "AppTheme.NoActionBar"
in the java class I have this:
protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = findViewById(R.id.mytoolbar);
setActionBar( toolbar );
}
Note the use of 'setActionBar' as opposed to 'setSupportActionBar'. That is correct.
Then there is the (finally!) properly coded override which might be informative to anyone regardless of what import library you choose:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
super.onPrepareOptionsMenu( menu );
if ( !bMenuInitialized ) {
this.menu = menu;
getMenuInflater().inflate( R.menu.home_mainmenu, menu );
bMenuInitialized = true;
}
/*
invalidateOptionsMenu();
toolbar.inflateMenu( R.menu.home_mainmenu );
*/
return true;
}
I left in the commented block what NOT to do. Calling invalidateOptionsMenu(), which you will see all over the internet, causes an infinite loop. Do not do that..... Only call invalidateOptionsMenu from some other code block if you have altered the menu in some way. This is the reason why the toolbar was not showing itself. (By 'altering the menu' I don't mean setting attributes on MenuItems.)
I also learned that you MUST create a flag to indicate if the menu has been inflated yet, because the onPrepareOptionsMenu() gets called every time the user accesses the toolbar. You don't want to keep inflating your menu every time because it just adds to the menu that's already in it making it longer and longer.
There you have it. I hope this helps someone. And thank you to all who responded.
I have instaled ActionBarSherlock from this tutorial. The problem is that when I have instaled Sherlock, in 2.2 version ActionBar still does not show up. Then I tried to implement ActionBar from Java file, when I start my app it threws error because I need at least 3.0 version. The Main Activtity is ListActivity, so my question is how can i implement SherlockActivity to ListAcivty and do I need to do it?
first of all you should declare its theme in the manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light">
then in each activity you want the actionbar there you should extend it to the actionbar sherlock.
public class xxxx extends SherlockActivity {
and for listview, you can use actionbar sherlock's one by using this
... extends SherlockListActivity {
and this is in the end of you acitvity if you are going to use actionbar sherlock's menu buttons
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.MenuOfButtons, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.idOfButton:
//action done
break;
}
return true;
}
}
Check this video tutorial:
http://www.youtube.com/watch?v=1S9f7GQlWDg
You need to extend from SherlockActivity.
I am afraid you can't use sherlock action bar when extending from other class like ListActivity. May be some workaround of creating an inheritance heiratchy of ListActivity and SherlockActivity come handy but it will be very complex.
If you can't afford to avoid ListActivity, you can increase API level (I think 12) in your manifest and use the action bar provided by android API.
You want to use this in manifest.Whether you applied this?
android:theme="#style/Theme.Sherlock.Light"
Moreover declare this in Main Activity when you extend SherlockActivity
final ActionBar ab = getSupportActionBar();
// set defaults for logo & home up
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayUseLogoEnabled(true);
ab.setDisplayShowTitleEnabled(true);
I followed the following link to create a test application to create menu:
http://developer.android.com/guide/topics/ui/menus.html
It worked for me. However, when I added it to my app which is a google map app, menu is not working. I suspect its because the in the sample I created I extended Activity class but in my app I have extended MapActivity class.
Can someone explain how can i do it?
Menu is working same for MapActivity as for Activity class. You just need to override onCreateOptionsMenu method. Doublecheck your resource name R.menu.mapmenu etc.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mapmenu, menu);
return true;
}
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.
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);
}