appcompat 21 logo as back button - android

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);

Related

Logo not showing in Theme.AppCompat.Light.DarkActionBar style [duplicate]

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);

Subtitle below logo on actionbar android

I'm trying to place a logo that will act as the actionbar title and then have dynamic fragment based subtitles with text below.
Can you either set text below logo or the title as an image?
Is it possible?
Thanks!
ActionBar does not provides APIs for setting title as an image, you could try find the actual TextView in layout but I would not recommend that due Android fragmentation. It will not work on all devices.
You can however use a custom view in ActionBar, you will have to call
getActionBar().setDisplayShowCustomEnabled(true);

how to show divider between icon and title in actionbar

I'm using ActionBar. I want to add splitter between icon and title: For example like here:
Nothing from these answers helped me:
Android actionbar sherlok doesn't show divider
ActionBar Divider Styling
ActionBar MenuItem Divider
I've tried to apply style to ActionBar but Eclipse shows this:
android:showDividers requires API level 11 (current min is 9)
What should I do?
the easiest way is to add divider logo. If later you want it is not displayed click, just make its selector and put a logo without the divider for statepressed but with the same dimensions.

Action Bar remove title and reclaim space

I am trying to remove the title from my action bar and reclaim the space it would otherwise consume for the use of menu items.
I have tried using a custom theme but so far nothing has worked. I have tried changing the text size to 0sp, changing the color to transparent, and setting visibility to gone.
I have also tried this :
ActionBar actionbar = getActionBar();
actionbar.setDisplayShowTitleEnabled(false);
I am targeting sdk 15+ and using the standard Android action bar (i.e. not Sherlock).
Thanks for any suggestions.
Here's what I'm talking about (the area bordered in red I want back):
Did you check out the ActionBar documentation yet? There's a method, setDisplayShowTitleEnabled(boolean) that does what you need.
Simply get a reference to your ActionBar, and call that with false:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);

Change logo that appears in Android ActionBar programatically

Is there a way to change the logo that appears in the ActionBar inside an Activity instead of listing it in the Manifest?
The reason I ask is I have an app for phones where I build a similar type of bar across the top with the launcher icon in the left corner similar to the ActionBar using an ImageView.
I also allow the user to change that image to one on their phone or on the internet. I am currently making the app work for tablets and would like to just use the ActionBar instead but can't figure out how to change the image based on the user. There is no method in the ActionBar class and I could not find a way to modify the icon or logo from the resources.
Anyone know if this is possible or could suggest something I could try?
Prior to the introduction in API 14 of the setIcon and setLogo methods that Jake mentions, you appear to be able to change the ActionBar logo by grabbing the view of android.R.id.home and setting a new drawable.
ImageView logo = (ImageView) findViewById(android.R.id.home);
logo.setImageDrawable(drawable);
API 14 introduced setIcon and setLogo methods.
As for 11 through 13 there's no easy way.
You could possibly use a custom navigation and hide the regular icon/logo by having your own layout mimic its functionality.
There is a homeLayout attribute in the action bar style which allows specifying an alternate layout resource to be used but it will always throw a ClassCastException since it is instantly cast to an internal class upon inflation (bug #21842).

Categories

Resources