Actions not showing in android action bar - android

I'm trying to make a dynamic action bar in android, right now I'm trying to make a search in the action bar like this.
http://developer.android.com/images/ui/actionbar-searchview#2x.png
However when I click the the search button in the action bar nothing happens.
Here is the menu XML file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title=""
android:icon="#drawable/abc_ic_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/action_menu"
android:title=""
android:icon="#drawable/abc_ic_menu"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.ShareActionProvider"/>
Here is the OnCreateOptionsMenu in the Activity
#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);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
I know the icon I have in the action bar is working because I have this code working without problems
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(getBaseContext(), "Search icon is working", Toast.LENGTH_LONG).show();
return true;
case R.id.action_menu:
Toast.makeText(getBaseContext(), "Menu icon is working", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I think it has something to do with this:
android:actionViewClass="android.support.v7.widget.SearchView"/>
Because even when I delete the android.support.v7 jar on the project (I just deleted it to test, I have it back again) I can execute the project without any errors. So maybe the XML file doesn't recognise the android.support.v7 path?
P.S: the action_menu icon doesn't work either.

I know the icon I have in the action bar is working because I have this code working without problems
That is consuming the event, preventing the SearchView from working. Please delete both case sections of this method, as they are incorrect for both of your menu items.
(and, please use this, not getBaseContext(), unless you know what getBaseContext() does)
Here is a sample app showing the use of SearchView.

Additionally android.support.v7.widget.SearchView throws NullPointerException at onCreateOptionsMenu() with default configuration.
Solved by changing it to android.widget.SearchView
The answer by #CommonsWare is valid also. It is more likely to be the correct answer.

Related

Android Optionsmenu does not change

So my problem is, is that I have a fragment. This fragment is used for creating new objects and editing existing objects.
When I create a new objects, I want to have a save button. And when I edit an object I also want a delete button.
I have two menu layout files. Which are
R.menu.element_actionbar_edit
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_delete"
android:icon="#drawable/ic_action_delete"
android:title="#string/delete"
yourapp:showAsAction="always"/>
<item android:id="#+id/action_save"
android:icon="#drawable/ic_action_done"
android:title="#string/save"
yourapp:showAsAction="always"/>
</menu>
R.menu.element_actionbar_add
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_save"
android:icon="#drawable/ic_action_done"
android:title="#string/save"
yourapp:showAsAction="always"/>
</menu>
Whenever the fragment with all the actions is called, it only onCreateOptionsMenu.
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.element_actionbar_add, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Eventhough I call the R.menu.add layout it still adds two items to the actionbar, before it actually inflates the menu already contains two items.
This fragment is the only place where I actually use a menu right now.
First I had added an if statement in the options create, to check if there was passed an existing object to edit, but in this case it did show the correct layout, but when nothing is passed I should get only the save button?Why does it show the wrong layout, without even specifying it anywhere else?
EDIT:
So it worked!
#Override
public void onPrepareOptionsMenu(Menu menu) {
if(currentObject==null) {
Global.ACTIVITY.getMenuInflater().inflate(R.menu.element_actionbar_add, menu);
}else{
Global.ACTIVITY.getMenuInflater().inflate(R.menu.element_actionbar_edit,menu);
}
super.onPrepareOptionsMenu(menu);
}
At first setting the menu using the onPrepare didn't work either. I had to delete the app from my phone completely, and reinstall it to make it work.
If you want your menus to change dynamically you should not
use onCreateOptionMenu instead use onPrepareOptionMenus
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
if(save){
menu.add(0, ADD, 0, "Save");
}
else{
// add rest
}
return super.onPrepareOptionsMenu(menu);
}
this method is called each time you click your menu button and menu.clear() is must since each time you add a menu.
Kindly share your results after implementing.

Android Maps and GUI

i am creating an android application which includes maps. I have finished the maps but for the reason that I am new to android programming I would like to ask how to create lets say a menu and when a button is clicked like the map button,then the map would be displayed. For the code that I have got now when i run the application the emulator shows the map,but what do i need to do in order to display a menu first instead of the map.For instance to display some options and when the map button is clicked then the map is shown up. Please help, anyone could give me some advice ?? thank you
Here you need to override onCreateOptionsMenu like:
#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;
}
then you need to override onOptionsItemSelected like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.map:
// write logic here for open map
Intent intent = new Intent(getApplicationContext(), YourMapActivity.class);
startActivity(intent);
}
return true;
}
And XML file is main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/map"
android:title="Map"/>
</menu>
Hi please try the example source code for Sliding Menu for Android.
Please try and let me know. Thanks
Sliding Menu for Android

