"android" package name in styles v14 - android

I'm generating styles using Jeff Gilfelt’s ActionBar Style Generator. Browsing the styles.xml in the different \values folders I notice that in \values-v14\styles.xml, all the item names have the android package name in them. Can someone explain why?

You don't use the android prefix for support library compatibility when using the AppCompat library, but you do if you're styling the normal ActionBar, which in this case is API level 14+.
Styling the ActionBar

Related

Removing Sherlock Actionbar library Theme not found

I have removed the ActionBarSherlock library and adapted my code.
Now I get errors in my style.xml
<style name="Theme.MyTheme" parent="Theme.Sherlock.Light">
What do I have to use instead of the Sherlock.light theme?
If you are using AppCompat library, use AppCompat.Light
#style/Theme.AppCompat.Light
else Try using Holo.Light.
#android:style/Theme.Holo.Light
If you are migrating to appcompat-v7, you need to migrate all Sherlock references to their corresponding AppCompat equivalents. This includes switching custom themes to inherit from Theme.AppCompat (e.g., Theme.AppCompat.Light).
You might consider installing Android Studio, then creating a scrap project using the new-project wizard. The defaults will use appcompat-v7, and you can examine the newly-created project to see what it uses, to help you determine how to change your Sherlock references.

how does appcompat library styling work

I'm quite confused about how styling in appcompat library works.
According to here:
We now use the support implementation of Toolbar/ActionBar on all
platforms meaning that we no longer read any android: attributes
related to the action bar.
For apps which already have existing appcompat setups, this means that
you should remove your v14+ themes which re-set the same values in the
android namespace. Please note, this is ONLY applicable for
styles/widgets which affect the action bar.
For most apps, you now only need one theme declaration, in values/
So here is my question:
If I want to use material design ActionBar in API 14+, I can just use ActionBar/Toolbar provided in appcompat_v7 and style it in the common value/ folder with the "android:" namespace removed ? but why am I seeing people writing code below:
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
....
<item name="windowContentOverlay">#null</item>
<item name="android:windowContentOverlay">#null</item>
....
</style>
why is "android:" namespace there? what's the difference between the code above and using value-v21, value-v14, folders?
can someone explain or direct me to the right source?
Yes, if you use AppCompat v21+, you only need a single theme with a parent of Theme.AppCompat (or a subtheme such as Theme.AppCompat.Light) and you do not need the android: namespace attributes for action bar/window related flags, nor separate themes for v14, v20, etc. The full list of top level attributes which AppCompat provides across all API levels can be found in the AppCompat R.styleable Theme.
Much of the code on the internet (including parts of the developer.android.com site) are still written for the pre-v21 AppCompat, which did require both the android: and non-prefixed versions.

android:Theme.Material.Light requires API level 21 (current min is 8)

I want to use Material Theme in my application which has minimum sdk version of 8. As per docs - "The material theme is only available in Android 5.0 (API level 21) and above. The v7 Support Libraries provide themes with material design styles for some widgets and support for customizing the color palette." Does it mean I can use it if I add v7 Support Libarary in my project? Because after adding this library I got the following error:
android:Theme.Material.Light requires API level 21 (current min is 8).
Or maybe I understood something wrong? Any suggestion will be appreciated. Thanks in advance.
For this you need to have 2 values folders.
One that exists by default, and another, you have to create in your res folder and name it values-v21.
In the default values folder, in styles.xml, use a theme other than Material theme.
And in the styles.xml of values-v21 folder that you created, use Material theme.
Android phone will automatically pickup the styles.xml which it supports. If the phone supports Material Design (Lollipop devices), your app will use Material theme (values-21 folder).
If it doesn't (in phones running older Android versions), the default values folder will be used.
You need to use android:theme="#style/Theme.AppCompat.Light" theme to get a material design.
Make sure your min is 8 and your target is 21. And you're using build tools/sdk 21.
Pedro Oliveira is right with regards to Theme.AppCompat, but some essential information is missing in that answer.
A blog post titled appcompat v21: material design for pre-Lollipop devices! by Chris Banes from the Android team probably best answers the question of how to get Material Theme for API levels prior to 21.
To summarise, you need appcompat-v7 dependency:
dependencies {
...
compile "com.android.support:appcompat-v7:21.0.3"
}
After that, for dark version as your base theme, use:
<style name="AppTheme" parent="Theme.AppCompat">
</style>
And for light version:
<style name="AppTheme" parent="Theme.AppCompat.Light">
</style>
And if you're new to AppCompat, there are things you need to know, such as:
All of your Activities must extend from ActionBarActivity*. It extends
from FragmentActivity from the v4 support library, so you can continue
to use fragments.
*NB: more recently, ActionBarActivity has been deprecated in favour of AppCompatActivity.
But you really should read the whole Setup section of that blog post! (The information on Toolbar vs Action Bar, and some of the comments are also something you probably shouldn't miss.)
In your NameActivity.java file import the following:
import android.support.v7.widget.Toolbar;
Comment the previous one:
//import android.widget.Toolbar;
With this the problem is solved.

No resource found that matches the given name: attr 'actionBarStyle'

I am trying to change the android action bar style. The minimum API support for the app is 8. So as using android:actionBarStyle attribute in the style xml file in the values folder gives the minimum required API 11 error, I have commented it out and moved it to a separate style xml file in v11 folder.
However, when using the actionBarStyle attribute only for android 2.1 and higher as mentioned in the android documentation, it gives me the No resource found error. Below is the style code:
<style name="AppTheme" parent="android:Theme.DeviceDefault">
<item name="android:typeface">monospace</item>
<!-- <item name="android:actionBarStyle">#style/MyActionBarTheme</item> -->
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
I have tried changing the style parent theme from DeviceDefualt to Holo.Light.DarkActionBar, AppCompat.Light.DarkActionBar, etc. but the error still remains.
If you want to use the standard Action Bar on Android devices v7 appcompat library, which backports the Action Bar to any API 7+ device when you use ActionBarActivity. (Note, the Android documentation you found reference this same library, hence the 'When using the Support Library' comments throughout that document)
To add it to your project, you can follow the directions for adding a library with resources for whatever IDE you use.
Once you have the appcompat library added, you may consider using a tool like the Android Action Bar Style Generator which automatically builds the correct styles/resources needed to fully style your action bar (make sure to change the 'Style Compatibility' drop down to 'AppCompat').

Design for new API but preserve compatibility

This line style="#android:style/Widget.Holo.Light.Spinner" makes my minSdkVersion go from 1 to 11!
How can I design my views for newer versions but still display ugly views for older versions (but allowing them to get the app) ?
The spinner is not supported on old versions. You can create the specific style that uses the spinner on a folder named values-v11, and on the style default folder (only "values") design a "ugly" spinner. Note that the styles must have the same name on both folders.
Check this answer https://stackoverflow.com/a/15339215/799979
The Spinner was added in API 1. It's the style you are attempting to use that is API 11+.
To solve this, you put another styles.xml file in the values-v11 folder. Then you have the Spinner use a style from your styles.xml file. In the styles.xml file in the default values folder you have your new style inherit from the android style you want to use. In the styles.xml file in values-v11, you modify that same style to use something can be used in older APIs.

Categories

Resources