New Blank Activity extends ActionBarActivity - android

I have a strange "problem" on my Android Studio 0.9.9, and still on the version 1.
Since this update, when i create a new project android studio doesn't create a blank activity. It should be "MainActivity extends Activity", but it's "MainActivity extends ActionBarActivity"
I have a second problem as well. When i'm editing Layout Files, on the Preview of the Layout, i can't see the ActionBar (But i don't have this problem when i'm editing previous project)
I really don't know where this problem is coming from. Maybe a problem in the configuration of Android Studio, but i didn't change anything. Or maybe it's just some new features.
I'm new in Android Development so it might be my lack of knowledge.
Has Someone had this problem before ? Any hint or advice on this?
Is my explanation lucid?
Thanks for your help !
EDIT :
After some search, i found some post from StackOverflow :
By default android creating new project as ActionBarActivity
Create New Activity adds an activity which extends Activity class instead of ActionBarActivity class in Eclipse
android adt bundle automatically extending ActionBarActivity and adding fragments and more stuff?

Update May 4th, 2015:
Starting with Support Library 22.1 (blog post):
In this release, ActionBarActivity has been deprecated in favor of the new AppCompatActivity.
Original Post:
Has Someone had this problem before ? Any hint or advice on this?
This is not a problem but intended to help you apply the new design language by Google, named Material Design. One major change is the new UI widget called Toolbar. To use it you have to extend from ActionBarActivity as mentioned in a recent blog post by the Android developers. In this blog post you will also find more information about the Toolbar widget.
So if you're new, Google is just trying to help you to get right on track with the new elements and styles.

In your resources xml file, your Action Bar style should have a parent. Something like:
<style name="MyActionBar" parent="#style/Widget.THE_STYLE_YOU_WANT"> //HoloDark, MaterialLight, ect
<item name="android:background">custom color</item>
</style>

i also feel strange for that it extends the actionbaractivity,some days before when i create a project and set minsdk to 14,it extends activity and use holo theme when under lollipop ,but today i do the same thing and found it didn't derive from the activity and use the appcompat theme only,indeed one new feature for appcompat library is that it Added support for material design user interfaces. maybe extends actionbaractivity is for the sake of using the newest theme,because under lollipop you can't use material theme,use appcompat is the quickest way, i don't know google's manner for the holo theme now.

Related

Why are Google's instructions for Material Design incorrect?

I have seen similar questions asked with solutions which solve my problem but I was wondering if anyone can explain it to me.
I am following instructions on the Google Developer website to apply the Material Design Theme located here. However, I am getting an IllegalStateException when I try to run the app on the emulator. My theme inherits from parent="android:Theme.Material" as specified in the instructions provided by Google.
The issue is solved by using Theme.AppCompat. Why is it that the instructions that Google provide leads to an error? I read that Theme.AppCompat extends Theme.Material, I don't know if that is true. Why can't Theme.Material cannot be used?
I tried following the line of extension for Theme.AppCompat but I didn't see it inherit from Theme.Material anywhere.
Why is it that the instructions that Google provide leads to an error?
The instructions assume that you are using Activity, not AppCompatActivity, as your base class for your activities. In your case, you are using AppCompatActivity.
I read that Theme.AppCompat extends Theme.Material, I don't know if that is true.
It is not true.
On Android 5.0+ devices, Theme.AppCompat will extend Theme.Material, indirectly. On older devices, it will not, as Theme.Material did not exist. Your error is coming from AppCompatActivity, which validates that you are using a theme that is based on Theme.AppCompat and therefore does not accept Theme.Material.
Probably you have one (or all) of two these problems:
1) You use only 1 xml file style.xml, where your wrote something like:
<style name="AppTheme" parent="Theme.Material">
And then you run app for device with api lower than 21.
Then you have problem, that devices with api < 21 don't know about your Material theme.
2) You use Material theme for api>=21 and AppCompat for api < 21, but your Activity extends AppCompatActivity, then you have a problem. Because AppCompatActivity requires AppCompat theme, not material
Please, have a look to this answer for more details
Also from official documentation page about AppCompatActivity:
Base class for activities that use the support library action bar features.
You can add an ActionBar to your activity when running on API level 7 or higher by extending this class for your activity and setting the activity theme to Theme.AppCompat or a similar theme.

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

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.

Best practice to bring Material look to Preferences using AppCompat?

What is the best way to bring Material design to the preferences using AppCompat?
I've been reading some questions and the main points seem to be:
The official guidelines suggest avoiding PreferenceActivity if working for API>11 (and I am). Instead one should use PreferenceFragments;
Using PreferenceActivity would be bad also because you can't extend both PreferenceActivity and ActionBarActivity;
You might use a general ActionBarActivity and load PreferenceFragment (API>11) into it. This way preference widgets loaded from resources gets tinted by AppCompat, but that's not true for any widget (see here );
With the suggested solution, anyway, the whole UI look isn't quite Material (see image on Android 4.2), even if inheriting from AppCompat theme.
Another way could be to extend a PreferenceActivity (API>1). As said, you won't extend ActionBarActivity but just inherit AppCompat theme.
Last, you could just make your own Activity extending ActionBarActivity, and deal with widgets, layouts, margins, SharedPreferences yourself.
At the moment I am not aware of a working solution. I've seen some discussion going on in a Chris Banes post introducing Material, but no solution was suggested. I'm asking:
Did some of you succeed in bringing a pure (as possible) Material look to preferences, and how?
Will there be improvements in v7 regarding this issue in the future?
I'm using AndroidDeveloperLB/MaterialPreferenceLibrary in my projects.
Easy to install. ImportEasy to use. Sample

How to activate control tinting on EditBox using new android support library v7

It seems that there is no way to activate color tinting on standard controls (EditBox and CheckBox) on KitKat with new android support v7 library (appcompact). I have tried everything.
I have followed instructions from offical android blog post to the letter:
http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
And I got everything working in short time without problems, except color tinting for EditBox and CheckBox.
I am not using some custom edit control (only standard android widgets), and I'm inflating all my layouts using LayoutInflater in fragments. I can only see effects of tinting with new 'SwitchCompat' widget. Oh, just to mention that I've set colorAccent in my theme.
Any ideas, what can I do to make it happen ?
After long struggle :) I got it to work !
Here is how:
All of your Activities must extend from ActionBarActivity, not from FragmentActivity, regardless if you are using actionbar or not, this is a must for tinting in support library.
Second, if you are using adapter for listview LayoutInflater for adapter must be created using themed activity as context, and for that matter any LayoutInflater that you are using must be provided by properly themed activity.

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.
}

Categories

Resources