Android no menu items showing in Action Bar

I am developing an Android app and I am trying to put a menu item into the ActionBar.
It has enough space, so it shouldn't be on the overflow or anything.
In my menu.xml I have added that item + android:showAsAction="ifRoom|withText"
However, no matter how large the screen is, that damn menu will not show up on the ActionBar (although it is present in the menu, if the user presses a key).
Unfortunately I cannot post any full-code since I am under a non-disclosure agreement, but I will answer all questions.
The section where I inflate the menu:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drinks, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.done:
//stuff
}
return super.onOptionsItemSelected(item);
}
Since you have only a menu item, use this attribute instead: android:showAsAction="always".
EDIT Above works all the time if you're running the code on post Honeycomb. But, in order to run on pre-Honeycomb, according to developer article, you need to extend from ActionBarActivity, that means adding compatibility support v4 & v7 and set the following theme for your activity:
<activity android:theme="#style/Theme.AppCompat.Light" ... >
... or a Them.AppCompat theme. Or use one of your own that extends from these.

Menu button is not show in 3.0 OS

I have created menu button which have two functions bookmark and home button.
This is working well in all the android version without android 3.0
do here any ways ? so my menu button will be display also in android 3.0 with all the version.
my code:-
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
db.updateContact(new Contact(itemN,imageStatus));
return true;
case R.id.home_page:
Intent i = new Intent(imageTouchs.this, Comics.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
and my androidmanifest.xml :-
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_bookmark"
android:title="Bookmark"
android:showAsAction="ifRoom|withText" />
<item android:id="#+id/home_page"
android:title="Home"
android:showAsAction="ifRoom|withText" />
</menu>
any menu is not display in action bar
The "overflow' button will only show up when there isn't a physical menu button on the device. This is the standard behavior for the ActionBar.
You can actually set the value of your AVD to specifically not have a menu button when you are configuring the image.
Here is a SO question addressing this same thing: Action bar overflow not displayed
If you are talking about the OverFlowMenu icon, to the best of my knowledge, it cannot be achieved (forced) using the standard Android Support Library.
If you must have an OverFlowMenu (forced), you will need to use the ActionBarSherlock library. Go through a couple of my answers where I have a few fairly detailed suggestions on how to achieve that:
https://stackoverflow.com/a/13307583/450534
https://stackoverflow.com/a/13180285/450534
NOTE: As already mentioned in both my answers linked above, this is not recommended to ensure users get a seamless UI on their devices. And also, if you must absolutely force the OverFlowMenu, you will need to use an older version of ABS, which is again, not recommended.

How to create the Action Overflow part of the ActionBar?

I have been looking online for a while, and there are very little tutorials on how to do this. Even the google docs have very vague info on how to put this stuff up there! I am sure its subtle, but I can't understand the terminology because I am still fairly new to Android. I would like to be able to get the Action Overflow icon to the right side of the ActionBar. Putting the view control was pretty understandable using the docs and example code when creating a project, but it does not have one for the Action Overflow. Thanks in advance!
Edit: I should probably elaborate. I would like the menus to default being under the action overflow. I found an eye opener answer to a similar question, but it only tells you how to put the menus at the top. How can I force them to go under the list? Is it even possible to force that? Thanks!
If you use API-11, it's not problem. If lower I am inviting you to this topic :
The best way to create drop down menu in android 2.x like in ICS
In the case when API-11 and higher you must :
Create menu xml like this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item_refresh"
android:icon="#drawable/ic_menu_refresh"
android:title="Refresh"
android:showAsAction="ifRoom|withText" />
<item
android:id="#+id/item_save"
android:icon="#drawable/ic_menu_save"
android:title="Save"
android:showAsAction="ifRoom|withText" />
</menu>
And create code like this :
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// gets the activity's default ActionBar
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu); //inflate our menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.item_refresh:
//click on refresh item
break;
case R.id.item_save:
//click on save item
break;
}
return true;
}
Good Luck!

Categories

Resources