Android app title bar doesn't show when testing the app? - android

I'm creating an app, and when I build it and start it on my phone (a Sony Xperia Z1 with Android 4.4.4), the title bar doesn't show. All activities is rendered without the title bar, like this:
I'm using Android Studio, and in the editor, the title bar is visible. It just don't work when the app is being ran.
All help with this is appreciated!

Update to this answer. ActionBarActivity was deprecated as of version 22.1.0. To achieve this same outcome, extend AppCompatActivity.
ActionBarActivity API Class Reference

I just found the answer. Each of my Activity classes extended Activity, and when changing that to ActionBarActivity, the action bar (called "title bar" by me) shows up. I changed those values when I created the classes, because I "was sure" that in all my other projects, the classes extended just Activity, and not ActionBarActivity, but I "was sure wrong".
Thanks everyone for your suggestions!

I'm not on my computer, so I can't link it, but go to your styles.xml and make sure there is nothing that says NoActionBar.

Related

Is there a way to set up an app bar/action bar in an Android app without going through the appcompat library?

I'm having a "this can't be right moment" trying to learn Android app development. I want to add some buttons to the top bar (app bar, toolbar, action bar, etc) of my Activity. Apparently this thing is called the app bar. But when I read the docs, I feel like I'm taking crazy pills:
This class describes how to use the v7 appcompat support library's Toolbar widget as an app bar.
Why do I have to use something called "appcompat" to add a toolbar? I don't care about backwards compatibility right now, I'm just coding an app to learn. Normally backwards compatibility shims/layers are an optional thing. I just want to do this in the most straightforward way at the moment - it'll be easier for me to learn that way.
There are other ways to implement an app bar—for example, some themes set up an ActionBar as an app bar by default—but using the appcompat Toolbar makes it easy to set up an app bar that works on the widest range of devices, and also gives you room to customize your app bar later on as your app develops.
Again, this sounds like the docs are saying that the method they advocate is not the simplest and most basic way, and that there's another. Then one of the very first steps in the tutorial is:
In the app manifest, set the element to use one of appcompat's NoActionBar themes. Using one of these themes prevents the app from using the native ActionBar class to provide the app bar.
Huh? So the first step to getting a toolbar is to turn off the toolbar? At this point I feel like I can hear clown horns going off. Am I being pranked? I don't think Google realize how bonkers this all sounds to a complete beginner.
Is there a way to add buttons/actions to the default Material UI ActionBar in an Activity, without going through the appcompat package?

Can't use Holo themes with ActionBarActivity child (correct API chosen)

I'm going through the tutorial at developers.android.com and I had problems with styling the action bar. I use the newest SDK (the bundle with Eclipse).
Say, that in values-v14/styles.xml I have
<style name="MessageTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
I've tried all the variations of that that I could find. Tried without DarkActionBar in values-v11 as well.
It compiles fine but when I open activity styled as such, app crashes and logcat says
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Like I said, API is set correctly. The target one in project properties (API 19) and android:minSdkVersion="14" in the manifest (tried higher as well).
Now, my activity extends ActionBarActivity (that's how the file was generated). If I make it extend Activity instead, then Holo works fine. That's an answer I found, but I don't understand why that works. What exactly is the difference between ActionBarActivity and Activity that makes this works and is this some hack or is it supposed to be done this way?
Also, that works fine with my additional Activity. If I try to this with the main activity from the tutorial, it doesn't compile because 2 methods used there are undefined - getSupportActionBar and getSupportFragmentManager.
You are using a compatibility library, so to style a support actionBar you need your theme to be descendant of appCompat.
Try this:
<style name="Theme.whatever" parent="#style/Theme.AppCompat.Light.DarkActionBar">
If you are still a little lost, you can generate your theme with this tool: ActionBar style generator and take a look how it's done.
Edit:
Check this out, also: Styling the Action Bar
See "For Android 2.1 and higher"
About the difference between Activity and ActionBarActivity...
As far as I know, you extend ActionBarActiviy if you need to have an action bar while targeting lower than 3.0 android versions. That's why you are having troubles with actionBar or supportActionBar depending on what kind of activity you are coding.
So, to summarize, when working with Acivity call actionBar, but If you are extending ActionBarActivity you should call SupportActionBar. For instance: getSupportActionBar().
More info you could use: Support Library Features
Edit 2: Android is yelling at you because your are trying to use appCompat features. To avoid this in your particular instance, all you need to do is NOT extending ActionBarActivity, but coding regular Activities. Then use ActionBar features as normally you would do.
Edit 3 and probably last:
Let's guess you are using holo as theme, and you are coding a regular Acitivty for API 11 and above. In this case you are not extending ActionBarActiviy, so you don't have to do anything special. That's ok, right? but now, you want the same thing to work for API versions lower than 11 and here comes your real problem. To make that happen you must extend ActionBarActivity, but you didn't. So your only way out (as far as I know) is to have another activity that extends ActionBarActivity, and somehow detect with code, which version of android is running, in order to execute the right code (this is, which class you of the two you should take advantage of) so your app would be able to avoid crashing.
Thats why I think using only appComapt is a nice solution, assuming you don't really need to use holo. But, if you truly want to make things that way...
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
// Use a regular Activity class for API 11 and above.
}
else{
// Use an activity extending ActionBarActivity. Have in mind that here you would be calling a supportActionBar instead of a regular ActionBar.
}

android custom title bar in library

I created my own library with one activity and use it in another project. But when I create my own app theme with custom title bar for my activities in my project I have an issue. Activity from my library (of course) have default title bar. Any ideas how to solve it with the easiest way?
You could probably extend that Library's Activity and then set it manually. Check a similar situation somebody asked here

how do I update the Android options menu so the backward compatible actionbar updates?

I have made a android app with the "new" actionbar and fragments api but I wanted it to work on phones below android 3.0... so I tested this one:
http://developer.android.com/resources/samples/ActionBarCompat/index.html
and using the compatibility pack from google...
but I now realize that the setHasOptionsMenu(true) wont work...
to sum it up, I want to update the actionbar menu from runtime... I have tried updating the options menu but havent found a way that works... so please help me :)
Edit: One thing I have realized is that it isnt the setHasOptionsMenu(true) function that wont work, its the actionbar that doesnt update when I try to update the optionsmenu...

Get a specific Activity from TabActivity or TabHost

How can I get a specific Activity from TabActivity or TabHost?
I tried TabHost.getChildAt(), but a View instance can't be casted to an Activity instance.
Or how do you get an Activity which is running in background?
I couldn't find TabActivity.getLocalActivityManager().getActivity().
For me it worked using TabActivity.getLocalActivityManager().getCurrentActivity().
found the answer: just use TabActivity.getLocalActivityManager().getActivity()
I know this isn't what you're looking for, but I HIGHLY recommend not using TabActivity. In all the projects I've worked on, we wound up yanking it out and just using buttons that look like tabs.
I also think its deprecated in 3.0, and certainly Ice cream. They push the action bar. I'd look for one of the available action bar libraries.

Categories

Resources