I followed up the instructions of building a new android project and I got a runnable one except a problem with action bar. The problem is that the application icon is not showed beside the application title on action bar. I created the project with the configuration below:
Minimum required SDK:API 8: Android 2.2(Froyo)
Target SDK:API 21:Android 4.X(L preview)
Compile With:API 21:Android 4.X(L preview)
Theme:Holo Light with Dark Action Bar(Eclipse set the corresponding appcompat theme)
Android Support Library 21.0.1
Android SDK Build-tools 21.1.1
Because my minimum sdk is api 8 which does not support action bar, so the project includes a appcompat_v7 library to allow the action bar feature. If I set the minimum sdk to api 14(android 4.0) or higher, then the project does not include appcompat_v7 library and application icon is showed successfully also. But I need my app to support older android os as low as api 8. So what should I do to fix this problem? Really appreciate you guys attention.
P.S: I went through the task above on windows ,mac, eclipse , android studio and got the same result.
You are using the AppCompat version 21+ and it is normal.
The Action Bar follows the material design guidelines and uses a Toolbar.
As you can read here:
The use of application icon plus title as a standard layout is
discouraged on API 21 devices and newer.
If you would like an application icon (but I discourage it), you can use the method setLogo().
Something like this:
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.my_logo);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
This issue comes when you use support library revised 21.
Use:
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
It worked for me or you can use toolbar. A Toolbar is a generalization of action bars for use within application layouts.
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.
Reference: https://developer.android.com/reference/android/support/v7/widget/Toolbar.html
Make sure you have the icon set in the manifest.xml file, in the application tag as:
android:icon="#drawable/launcher_icon"
Then in the onCreate method insert the following lines:
ActionBar ab =getSupportActionBar();
ab.setDisplayShowHomeEnabled(true);
ab.setIcon(R.drawable.launcher_icon);
This is a common "problem".Before you pull your hairs out make sure you are using:
import android.support.v7.app.ActionBar; //or the v4, haven't tried with that though
and not:
import android.app.ActionBar;
and then:
ActionBar actionBar;
actionBar = getSupportActionBar();
instead of:
actionBar = getActionBar();
And finally:
if (actionBar != null) {
// enabling action bar app icon and behaving it as toggle button
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.drawable.your_icon);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
Attribute logo is only used in API level 11 and higher (current min is 8), I was also confused about this question, maybe google just don't want the icon to show on material design, when the minimum sdk is set to 14 or higher and under 21,it uses holo theme, it has an icon, but appcompat style is more like material design I think, maybe google just forget to modify the holo theme
IN Style.xml
<style name="MyTheme_ActionBar" parent="#style/Theme.AppCompat.Light">
<item name="icon">#drawable/actionbar_logo</item>
</style>
In activity add this and try
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP |
ActionBar.DISPLAY_SHOW_CUSTOM |
ActionBar.DISPLAY_SHOW_HOME);
Related
After update sdk to 21 version logo doesn't display. I display logo using following code:
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.drawable.ic_launcher);
The code looks:
http://i.stack.imgur.com/fAWIx.png
This code:
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.drawable.ic_launcher);
Looks:
http://i.stack.imgur.com/hkYqa.png
This code:
actionBar.setDisplayHomeAsUpEnabled(true);
Looks:
http://i.stack.imgur.com/Ssw2A.png
My logo doesn't display as back button.
How me make old style as here? http://i.stack.imgur.com/BKOz4.png
Note: Sorry, I didn't notice similar questions. =(
Per the Toolbar documentation:
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 indeed the correct method.
Here's what worked for me if using an icon
actionBar.setIcon(R.drawable.ic_app_icon);
actionBar.setDisplayShowHomeEnabled(true);
Or, if you want a wider logo
actionBar.setLogo(R.drawable.ic_app_logo);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
I'm using the following code to try and add an ActionBar to my Activity which uses Theme.Holo.NoActionBar
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main_activity);
ActionBar actionBar = getActionBar();
actionBar.show();
I get an exception at the last line saying actionBar is null. I'm targetting API 19 but running on API 18. Any ideas?
You should use a theme that enables the use of the ActionBar in your activity (such as one of the Theme.Holo... theme versions that DO include activation settings of the ActionBar, as an example) and control whether the action bar is shown or not, by alternating the use of ActionBar.Show()/Hide() methods.
I am currently working on an application using the Holo theme and am targeting the minSdkVersion = 9. I have an action bar with tabs throughout the entire application that work from version 11+ and am wondering the best way to use backwards compatibility to get versions 9 & 10 to function correctly.
Thanks!
You can use ActionBarSherlock. This will give you a backwards compatible ActionBar and ActionBar tabs.
I'm using the actionBar compatibility api for my app.
I'm trying to show the up button at the left of the app icon. I know how to do that with the original ActionBar class, but I can't figure out how to do the same with the ActionBar Compat
(I don't have any method like setDisplayHomeAsUpEnabled(true) in the original ActionBar class).
Someone knows how to do this? Thank you very much.
You can't use the ActionBar with the Android Compat library, use ActionBarSherlock for ActionBar on API 10 or lower.
I notice the action bar (Theme.Holo) looks cool only in android 3.0 or heigher, but it doesn't like nice on for example Android 2.3.
is there away to show the action bar ( Theme.Holo) based on Android version?
The action bar requires API 11 minimum. If you want to remove it programatically though you can use hide() in the code for that activity.
ActionBar actionBar = getActionBar();
actionBar.hide();
Not really sure what it would show for anything below 3.0, but it seems like the best place to start.
Edit:
You can check the SDK version at run time with the Build constants
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { //checks if its lower than Honeycomb
ActionBar actionBar = getActionBar();
actionBar.hide();
}
That is because the ActionBar wasn't added until Android 3.0 (API level 11) so any version of Android lower than that will have a title bar, not an action bar.
The ActionBar is not yet a part of the official compatibility library, so in the mean time, the ActionBarSherlock library will allow you to have an ActionBar that looks the same across all API versions.
If you want to hide the ActionBar completely just add adding android:style/Theme.Holo.NoActionBar to the activities in the manifest.