requestFeature ActionBar doesn't add ActionBar - android

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.

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

ActionBarSherlock with translucent tab navigation

In my app I have ActionBar with tabs and I want to make tabs translucent, but not ActionBar. Is it possible to achive this via styles and themes?
You can use custom layout and inflate it to use it as tabs
Please find it here : https://stackoverflow.com/a/10013297/951045
and to hide actionbar use
getSupportActionBar().hide(); // if you are using support lib
otherwise use getActionBar().hide();
You can also hide actionbar by using Theme.Sherlock.NoActionBar (for support lib)

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

How to to hide action bar (Theme.Holo) if Android version below 3.0

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.

Android: actionbar dissapeared after returning to the app

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

Categories

Resources