I've seen a few posts about getting the title to show in actionbarsherlock along with the icon. The icon is displaying fine, but the title does not show beside it.
Currently it shows the home icon with all the tabs beside it. What I am looking for is the home icon, then title, and below all this have the tabs show. Here is my code.
ACTIVITY
public class MainActivity extends SherlockFragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mJsonHandler = JsonHandler.getInstance(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(true);
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayUseLogoEnabled(true);
ab.setTitle("TITLE TO SHOW");
inflater.inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId()) {
case R.id.menu_item0:
break;
case R.id.menu_item1:
break;
case R.id.menu_item2:
break;
case R.id.menu_item3:
break;
case R.id.menu_item4:
break;
case R.id.menu_item5:
break;
default:
this.onBackPressed();
break;
}
return false;
}
}
activity_main.xml (for menu)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item0"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_item0"/>
<item
android:id="#+id/menu_item1"
android:orderInCategory="2"
android:showAsAction="always"
android:title="#string/menu_item1"/>
<item
android:id="#+id/menu_item2"
android:orderInCategory="3"
android:showAsAction="always"
android:title="#string/menu_item2"/>
<item
android:id="#+id/menu_item3"
android:orderInCategory="4"
android:showAsAction="always"
android:title="#string/menu_item3"/>
<item
android:id="#+id/menu_item4"
android:orderInCategory="5"
android:showAsAction="always"
android:title="#string/menu_item4"/>
<item
android:id="#+id/menu_item5"
android:orderInCategory="6"
android:showAsAction="always"
android:title="#string/menu_item5"/>
Manifest
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Sherlock.Theme" >
<activity
android:name=".activities.MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Sherlock Theme
<style name="Sherlock.Theme" parent="Theme.Sherlock.Light.DarkActionBar">
</style>
You're setting too many menu items to always show in the ActionBar. You should only set the most important ones to do so. You should read through the Android Design Guidelines on the appropriate way to set up the ActionBar
Android Design Guidelines - Action Bar organization
Also, you should set up the ActionBar in onCreate and leave onCreateOptionsMenu for only inflating your menu(s). This is just a general tip and goes towards better, more professional code formatting.
Related
I have made a ListView with selectable items but the ActionMode is not showing properly. (There is and a "compare" menu button on the right with white color)
I tried to style the actionMode with the following code but nothing changes. Any ideas why might this happens? I found out that if I set the background color direct on the Toolbar widget instead of the DarkTheme.ActionBar the color arround the text on actionMode is gone but still the color of the text is white and also I need to have the color of the ActionBar defined on the theme instead of the widget.
toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/action_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:theme="#style/DarkTheme.ActionBar"
app:popupTheme="#style/DarkTheme.Popup"/>
mytheme.xml
<resources>
<style name="DarkTheme.ActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColorPrimary">#color/abc_primary_text_material_dark</item>
<item name="actionMenuTextColor">#color/abc_primary_text_material_dark</item>
<item name="android:textColorSecondary">#ffff8800</item>
<item name="android:textAllCaps">false</item>
<item name="android:background">#303030</item>
</style>
<style name="DarkTheme.Popup" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColor">#ffffff</item>
</style>
<style name="DarkTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColor">#101010</item>
<item name="android:textColorLink">#ff0099cc</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:colorPrimaryDark">#000000</item>
<item name="android:navigationBarColor">#000000</item>
<item name="android:textAllCaps">false</item>
<item name="android:actionModeStyle">#style/DarkTheme.ActionMode</item>
</style>
<style name="DarkTheme.ActionMode" parent="Widget.AppCompat.ActionMode">
<item name="android:actionModeBackground">#android:color/black</item>
<item name="android:background">#000000</item>
<item name="android:backgroundSplit">#000000</item>
</style>
</resources>
I don't realy understand your stuff, it looks strange.
Theme are for Activity or Application in the manifest. For widget you should use style.
When looking at your question, I think, you just don't know how to use theme and what themes are for.
So your theme should looks like a stuff like that:
<resources>
<style name="AppBlankTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#color/accent</item>
</style>
<!--Both themes below are those accepted to make the ToolBar works-->
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBlankTheme">
<!-- Customize your theme here. -->
<!-- Base application theme. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="MyAppTheme" parent="Theme.AppCompat.NoActionBar"/>
<!--The Theme for the Actvity that have actionMode-->
<style name="ActionModeAppTheme" parent="AppTheme">
<item name="windowActionModeOverlay">true</item>
<item name="actionModeBackground">#color/primary_dark</item>
<item name="actionBarPopupTheme">#style/ThemeOverlay.AppCompat.Light</item>
</style>
</resources>
Then your manifest you looks like something like that:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android2ee.formation.lollipop.toolbar" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".sample.ActivityWithItems"
android:label="#string/title_activity_activity_with_items"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android2ee.formation.lollipop.toolbar.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.android2ee.formation.lollipop.toolbar.EXAMPLE" />
</intent-filter>
</activity>
<activity
android:name=".sample.ActionModeActivity"
android:label="#string/title_activity_actionmode"
android:theme="#style/ActionModeAppTheme"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android2ee.formation.lollipop.toolbar.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.android2ee.formation.lollipop.toolbar.EXAMPLE" />
</intent-filter>
</activity>
</application>
</manifest>
And your activity looks like something like that:
public class ActionModeActivity extends AppCompatActivity {
ActionMode mMode;
/**
* The action Bar
*/
private ActionBar actionBar;
private Toolbar toolbar;
Callback actionModeCallBack;
private boolean postICS,postLollipop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//You could also hide the action Bar
// getSupportActionBar().hide();
setContentView(R.layout.activity_action_mode);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_action_custom_up);
postICS =getResources().getBoolean(R.bool.postICS);
postLollipop =getResources().getBoolean(R.bool.postLollipop);
if(postLollipop){
toolbar.setElevation(15);
}
setSupportActionBar(toolbar);
actionBar=getSupportActionBar();
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
// Show the Up button in the action bar.
findViewById(R.id.start_actionmode).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
enableActionMode();
}
});
findViewById(R.id.stop_actionmode).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mMode != null) {
//To quit the ActionMode
mMode.finish();
}
}
});
actionModeCallBack=new Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
getMenuInflater().inflate(R.menu.action_mode, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(ActionModeActivity.this, "Got click: " + item, Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
};
}
private void enableActionMode() {
mMode = startSupportActionMode(actionModeCallBack);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_with_items, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am defining a search action in my action bar. The search works perfectly. However, when touching the search icon a white background layout occurs (containing the app icon) and when touching the back arrow a blue layout occur (without the app icon).
My problem is that I only want to show the blue layout (appearing after touching the back arrow on the white layout).
Here are code relative to the search action bar implementation.
AndroidManifest:
<meta-data
android:name="android.app.default_searchable"
android:value=".RecipeListActivity" />
</activity>
<activity
android:name=".RecipeListActivity"
android:label="#string/title_activity_recipe_list"
android:parentActivityName=".RecipeFolderActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.cookbook.android.mycookbook.RecipeFolderActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
This is the definition of the menu in xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.cookbook.android.mycookbook.MainFolderActivity" >
<item android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/icon_loupe"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/add_main_folder"
android:title="#string/add_main_folder"
android:orderInCategory="101"
app:showAsAction="never" />
</menu>
Here is the java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_folder, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==R.id.search){
Log.d(LOG_TAG,"In onOptionItemSelected, for search");
onSearchRequested();
}
return super.onOptionsItemSelected(item);
}
Any help will be greatly appreciated. :-)
I am following the Android Developers Guild
I want to have the green robot logo on the very left hand side of the action bar
http://developer.android.com/images/training/basics/actionbar-theme-custom#2x.png
but I do not have the logo on action bar at all. I have searched for a few days, still can not locate an answer for this.
Please help, thanks!
src/.../MainActivity.java
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayUseLogoEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_tab:
ActionBar.openTab(this);
return true;
case R.id.action_search:
ActionBar.openSearch(this);
return true;
case R.id.action_settings:
ActionBar.openSettings(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
menu/action_bar.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Tab, should appear as action button -->
<item android:id="#+id/action_tab"
android:title="#string/action_tab"
app:showAsAction="always" /> <!-- you can change "always" to "ifRoom" -->
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="always" /> <!-- you can change "always" to "ifRoom|withText" -->
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
res/values/themes.xml
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionMenuTextColor">#color/actionbar_otherthantitleandoverflowtext</item>
<item name="android:windowActionBarOverlay">false</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="actionMenuTextColor">#color/actionbar_otherthantitleandoverflowtext</item>
<item name="windowActionBarOverlay">false</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#color/actionbar_background</item>
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
<!-- Support library compatibility -->
<item name="background">#color/actionbar_background</item>
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_titletext</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
</resources>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:logo="#drawable/ic_launcher"
android:theme="#style/CustomActionBarTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName=".MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
AFAIK ActionBar should be by default be displayed at top, but in my case it's coming at bottom.
I've followed the standard tutorial and kept all basics in check.
Here's my code and supporting screenshot:
MainActivity.java
package com.android.actionbardemo;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
Toast.makeText(this, "Home Option Selected", Toast.LENGTH_SHORT)
.show();
return true;
case R.id.java:
Toast.makeText(this, "Java Option Selected", Toast.LENGTH_SHORT)
.show();
return true;
case R.id.android:
Toast.makeText(this, "Android Option Selected", Toast.LENGTH_SHORT)
.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
main.xml (menu)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/home"
android:icon="#drawable/home"
android:showAsAction="ifRoom"
android:title="Home"/>
<item
android:id="#+id/java"
android:icon="#drawable/java"
android:showAsAction="ifRoom"
android:title="Java"/>
<item
android:id="#+id/android"
android:icon="#drawable/android"
android:showAsAction="ifRoom"
android:title="Android"/>
</menu>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.actionbardemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:uiOptions="splitActionBarWhenNarrow" >
<activity
android:name="com.android.actionbardemo.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Am I doing anything wrong?
Any help appreciated.
You're using the uiOptions splitActionBarWhenNarrow in your application tag.
From the docs:
Add a bar at the bottom of the screen to display action items in the
ActionBar, when constrained for horizontal space (such as when in
portrait mode on a handset). Instead of a small number of action items
appearing in the action bar at the top of the screen, the action bar
is split into the top navigation section and the bottom bar for action
items. This ensures a reasonable amount of space is made available not
only for the action items, but also for navigation and title elements
at the top. Menu items are not split across the two bars; they always
appear together.
I added a search to the Action bar by using the following code:
activity_main_actions.xml
<!-- 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"/>
<!-- Add -->
<item android:id="#+id/action_add_tip"
android:icon="#drawable/header_add"
android:title="#string/action_add_tip"
android:showAsAction="ifRoom" />
<!-- Count -->
<item android:id="#+id/action_count_tip"
android:icon="#drawable/header_count"
android:title="#string/action_count_tip"
android:showAsAction="ifRoom" />
in MainActivity.java
#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);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
searchable.xml
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Manifest.xml
<activity
android:name="com.emy.healthytips.MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTop">
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
The search is working well with me, but the icon of the search in Action bar is still as it "Gray icon" like the following ScreenShot, although I added my own icon in
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
How can I add my own icon?
This question ma be related with your problem. Please check it.
How to change the default icon on the SearchView, to be use in the action bar on Android?