Android action bar menu in tablet - android

At my new application that I created, I got auto generated code for creating menu:
#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;
}
And I added item at menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/item1"></item>
</menu>
But there is no menu button, am I missing something?

EDIT:
In your menu definition you put:
android:showAsAction="never"
Change it to:
android:showAsAction="always"
Check this example, including an icon for the menu:
<item
android:id="#+id/menu_calendar"
android:title="#string/calendar"
android:icon="#drawable/ic_menu_calendar_holo_light"
android:showAsAction="always|withText" />
always means the button will always be displayed. You can replace it by ifRoom if it is a menu such as Settings that should appear in the menu as an option but not displayed all the time.
withtext means that the title of the menu will be displayed beside the icon if there is enough place for it.
Details about all these options are available here.
For the rest, you need to create and show the ActionBar in your onCreate() function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//you might need this line if you are not using the Holo theme
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main_activity);
ActionBar actionBar = getActionBar();
actionBar.show();
(...)

Check for the import you are using. Check example below
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
You should use Menu and MenuInflater of actionbarsherlock..
Check out this link on implemention of ActionBarSherlock

Try
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:title="#string/action_settings"/>
<item android:id="#+id/item1"></item>

Related

Adding icon on Action Bar instead of three dots

I'm practicing follow this guide http://developer.android.com/training/basics/actionbar/adding-buttons.html
but it doesn't display icon "search" on action bar, it just have "Search" string after I clicked on "three dots". How to remove three dots and showing search icon on Action Bar?
My app like this but there is no search icon, but there is a "Search" string in "three dots"
add this to your menu item:
android:icon="#drawable/my_icon"
app:showAsAction="ifRoom"
What you have to do is setHasOptionsMenu method in true and override onCreateOptionsMenu and onOptionsItemSelected if you are using fragments.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.events_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
openSearchView();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is the menu xml file that displays a search option on the ActionBar:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"
app:showAsAction="always" />
You can remove the 3-dots menu by just adding below attribute android:showAsAction="never" to it:
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
And also use app:showAsAction="always" in all other menu items.
Hope this helps!

Programmatically change ActionBar icon

I'm coming back to my main activity from a fragment and for some logic I have to change the appearence of an icon on the action bar menu.
This is the menu on the action bar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="it.gn.sfa.Main">
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:title="Search" />
<item
android:id="#+id/action_filter"
android:icon="#drawable/ic_action_filter_empty"
android:showAsAction="ifRoom"
android:title="Filter" />
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:showAsAction="ifRoom"
android:title="New" />
</menu>
I have to change the sencond item (the one with id = action_filter).
I've tried different solutions, found on different post. The most rated is
mOptionsMenu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_action_filter));
but seems not to work.
On the other side getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_action_filter)); changes the logo, and I don't want so.
How can i change only the second item on menu?
try this one
mOptionsMenu.findItem(R.id.action_filter).setIcon(R.drawable.ic_action_filter);
Assuming you have it all set up for mOptionsMenu in
private Menu mOptionsMenu;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// inflating your menu here
mOptionsMenu = menu;
return super.onCreateOptionsMenu(menu);
}
Hope it helps :)
I hope it will be help for you
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>" + "Messages" + "</font>"));
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.messagebar_color)));
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_arrow_black);
You have to modify your onCreateOptionsMenu(Menu menu)
I changed the color of my search bar programmatically. I am posting the code here. Hope it helps.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
menu.getItem(0).setIcon(getTintedDrawable(R.drawable.search, R.color.blue));
return super.onCreateOptionsMenu(menu);
}
Where getTintedDrawable() is a function that i created which returns a drawable. So all you need to do is replace getTintedDrawable(R.drawable.search, R.color.blue) by your drawable.
NOTE: I have used menu.getItem(0) my code since I had only 1 item defined in menu/items.xml. If you have multiple try different values (from 0 to one less than number of menu items). My guess would be that its the number at which the item is defined but I'm not too sure.
I manage to rotate/change the icon this way:
MenuItem item = getToolbar().getMenu().findItem(Menu.FIRST);
<prepare the image view from drawable here>
item.setActionView(imageView);
Seems to work OK.
You could also simply use the item.setIcon() instead.

Android Action bar only showing in overflow

In my android app .. I am trying t put a search edittext with seARCH icon .. but I cant see the search icon in action bar rather the title is seeing in overflow. I am giving my code below
activity
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:title="Settings"/>
</menu>
MainActvity
public class MainActivity extends ActionBarActivity
{
WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv1 = (WebView) findViewById(R.id.wView1);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.loadUrl("http://www.example.com/songs/");
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
Use Search Manager n search view in onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
and in your main.xml add this item for search
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
I think you must return `true` instead of
super.onCreateOptionsMenu(menu)
in `onCreateOptionsMenu(Menu menu)` method
Excuses. That was wrong.
I think this problem has something to do with the base class you are using. Your are subclassing ActionBarActivity instead of Activity. This means that you are using compatibility for previous versions. For this to work, you need to set your application's Theme to Theme.AppCompat (or a similar one), and also include the Support Library. Look here
I just realized how to fix the problem!
your menu xml file should have an starting tag like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myApp="http://schemas.android.com/apk/res-auto" >
you can replace
myApp
with any names you want, NOW the important part is when you define an action item:
<item
android:id="#+id/actionitem_switch_view"
android:title="#string/view_as"
android:showAsAction="always"
myApp:showAsAction="always"
android:icon="#drawable/ic_action_view_as_list"/>
You'd have two showAsAction tags,one for API<11 devices and one for the others,
Look closely that the two showAsAction tags are different,one is from Android namespace and the other's from the tag you defined in menu Starting point

Three dots settings menu in the Action Bar Android doesn't appears

I want to create a simple option menĂ¹ on the Action Bar (classic three dots). I wrote this part of code but nothing appear:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Even this one but always nothing appear:
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Settings");
return super.onCreateOptionsMenu(menu);
}
There are no three dots on the Action Bar. Is there anything else to write maybe in the onCreate()?
Edit. The menu.xml
<xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Info" android:id="#+id/settings"></item>
</menu>
Add the below line into the menu xml file
<item
android:title="Info"
android:showAsAction="never"
android:id="#+id/settings" >
</item>
check AndroidManifest.xml, set android:targetSdkVersion="10" or lowwer.

MultiSelect Menu in Action Bar Sherlock

I wan to have one action bar with one "menu" button. (Until here I know how to do) But Now I want to do that, when the users Clicks on any subitem of the menu, don't close this menu.
Said in other words, I want to enable the multiselect menu in action bar sherlock, but I dont know how to do it.
Can someone explain me some way to achive it?
Thats what I have
public class MainActivity extends SherlockActivity{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Sherlock___Theme_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setTitle("Testing");
actionBar.setSubtitle("test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
And this is the XML Menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu"
android:orderInCategory="0"
android:title="Menu"
android:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/subitem1"
android:title="SubMenu1"/>
<item
android:id="#+id/subitem2"
android:title="SubMenu2"/>
<item
android:id="#+id/subitem3"
android:title="SubMenu3"/>
<item
android:id="#+id/subitem4"
android:title="SubMenu4"/>
</menu>
</item>
</menu>

Categories

Resources