Adding An Action Bar To Theme.black.notitlebar Android
Solution 1:
From the documentation you linked to:
If you have a custom activity theme in which you'd like to remove the action bar, set the
android:windowActionBar
style property tofalse
. However, if you remove the action bar using a theme, then the window will not allow the action bar at all, so you cannot add it later—callinggetActionBar()
will returnnull
.
Since you're using a theme without an action bar, getACtionBar()
is returning null
, and then you're attempting to call show()
on that null
, resulting in an Exception being thrown.
So that explains the error you're getting. As far as what to do about it, the documentation for ActionBar says:
Beginning with Android 3.0 (API level 11), the action bar appears at the top of an activity's window when the activity uses the system's Holo theme (or one of its descendant themes), which is the default. You may otherwise add the action bar by calling
requestFeature(FEATURE_ACTION_BAR)
or by declaring it in a custom theme with thewindowActionBar
property.
That gives you two easy options to have an ActionBar without using a Holo theme. This is probably simplest in your case:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR); // Add this line
setContentView(R.layout.inbox);
ActionBaractionBar= getActionBar();
actionBar.show();
}
Update: You should also switch to just Theme.Black
without the NoTitleBar
part, as that prevents the ActionBar from working.
Solution 2:
I ran into the same problem. I eventually came up with a reasonable solution. I have a min api version 8 with a target api of 11. I don't like the holo styles either so I override the styles like the following:
First, you have to create a "res/values-v11" folder. You probably already have a "values" folder, just create a "values-v11" at the same level (doesn't matter what your target API level is...aways use "values-v11").
Once that is created, you create an XML document containing your theme in each of the folders. The original values folder will be used for anything less than android 3.0 (< api 11). Your theme would look something like this.
<?xml version="1.0" encoding="utf-8"?><resourcesxmlns:android="http://schemas.android.com/apk/res/android"><stylename="Theme.Black.OptionalActionBar"parent="@android:style/Theme.Black.NoTitleBar"><itemname="android:windowNoTitle">True</item><itemname="android:windowActionBar">False</item></style><stylename="OptionalVisibility"><itemname="android:visibility">visible</item></style></resources>
Notice that I am NOT using the Holo theme but instead using the Theme.Black.NoTitleBar theme as the parent. From there, I set the options to hide the window title and action bar for things lower than api 11.
Then, I create a second theme document in the "values-v11" folder and set it up like this:
<?xml version="1.0" encoding="utf-8"?><resourcesxmlns:android="http://schemas.android.com/apk/res/android"><stylename="Theme.Black.OptionalActionBar"parent="@android:style/Theme.Black.NoTitleBar"><itemname="android:windowNoTitle">false</item><itemname="android:windowActionBar">true</item><itemname="android:actionBarStyle">@android:style/Widget.Holo.ActionBar</item></style><stylename="OptionalVisibility"><itemname="android:visibility">invisible</item><itemname="android:height">1dp</item></style></resources>
In the above theme, it enables the action bar and sets it up with the Holo theme, but leaves all the other elements alone. So the action bar looks "holo"-ish but everything else stays exactly as it appears in the lower versions (and I don't get the ugly text entry that comes with Holo).
Then in the manifest if I want to have the actionbar show up, I just set the theme for that activity to android:theme="@style/Theme.Black.OptionalActionBar"
I did this specifically because I failed on my layouts by relying on the, now deprecated, hardware menu button. With the use of the themes above, I can setup the action bar if it is required or leave it as menus if it is an older device. Then, all I have to do is set a single property on my menu creation methods to support 2.0 through 4.0 seamlessly.
At any rate, take a look and see if this gets you to where you need to be.
In my mind it is a lot easier than the other approaches because you don't have to remember to request the actionbar or do anything in code. You just set your theme on your activity...and off you go.
Solution 3:
What Darshan Said ... kind of
To go a little further I would check out ActionBarSherlock, in combination with the support library you can then add the action bar to pretty much any version of android that is 2.x+
To get a full screen / no title bar you would create your own theme from one of the ABS theme's similar to
<stylename="Theme.MY.Sherlock.Fullscreen"parent="Theme.Sherlock.Light.DarkActionBar"><itemname="android:windowFullscreen">true</item>;
</style>
Applying that theme in your onCreate and then getting the action bar to show is the same (except you would use the ABS support prefix ) so basically
setTheme(R.style.Theme_MY_Sherlock_Fullscreen);
setContentView(R.layout.mylayout);
getSupportActionBar.Show();
I am assuming you can do the same for a standard action bar (using the holo theme as a base) since ABS is pretty much a duplicate functionality wise (and much of the source, ABS is mostly a wrapper class) it even detects and then uses the native implementation on 14 and above (11 and in-between has some missing features / fixes )
You can take a look at ABS here and it looks like he just updated it to JB
-- In your resources folder under values create a themes.xml file ( /res/values/themes.xml )
Then put this in there and give the above a try, but of course in setTheme( replace the name with Theme_My_Holo_FullScreen and dont use the getSupportActionbar(). prefix, I never bother using the native implementation but it should work the same.
<?xml version="1.0" encoding="utf-8"?><resources><stylename="Theme.My.Holo.Fullscreen"parent="Theme.Holo"><itemname="android:windowFullscreen">true</item>;
</style></resources>
Post a Comment for "Adding An Action Bar To Theme.black.notitlebar Android"