Action bar app icon missing with Android 5 - android

Just rebuilt my app using the Android 5 SDK and associated appcompat.
Seems to work fine but my app icon is no longer showing in top left hand corner. The icon to open nav drawer is there but no icon.
Any way to fix this?

Use the code below in onCreate:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);

I too noticed that the default projects created by Android Studio were missing the icon in the action bar. Here's how I fixed it.
Disclaimer: This solution will result in in the Material theme being dropped in favor of the older JellyBean/Kitkat styles.
First, change themes setting in styles.xml from this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
To this:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
Now change all your Activities to inherit from android.app.Activity instead of android.support.v7.app.ActionBarActivity. That is:
Change this:
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActivityBarActivity {
To this:
import android.app.Activity;
public class MainActivity extends Activity {
The end of result of doing this is an app that has the Holo theme more commonly seen on Jelly Bean and Kitkat.

Make sure to extend ActionBarActivity rather than Activity. And add the following code:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);

You shouldn't have an icon in an API OS 5+ app. By default it is not displayed (and this is the preferred behavior). The Toolbar docs specifically state this:
"In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer."
http://developer.android.com/reference/android/widget/Toolbar.html

Related

No App Icon on ActionBar

There are a lot of queries here about adding icons to ActionBar but none solved my problem. If you know a duplicate of this question, feel free to comment or close this question.
I migrated my project to IntelliJ and I didn't encounter this problem with my previous IDE (Eclipse).
PROBLEM: The app icon is not displayed in the ActionBar.
I think it's supposed to be added by default that's why I can't add it through its XML
Here's its 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=".MainActivity" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>
Thanks!
As of AppCompat version 21, the Action Bar follows the material design guidelines and uses a Toolbar:
A title and subtitle. The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.
However, if you want an application icon, setLogo() is the correct method.
Update your onCreate() method with the code below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.activity_main);
}
NOTE: ic_launcher is the icon you want to display in your actionbar. To display it, add the icon in the drawable folder of your app project.
In your Style.xml file:
<style name="MyTheme_ActionBar" parent="#style/Theme.AppCompat.Light">
<item name="icon">#drawable/actionbar_logo</item>
</style>
In activity add this code:
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
If you don't care about the Material theme and are fine having an Activity that looks more JellyBean/Kitkat style and includes the icon in the Action Bar, you can do the following:
First, change themes setting in styles.xml from this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
To this:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
Now change all your Activities to inherit from android.app.Activity instead of android.support.v7.app.ActionBarActivity. That is:
Change this:
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity{
To this:
import android.app.Activity;
public class MainActivity extends Activity {
The end of result of doing both of the above steps is that the icon as specified by the android:icon attribute in AndroidManifest.xml will appear in the Action Bar.
This worked for me. Your onCreate method should have these lines:
ActionBar menu = getSupportActionBar();
menu.setDisplayShowHomeEnabled(true);
menu.setIcon(R.mipmap.imageFile);
Make sure the imageFile is a .png in the mipmap folder. To get the icon exactly at the start of your action bar, the mipmap folder should have multiple versions of the image file in all screen sizes: hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi, etc.
Setting Icon On the Action Bar
Showing Icon On the Action Bar Can Be Tricky
If you are extending Activity this should be enough:
getActionBar().setDisplayShowHomeEnabled(true);
If you are extending AppCompatActivity then additional code is needed:
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);

Theme.Holo with Dark Action Bar

I am making a daydream application and I want the settings activity to follow the same interface as the standard settings. For right now I want to make the app use the Holo theme but have a dark action bar like you can get with the Holo.Light.DarkActionBar theme. I've googled around and have not found anything similar, and I tried looking through the android source code to find out how they do it for the Settings page, but it is too big for me to handle and I cannot find the styling system.
I've got my styles set up but I cannot find the correct value to use for actionbar style. I do not have interest in adding more libraries like actionbar sherlock.
NOTE: Dark Action Bar =/= Holo actionbar.
My app only works with 4.2 and up so the Holo.Light.DarkActionBar theme should exist.
For reference, I want my app to have this theme:
The settings app uses the Widget.Holo.ActionBar.Solid for its actionBarStyle. As in:
<style name="Your.Theme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#android:style/Widget.Holo.ActionBar.Solid</item>
</style>

Custom Title bar with Holo Themes

Like a lot of people, I would like to use a custom title bar in android but also use the Holo theme. I've seen a lot of posts recommending using Theme.Holo.NoActionBar but it still gives me the same error as when I change my custom theme to use Theme.Holo. I want to resolve once and for all, is it possible to use a custom title bar like this:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_bar);
which seems to be the most common way to do it.
Well you can create another element layout which looks like title bar for you and in the current activity you can set requestWindowFeature(Window.FEATURE_NO_TITLE)
This way you get the holo theme aswell as custom title. This is commonly used practice in this scenario.
you can define parent="#android:style/Theme.Holo"
then just disable the windows action bar
like this
<item name="android:windowActionBar">false</item>
in theme.
It's working for me...

Android custom theme on 2.3, holo theme on 4.0

I read http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html but have some issues. For pre-honeycomb I want a custom title, for post-honeycomb I want the default. When I try to run my app on ICS
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
If I remove the custom title it works fine on all releases, just without the custom title.
in values-v11 I have themes.xml file with this content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme.Holo.Light">
</style>
</resources>
So my theme document says to use no custom theme basically.
If I remove
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
then the theme shows up correctly on ICS.
I cannot imagine that I have to check on coding level what API level I run and either request the window feature or not, that should be handled by the system.
Thanks, A.
Pretty similar to this question. I guess the action bar is considered a title feature, so turning it off gets rid of the exception.
I'm still a bit confused by the question though. You say you want the default title for post-honeycomb, but you can't use a custom title and also use the default actionbar. If you really want to do what you're asking (custom title for < 3.0, default actionbar for >= 3.0), then you'll need to check Build.VERSION.SDK_INT before calling window.requestFeature etc. That's how it's done in the actionBarCompat example that does just this sort of thing.

How To Enable Menu in a Custom Theme in Android?

I'm creating an Android app using the theme "noTitlebar" and all the design changes are done and approved. Now I need to add a menu, so I have to add title bar. But, if I change the theme, the whole design for pop up, border, etc. will be affected.
So I created a custom theme by extending the theme
"android:style/Theme.NoTitleBar".
I enabled the title by
<item name="android:windowNoTitle">false</item>
Now I got the title bar, but menu is showing up. The control is not coming to
public boolean onCreateOptionsMenu(Menu menu) {
How can we enable menu in this theme? Help will be greatly appreciated.
Since your are targeting Honeycomb (right?), when you say titlebar you probably mean Action Bar? Follow the link to read the docs.
In a nutshell:
You need to set targetSdkVersion="11" and your build target (in eclipse) to v11.
Just use Theme.Holo. Action bar is by default enabled. Theme.Holo is a default theme for v11 so you even don't need to define it.

Categories

Resources