How do I use the Action Bar in Android 2.1 - android

Various websites about this topic explained. But I did not. Please explain step by step use action bar in android 2.1.thanks

You need to use AppCompat from the support library.Download the Support library and import AppCompat to workspace. Its a Library project and you must reference the same in your Android project.
https://developer.android.com/tools/support-library/setup.html
Check Adding libraries with resources in the above link
Your Activity needs to ActionBarActivity. Need to use Theme.AppCompat
Check Adding ActionBar #
http://developer.android.com/guide/topics/ui/actionbar.html
or You can use ActionBarSherlock.
http://actionbarsherlock.com/

in res/menu you will add xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings">
</item>
and then in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_refresh){
}
return true;
}

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.

How to set visibility for an actionbar menu group?

UPDATE
Originally I was using ActionBarSherlock I have since created a brand new project using a native android action bar just to test this and I am still getting the same problem.
I am successfully showing/hiding items but not groups. I am rapidly coming to the conclusion that there is a bug in the ActionBar and it is not possible to programmatically set the visibility of a group
END of UPDATE
Given the following menu When accessing the Group I get a null pointer exception
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_settings"
android:title="#string/settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<group android:id="#+id/mnu_text_group"
android:visible="false">
<item android:id="#+id/mnu_text_type"
android:enabled="true"
android:visible="true"
android:icon="#drawable/ic_action_text_icon"
android:showAsAction="always">
</item>
<item android:id="#+id/text_color"
android:enabled="true"
android:visible="true"
android:showAsAction="always"
android:icon="#drawable/ic_action_color_line">
</item>
</group>
<item android:id="#+id/mnu_images"
...
In the onPrepareOptionsMenu of the relevant activity I have
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
mnuImage.setEnabled(mEnableImageMenu);
mnuTextGroup.setVisible(false);
...
The call to mnuTextGroup.setVisible(false); raises a null pointer exception
However, by changing the find method to find an item within the group works fine e.g. MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_type);but obviously this works just for the specific item. I know groups are designed for exactly this purpose, to be able to set the visibility of and enable/disable all items within the group but I have been unable to find a way to do this programatically.
Finally found the solution
I needed to use the setGroupVisible() method of the menu object passed into the onPrepareOptionsMenu() method
This is what worked for me
Instead of
MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
mnuImage.setEnabled(mEnableImageMenu);
mnuTextGroup.setVisible(false);
This is what I needed
menu.setGroupVisible(R.id.mnu_text_group, false);
Simple and one line
navigationView.getMenu().setGroupVisible(R.id.groupstaff, false);
Use In Activity Where You Want to Hide
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main_itemlist, menu);
boolean isdown = false;
menu.findItem(R.id.addwork).setVisible(isdown);
MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
mnuTextGroup.setVisible(isdown);
return true;
}
altering the options menu needs to be done within onPrepareOptionsMenu or else it sometimes doesn't work (not sure exactly why, hopefully someone else can elaborate):
#Override public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// set visibility of menu items here
return true;
}

Actions not showing in android action bar

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.

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