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.
Related
Can I still use ActionBar in AppCompatActivity? Are there any issues on using it from Android 6 to Android 4?
I know Toolbar is the new ActionBar, but if I still want to use ActionBar, is this is safe? The reason I don't want to use toolbar is because I'm already skinning the ActionBar.
Btw, I see a huge fragmentation in the API, almost confusing I would say.
There's Activity, AppCompatActivity and setActionBar, setSupportActionBar and ActionBar/Toolbar...
Using Toolbar will be a great option it can be highly customizable, which means you can add navigation buttons, logos, titles, subtitle, action menu items, or even your own custom views. If you still want ActionBar,You need to use getSupportActionBar() while using AppCompatActivity
You can also check customizing option which toolbar provide to that of actionbar
Simple Answer: You need to use getSupportActionBar() if you are using AppcompatActivity irrespective of what version your app is running.
Recommended: Should you choose to use the new Toolbar instead of ActionBar, because it's way too flexible.
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);
I am trying to hide action bar, but keep tabs.
I used following codes, but this is not working for Android KitKat (Tested on Nexus 5).
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
Does anyone know how to solve this issue out on Android KitKat?
ActionBar tabs are part of the ActionBar, you can't display the tabs without the ActionBar.
Instead, you have to use TabHost.
Perhaps you didn't yet try:
actionbar.hide()
I've got an app with android:minSdkVersion set to "4".
When I run this app on my HTC one X (4.0.3) I have a actionbar and everything is okay, but when I turn of my phone and return to the app the actionbar disappeared.
Is there an manifest command or something to always show the actionbar when possible?
This isn't allowed in my app because of the minSdkVersion.
ActionBar actionBar = getActionBar();
actionBar.show();
Thank you very much for your help!
You can use the Android Compatibility Library for this; it will provide your app with an action bar on older versions of Android, and then you can use getSupportActionBar(). It also provides you with an Activity base class you can extend; called ActionBarActivity which takes care of this sort of logic for you.
Alternately, you can check out ActionBar Sherlock which (to the best of my knowledge) provides a superset of the support library capability.
Call actionBar.show() in your Activity.onResume() method. Every time your app gets paused, it will call your ActionBar.
Just put this in your onCreate method of your activity :
Actionbar actionbar;
actionbar = getSupportActionBar();
THis should load the actionbar everytime the app is started
I want to create an ActionBar and a tabbed navigation like the google+ app.
I used this example as a starting point and now I have a great actionbar:
http://developer.android.com/resources/samples/ActionBarCompat/index.html
I've also included a ViewPager and a TabHost to have tabs and scrolling left/right Fragments.
What i need is to show the back arrow in version prior to honeycomb.
If I set getActionBar().setDisplayHomeAsUpEnabled(true), the arrow is automatically show in version >= honeycomb.
How can I do that in version prior to honey?
What I also want to have is Tabs like the google+ app.
This is how is my tab bar looks:
removed dead ImageShack link
...and this is what i want:
removed dead ImageShack link
I can't find any example to style tab bars like that one.
I would recommend you the ActionBarSherlock for ActionBar compatibility with Android <3. It is a better implementation that the example offered in the developers page.
Using this package, the back arrow will appear when you add to your activity the following line.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Regarding the Google+ tabs, I have found the widget ViewFlow very useful and looking exactly the same. Moreover, you don't have any backward compatibility issues. Have a look to the CircleFlowIndicator example.
You can use ActionBarSherlock and a ViewPager with ViewPagerIndicator to achieve the desired look. These libraries also work pre ICS.
As miguel.rodelas suggested, use getSupportActionBar().setDisplayHomeAsUpEnabled(true) for the home arrow. In onOptionsItemSelected you can catch it by the id android.R.id.home.
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
//use your custom xml view here
View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//your logic for click listner
setListenerForActionBarCustomView(actionBarView);
As stated, download the ActionBarSherlock and import it as a library here
https://api.github.com/repos/JakeWharton/ActionBarSherlock/zipball/4.2.0
Instead of using getActionBar use getSupportActionBar
Download the support library
Instead of Activity, extend your class as SherlockActivity
Same with fragments, use SherlockFragment
Then just copy your code, should work perfectly.