trying to implement toolbar in android - android

I am trying to get rid of my actiobars and use toolbar to update my app. I am not making my apps for 5.0 yet, so no other material goodness.
From following another post I made my themes.xml look like this:
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="colorPrimary">#color/mycolorprimary</item>
<item name="colorPrimaryDark">#color/mycolorprimarydark</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
The first issue I am having is that "Style" is throwing this error:
Element Style must be declared

Theme vs Style
So what exactly is the difference? Well they are both declared in exactly the same way (which you already know), the difference comes in how they’re used.
Themes are meant to be the global source of styling for your app. The new functionality doesn’t change that, it just allows you to tweak it per view.
Styles are meant to be applied at a view level. Internally, when you set style on a View, the LayoutInflater will read the style and apply it to the AttributeSet before any explicit attributes (this allows you to override style values on a view).
Values in an attribute set can reference values from the View’s theme.
Themes are global, styles are local.
From theme-vs-style
I would recomend a read at the above link.

Related

Setting name attributes in material design

I have a simple question, but I really don't get it. When we set a new style under material design, we use parent styles as follow:
<style name="AppTheme.Base" parent="Base.Theme.AppCompat.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
However, in some tutorials, I saw they insert the reference "android:" into the attribute, e.g.:
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
I know that the latter should be used for referencing every attribute in android, but I don't understand why in this case it is sometimes omitted and sometimes not.
It is omitted if you are using the Android V7 Support Library. In this case you use one of the Theme.AppCompat themes, instead of the Android theme. Take a look at Maintaining Compatibility.
It is depending on which compatibility they want:
using android: prefix they refer to a platform attribute, that is it will be effective only on devices where the attribute exist (e.g. API14+, API21+ etc)
using without android: prefix they refer to an Appcompat Library's attribute linked with the app project, that is it will be effective on all devices with API 7+

Defining styles in Android and need to be inheriting from a parent

I am a little confused about how I should be writing my styles.
I have written some styles and they appear to work great but I am unsure if I should be inheriting from a style.
For example, by default, a text view (for example) has a default style before applying mine?
So I should be inheriting from something else in my style before applying it, i.e. a halo style?
So, for example, I designed the following style
<style name="TestMe">
<item name="android:textSize">30sp</item>
<item name="android:textColor">#FFFF0000</item>
</style>
Which I have applied to a few text views, seems to work great but should I be doing
<style name="TestMe" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textSize">30sp</item>
<item name="android:textColor">#FFFF0000</item>
</style>
Inheriting from a parent, if so which one?
If I apply a style to my text view and do not inherit, in effect is the text view losing a lot of styles that were predefined on it before applying my style? I know that I have an app theme that inherits from a parent and this is applied in the androidmanifest.xml. So adding a style doesn't override the theme, which in essence is a style?
Or is inheritance on styles only being used when I want to override something?
Be aware of the precedence order of different styling techniques — if you’re trying to style some text and not seeing the results you expect then your changes are likely being overridden by something higher up in this hierarchy:
Here the whole detail regarding text appearance URL

ActionBarSherlock mirrod attributes -- when to use "android:" prefix?

When styling ActionBarSherlock I was wondering when I have to use the prefixed attribute, when the non-prefixed attribute, and when both. For example:
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
I found this explanation on the ActionBarSherlock website:
Mirrored Attributes
Due to limitations in Android's theming system any theme customizations must be declared in two attributes. The normal android-prefixed attributes apply the theme to the native action bar and the unprefixed attributes are for the custom implementation. Since both theming APIs are exactly the same you need only reference your customizations twice rather than having to implement them twice.
The easiest way to convey exactly what this entails is with an example. The following is the full theme from the “Styled” example mentioned above:
<style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="background">#drawable/bg_striped</item>
<item name="android:background">#drawable/bg_striped</item>
<item name="backgroundSplit">#drawable/bg_striped_split</item>
<item name="android:backgroundSplit">#drawable/bg_striped_split</item>
</style>
I thought, non-prefixed attributes only have to be used for attributes, that did not exist before API level 11. But why is there an android:background as well as a background attribute in the example? android:background exists since API level 1. Can someone please give some more details about these mirrored attributes?
From what I understand of the ActionBarSherlock documentation you quote, the android-prefixed attribute (which you would have been the only one to set if you used the "normal" ActionBar) is used when ActionBarSherlock uses the native version of ActionBar (that is, on devices running Android 3+, where it's available), and the non-prefixed version is used on older versions, when ActionBarSherlock actually has to use its own implementation of the ActionBar component.
In short, android-prefixed attributes are used by Android native features, and non-prefixed versions are used by custom components.
Anyway, it looks like you always have to set both prefixed and non-prefixed attribute when theming an ActionBarSherlock object.
Simple rule is anywhere you are inheriting from parent="Widget.Sherlock.etc" then you should have dual attributes.
The exception being direct styles like Text and Button don't as you are only passing your style to that TextView/Button directly, I would however always inherit from the parent/current style defined in the abs__styles.xml, that way you will always get the correct spacing etc..

android: How can I define custom colors, drawables, etc. in themes?

I hope I can explain what I'm after. In essence, my users have asked me to allow different looks in my application, which I hope I can do with themes.
I hoped I could do something like this:
<style name="NewTheme" parent="android:Theme.Dark">
<item name="labelColor">#f90</item>
<item name="buttonColor">#fff</item>
<item name="buttonBg">#drawable/button</item>
</style>
<style name="OldTheme" parent="android:Theme.Dark">
<item name="labelColor">#fa0</item>
<item name="buttonColor">#88f</item>
<item name="buttonBg">#drawable/button_old</item>
</style>
And then reference these values in my styles.xml:
<style name="labelStyle">
<item name="android:textColor>#labelColor</item>
</style>
<style name="buttonStyle">
<item name="android:textcolor">#buttonColor</item>
<item name="android:background">#buttonBg</item>
</style>
I know this syntax is wrong, but what might be the right syntax? Basically, I want to create sets of attributes (color, background, a couple other things) and select them based on theme.
To work with themes and styles in Android you have to:
Define one or more themes in themes.xml and set the definitions of
your styles there.
Define custom attributes, a.k.a. custom styles, in attrs.xml.
Describe what the values of your custom styles are in styles.xml.
In your layout files, give your views a style attribute, which has a
custom style name as their values.
Set the theme of your application or activity in either
AndroidManifest.xml or in the Activity's onCreate(). This is done by calling setTheme() in the activity's onCreate() method, before any call to setContentView().
To change the theme, you simply need to restart your activity.
Iadvice you to look at this tutorial it deals with all that a programmer want to work on android themes (text color, text formatting, state list drawable etc ...)

Finding all available styles defined by android platform themes

In several android styling tutorials parent style elements are used that i can not find in the android.R.style.* styles (http://developer.android.com/reference/android/R.style.html).
A few examples are from the http://android-developers.blogspot.com/2011/04/customizing-action-bar.html action bar article. Nick uses parent styles like:
<style name="MyDropDownNav" parent="android:style/Widget.Holo.Light.Spinner.DropDown.ActionBar">
...
</style>
or
<style name="MyActionBarTabStyle" parent="android:style/Widget.Holo.Light.ActionBarView_TabView">
...
</style>
Where do these parent styles come from? Is it possible to list all available parent styles?
Thanks.
As stated in the "Applying Styles and Themes" article:
The Android platform provides a large collection of styles and themes
that you can use in your applications. You can find a reference of all
available styles in the R.style class. To use the styles listed here,
replace all underscores in the style name with a period. For example,
you can apply the Theme_NoTitleBar theme with
"#android:style/Theme.NoTitleBar".
The R.style reference, however, is not well documented and does not
thoroughly describe the styles, so viewing the actual source code for
these styles and themes will give you a better understanding of what
style properties each one provides. For a better reference to the
Android styles and themes, see the following source code:
Android Styles (styles.xml)
Android Themes (themes.xml)
These files will help you learn through example. For instance, in the
Android themes source code, you'll find a declaration for <style
name="Theme.Dialog">. In this definition, you'll see all of the
properties that are used to style dialogs that are used by the Android
framework.
For "android:style/Widget.Holo.Light.ActionBarView_TabView" see:
http://developer.android.com/reference/android/R.style.html#Widget_Holo_Light_ActionBar_TabView
...and note the API level it was introduced at!
For "android:style/Widget.Holo.Light.Spinner.DropDown.ActionBar" try:
http://developer.android.com/reference/android/R.style.html#Widget_Holo_Light_DropDownItem_Spinner
The latter is just my best guess - I can't get the list to actually drop down.
Parent attribute is a optional one while creating your own themes or styles.
Basically parent attributes are used for inheriting the styles which are already defined in the platform itself.
If we wish to inherit a theme or style and overwriting some features, we can use parent attribute.
Here's a link for the all available platform defined styles.
And one more tip :
If you want to inherit your own style that you created by yourself means follow this example to inherit.
<style name="MyDropDownNav.Red">
<item name="android:textColor">#FF0000</item>
</style>
Here is Official Google's Documentation for Default List of Themes and Styles
Google Android Documentation
You can Find here in this documentation
Android Styles (styles.xml)
Android Themes (themes.xml)
I know the answer to above question finishes over here still giving way of implementation, as it will be helpful for novices..
How to Implement them
This is what i got from the themes.xml
<style name="Theme.NoTitleBar">
<item name="android:windowNoTitle">true</item>
</style>
Now to implement it in my project
I have to add the following line in my Styles.xml file
<style name="AppTheme" parent="android:Theme.NoTitleBar" />
In this I am creating my own theme named "AppTheme" which will inherit its properties from already defined them by google. so I am mentioning it in parent
By looking into themes.xml and styles.xml will not only help to get the list of the already defined themes and styles but also help novices to have an idea how to create the themes.
As the theme I am mentioning here in parent is already defined by android so we will use it by prefixing the android: to theme name ..
If You want to inherit your created theme then
<style name="Theme2" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
then just provide the style name to the parent, here i am using Apptheme as parent theme for theme2

Categories

Resources