I've perused ALL of the questions here and countless tutorials on Styling and Themes for Android. The odd hierarchy between Themes and Styles is still a mystery to me.
My Manifest calls out this "Style" (android:theme, "#style/rforderstyle") [this is formatted from the manifest editor in Basic4Android] (not my choice but its what I have to use.).. however the manifest is still accessible should I need to.
I mention this because I've noticed that you can style a specific activity. I actually don't see a way to force have multiple "wigets" have their own "theme" while they all exist on the same activity.
Please advise if so then I my need to make my "little creations" as whatever "fragment" or "wigets".
What I'm attempting is a windows mobile app type form with a ton of User controls on it..so this set at the bottom happens to use a mid sized black text font.. but the control over here in the right upper is a different text size and color on & On & on ...
So regarding the style xml file, I've read that,,, its important that,,,, my parent be some all encompassing Android theme and I override individual "properties" in this section::
Could that mean that although I'm concerned with text size, since I have labels and editcontrols present the absence of a "parent" style that addresses these will cause me an error as I attempt to use this referenced parent style ??
How does a theme get into this mix...?
<resources>
<style name="rforderstyle" parent="#android:style/TextAppearance">>
<item name="android:textSize"> 21.0dp</item>
<item name="android:typeface">serif</item>
<item name="android:textColor">#FF9900 </item><!--#00FF00-->
</style>
</resources>
This is oddly confusing because the best examples reference layout files for the views but I don't really have layout files in Basic4Android I'm programmatically creating all of these views. If I don't need the depth of theme and style then fine but ...is the manifest supposed to point to a Style or a theme?
Related
I am quite familiar with Android layouts and the fact you can declare an app wide theme in the manifest or indirectly via the Styles resource such as "Halo", or "Material", etc... .
I also know that style can be done on a per layout basis. Android Studio "Layout Editor" even allows you to select a theme for the layout.
Question:
Where is the selected theme stored when applied per layout?
The Android Documentation seems to suggest its in the manifest where the activities are declared. For example:
<activity android:name=".MainMenu" `android:theme="#android:style/Theme.Holo.NoActionBar"/>`
However, when I select a different theme, even after I compile and run the application I see no changes in the manifest, or respective layout, or the Activity sub class.
So where is the association between a layout and the chosen theme for it being stored?
tl;dr
AndroidManifest.xml references styles.xml which in turns references colors.xml. The layout editor modifies styles.xml, you're not seeing the results because of possible overrides in styles.xml.
-- Modify the parent style in styles.xml and removes unwanted overrides within. --
After investigating this issue as I was also coming across this problem, here is the conclusion that I came to:
Starting with a new "Empty Activity" you will see the following in your AndroidManifest.xml file: android:theme="#style/AppTheme". That is not directly related to changes in the Layout Editor in the "Design View" of your current activity.
AndroidManifest.xml
Hovering over the theme definition you will this below:
styles.xml
Clicking on it will take us to styles.xml. By default, you will see the following:
This is where AppTheme is coming from that is being referenced in the AndroidManifest.xml. As you see, Theme.AppCompat.Light.DarkActionBar is the parent style, and if you want to change the overall theme you will need to modify that, e.g. "#android:style/Theme.Material" or "#android:style/Theme.Holo".
It is worth noting that as long as the override colors are still there, you will not notice much changes when the app is launched, even though the changes did go through.
To see the changes commend the following lines:
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
By deleting or commenting:
<!-- <item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>-->
Theme Layout Editor
For some reason Android Studio 2.2 got rid of the refresh button on the layout causing many difficulties in the update of the style.
For the time being, manually override the parent style in styles.xml and remove any overrides that are being imported from colors.xml. Not ideal, but it's a solution.
For a full list of themes you can check this developer R.style link or by clicking on Open Editor to get a visual representation of the different themes.
Which displays the following:
I believe the answer to this question is that Android Studio does not currently modify anything when you select a layout in the preview, it is just a preview and that is it.
To make the change one has to manually edit the theme for the activity entry in the manifest.
I'd like to anticipate that I'm very noob in programming.
I'm just starting.
I'd like to ask for help to make this button image working. I've already done a research here and within the forum but I couldn't find a similar case.
I'm trying to modify an app and change the buttons
I've created a holo themed button using http: / /android-holo-colors.com
This tool created the following structure.
structure
To achieve the goal I'm modifying the activity layout xml where I put the button
<Button
android:id="#+id/buttonGameOverPlayAgain"
android:text="#string/popupGameOverButtonPlay"
style="??????" />
Here I'm not sure if I need to refer to the style.xml or the styles_apptheme.xml created by the tool
and where to I need to declare the button?
also in the styles_apptheme XML there are two styles
<style name="ButtonAppTheme" parent="android:Widget.Button">
<item name="android:background">#drawable/apptheme_btn_default_holo_light</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
<item name="android:textColor">#000000</item>
<style name="ImageButtonAppTheme" parent="android:Widget.ImageButton">
<item
name="android:background">#drawable/apptheme_btn_default_holo_light</item>
To which one do I need to refer? "ButtonAppTheme" or "ImageButtonAppTheme"
Hope it's understandable, thanks everyone in advance for the patience and the support.
How to access styles
Styles are resource values and as such their value can be accessed all the same, despite the name of the file they are declared in, as long as it is in /res/values/ folder of your app. Thus it will be all the same if you have your style declarations in two files styles_apptheme and styles or you merge them in one.
You access the style with its name, not needing to know in whch exactly file it is declared. Thus even with your current file structure you address ButtonAppTheme with style="#style/ButtonAppTheme".
What are styles
Teh styles declare reusable sets of view attribute values. Declaring a single style like e.g. your ButtonAppTheme allows you to use the set of attributes declared in this style sin multiple places without the need of copy-pasting and thus sparing you the need of modifying on multiple places if you decide any of the attrbutes needs a change.
How to use them
Where to place the button - anywhere in your application layouts, where you decide you need to use such a buttlon
Which of the two style sshould you use - one of them is meant for ordinary buttons, the other is for ImageButton. The first is a button with text label, the second is imege-only button supported by Android. Use whichever of the two is more appropriate for each of your cases. I can speculate that you are interested in plain Buttons.
I'm not sure what is the best way to develop interfaces in Android.
is it better to clean the layout file by moving inline attributes to a style file? As far as I know, in HTML it is better to use classes and ids inside HTML, and use them in style.css file. What about android?
I found this, maybe it helps someone else.
When to use Styles
The first problem we must address is a simple one: when should you use a style instead of inline attributes?
Rule #1: Use styles when multiple Views are semantically identical.
This rule is best illustrated with a few examples:
You're creating a calculator. Each button should look the same, so it makes sense to create a `CalculatorButton` style.
You've got a couple screens with multiple text formats - say, headers, subheaders, and text. You can unify their look by creating `Header`, `Subheader` and `Text` styles.
You've got thumbnails all over your app. You want them all to look the same. The `Thumbnail` style is born.
The common thread in all these examples is that these Views are not just using the same attributes - they play the same role across the app. Now, when you want to tweak the look/feel of any of these Views, you can just edit the style and change them all at once. It saves you time, effort and keeps your Views consistent.
Want to save even more work? Use resource references!
Rule #2: Use references within styles when appropriate.
You could define a style this way:
<style name="MyButton">
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">48dp</item>
</style>
What if you wanted minWidth to vary based on screen size? You could just duplicate the style once per screen size (say, sw600dp and sw900dp), but then you're having to duplicate the minHeight attribute as well. What if you want both attributes to change? Suddenly you've got tons of MyButtons defined everywhere, each one duplicating all other attributes. It's a recipe for disaster; it's so easy to forget to change one attribute in one of the many copies.
Styles are just an alias to a series of attributes. It's a lot easier to just define the style like this:
<style name="MyButton">
<item name="android:minWidth">#dimen/button_min_width</item>
<item name="android:minHeight">#dimen/button_min_height</item>
</style>
Now you can just modify a single attribute for each resource qualifier. It's absurd to think about duplicating a layout just to change, say, the width of one View in portrait vs. landscape. You'd use a dimension for that. The same applies for styles.
I don't mean to imply you should always use resource references in styles; just that you should use it if you need multiple values switched on resource qualifiers.
This isn't to say that sometimes you won't need to duplicate a style across resource qualifiers, but you can keep it to a minimum. Usually the only reason to do so is because of platform changes (e.g., the change from paddingLeft and paddingRight to paddingStart and paddingEnd).
Multiple Styles
It would be wonderful if you could apply multiple styles to a single View, like CSS.
You can't. Sorry.
But you can, in a couple of cases, get an approximation of multiple styles.
Rule #3: Use themes to tweak default styles.
Themes provide ways of defining the default style of many standard widgets. For example, if you want to define the default button for the app, you could do this:
<style name="MyTheme">
<item name="android:buttonStyle">#style/MyButton</item>
</style>
If you're just tweaking the default style, the only tricky part is figuring out the parent of your style; you want it to match the appropriate theme for the device, but that varies based on OS version.
If you're using an AppCompat theme, you should use their styles as the parent since they handle differences across platforms as well. For example, they have a Spinner style:
<style name="MySpinner" parent="Widget.AppCompat.Spinner" />
If the style doesn't exist in AppCompat (or you're not using it), the problem is a bit trickier, since you need the parent to switch based on the theme. Here's an example of a custom Button style that uses Holo normally, but Material when appropriate.
You'd put this in /values/values.xml:
<style name="ButtonParent" parent="android:Widget.Holo.Button />
<style name="ButtonParent.Mine">
<item name="android:background">#drawable/my_bg</item>
</style>
Then, in /values-v21/values.xml:
<style name="ButtonParent" parent="android:Widget.Material.Button />
Setting up the correct parent will ensure consistency with both your app and the platform.
If you truly want to define all necessary attributes (instead of just tweaking the defaults), you could skip parenting entirely.
Rule #4: Use text appearance when possible.
TextAppearance allows you to merge two styles for some of the most commonly modified text attributes. Take a look at all your styles: how many of them only modify how the text looks? In those cases, you could instead just modify the TextAppearance.
First, you need to define your TextAppearance:
<style name="MyTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#0F0</item>
<item name="android:textStyle">italic</item>
</style>
Notice how I've set a parent - text appearances won't merge, so you need to make sure to define all attributes. You can use any appropriate TextAppearance as the parent.
Now you can use it in a TextView:
<TextView
style="#style/MyStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/MyTextAppearance" />
Notice that I can still apply a style to this TextView, getting me a whopping TWO styles for one view! Not as good as true multiple styles, but I'll take what I can get.
You can use TextAppearance in any class that extends TextView. That means that EditText, Button, etc. all support text styling.
Common Pitfalls
I've explained all the times when I use styles. Unfortunately, it is easy to abuse styles in ways that will hurt you in the long run. Here's a few anti-patterns to avoid.
Rule #5: Do NOT create a style if it's only going to be used once.
Styles are an extra layer of abstraction. It adds complexity. You have to lookup the style to see the attributes they apply. As such, I see no reason to use them unless you're going to use the style in multiple places.
Which would you rather see when you open up a layout: This?
<TextView style="HelloWorldTextView" />
Or this?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
It's so easy to create a style later if you need to do so. Don't plan ahead too much.
Rule #6: DO NOT create a style just because multiple Views use the same attributes.
The main reason to use styles is to reduce the number of repeated attributes, right? Why not just use a style whenever multiple Views use the same attributes?
The problem with this attitude is that those Views, if they are not used in the same context, may eventually want to differ in how they look. And at that point, your base style becomes difficult to edit without unintended side effects.
Think about this scenario: you've got a few TextViews that the same text appearance and background. You think, "hey, I'll create a style, that'll cut down on code duplication." Everything is hunky dory at first, but eventually you want to tweak how some of the TextViews look. The problem is, by now that style is used all over the place, so you can't edit it without some collateral damage.
Fine, you say - I'll just override the style directly in the layout XML. Problem solved. Then it happens again. And again. Eventually that style is meaningless because you're having to override it everywhere. It ends up adding extra work instead of making life easier.
That's why I specified in rule #1 that you should use styles when the Views are semantically identical. This ensures that when you change a style, you really do want every View using the style to change.
Implicit vs. Explicit Parenting
Styles support parenting, wherein a child style adopts all attributes of a parent style. It would be rather limiting if they did not.
Suppose I want every Button in the app to look the same, so I make a ButtonStyle. Later, I decide half the Buttons should look slightly different - with parenting, I can just create ButtonStyle.Different, getting the base style + the tweaks.
It turns out there are two ways of defining parents, implicitly and explicitly:
<!-- Our parent style -->
<style name="Parent" />
<!-- Implicit parenting, using dot notation -->
<style name="Parent.Child" />
<!-- Explicit parenting, using the parent attribute -->
<style name="Child" parent="Parent" />
Simple enough, right? But what do you think happens here, when we define parents with both methods?
<style name="Parent.Child" parent="AnotherParent" />
If you answered that the style has two parents, you are wrong. It turns out that it only has one parent: AnotherParent.
Each style can only have one parent, even though there are two ways to define it. The explicit parent (using the attribute) takes precedence. This leads me to the next rule:
Rule #7: DO NOT mix implicit and explicit parenting.
Mixing the two is a recipe for confusion. Suppose I have this layout:
<Button
style="#style/MyWidgets.Button.Awesome"
android:layout_width="match_parent"
android:layout_height="match_parent" />
But it turns out that my style is defined thus:
<style name="MyWidgets.Button.Awesome" parent="SomethingElse" />
Even though it looks like my Button is based on MyWidgets.Button, it's not! The style name is misleading and the only way to discover that is to do extra work and dig into your style files.
The common temptation is to keep using dot notation with explicit parenting so that your styles look hierarchically related:
<style name="MyButton" parent="android:Widget.Holo.Button" />
<style name="MyButton.Borderless" parent="android:Widget.Holo.Button.Borderless" />
Object-oriented styles! They look so pretty, right? But looks are all you're getting - an illusion that styles are related when they are not. The deception is that MyButton.Borderless is related to MyButton, but they have nothing in common! Let's remove the confusion by removing the dots from the name:
<style name="MyButton" parent="android:Widget.Holo.Button" />
<style name="MyBorderlessButton" parent="android:Widget.Holo.Button.Borderless" />
I lose out on the hierarchy looking pretty, but I gain a lot of utility in code.
Styles vs. Themes
Styles and themes are two different concepts. While styles apply to a single View, themes are applied to a set of Views (or to a whole Activity).
For example, suppose you are using AppCompat and you want to set the primary color for the screen. For this, you must theme the entire Activity:
<style name="MyTheme">
<style name="colorPrimary">#color/my_primary_color</style>
</style>
Themes use the same data structure as styles - even using the style tag - but they are, in fact, used in totally different circumstances! They don't operate on the same attributes - for example, you can define a textColor on a View, but there is no textColor attribute for a theme. Likewise, there exists colorPrimary in themes, but in styles they go unused. Thus:
Rule #8: DO NOT mix styles and themes.
Two common mistakes I've seen:
Applying a theme (as a style) to a `View`:
It just makes no sense because a `View` can't use any of the theme attributes anyways. Nothing happens.
Combining the themes/styles in your hierarchy via parenting. I've seen this as a result of people trying to maintain the illusion of hierarchy using dot notation:
Stupid! So, stupid! It does not make any sense and sometimes misfires in strange ways. Just don't do it!
As of Lollipop, you can apply themes to a View and all its children2. Even in that circumstance, you shouldn't mix up the two, though you could use them both in parallel:
<View
style="#style/MyView"
android:theme="#style/MyTheme" />
AppCompat has a simulacrum of View theming for the Toolbar, but that's all you'll get for a while until Lollipop is the minimum supported version of your app. In other words - you can have fun with this feature in a couple years. :P
Conclusion
The unifying element of these rules are to be careful and thoughtful when using styles. They can save you time, but only if you know when to use them.
Font: this article
I tried changing the appearance of a spinner and I partly succeeded. I'm doing this via overriding parts of the theme. I managed to change the text size of the spinner item (i.e. the text size in the drop down button) with my themes.xml and styles.xml:
My themes.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="#android:Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:spinnerItemStyle">#style/CustomSpinnerItem</item>
</style>
</resources>
My styles.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomSpinnerItem" parent="#android:Widget.TextView.SpinnerItem">
<item name="android:textAppearance">#style/CustomTextAppearance</item>
</style>
<style name="CustomTextAppearance">
<item name="android:textSize">30dp</item>
</style>
</resources>
However I cannot find the attributes that are responsible for the text appearance of the items in the dropdown list of the spinner. I tried dropDownItemStyle amongst other things. In my opinion the attribute names are not self-explanatory, so I wanted to know whether there is a documentation of what attribute does what in a style to find out which attributes to override. I found it very cumbersome to trace back all the styles used in a theme via the themes.xml and styles.xml of the platfrom and then try to find the right attributes via trial and error.
I know that one can change the appearance by passing layouts to the adapter, however, this is not really what I was looking for, since (as far as I know), you can only use inheritance in styles and not in layout xml files. If I created a custom layout for the adapter I'd have to create 9-patch images etc., which I think is a bit too time consuming in case I only want to change the text size.
Of course it's possible that I misunderstood the whole concept, since I'm new to Android ;)
You probably have found out the answer since you asked but for others looking at similar questions:
I do not know of a list of attribute names with good explanation of what they do (R.attr's page mostly gives information that is already in the name) but the way I do it is:
Start from the element I give to setDropDownViewResource(), in my case: android.R.layout.simple_spinner_dropdown_item and find.
Find its layout definition in \sdk\platforms\android-17 (specific platform version to avoid redundant results).
Get its style from the layout file. In this case: ?android:attr/spinnerDropDownItemStyle
We now have the attribute name we need.
It's better to do it that way rather than try to guess what attribute to use because you know which attribute the system itself use so it's very likely to be the correct one (unless there's a bug).
If I created a custom layout for the adapter I'd have to create
9-patch images etc.
Well, no, the layout determines what kind of GUI element you would have (a textfield, a spinner, an imagebutton, a custom element...), not how they are styled (nine-patch backgrounds, text colors...), so you still would have to mess with styles to get the right appearance.
For example, for visual consistency I ported the button, checkbox and spinner style from Theme.Holo to Gingerbread, yet I did not mess with layout, all I did was the aforementioned steps plus looking up the result (spinnerDropDownItemStyle in the above example) in themes.xml, which gave me the style name (e.g.: Widget.Holo.DropDownItem.Spinner).
Then I looked that up in styles.xml and imported it (and any parent*) in my project's styles.xml, searching and copying any Holo specific reference in my project and adjusting the namespace accordingly (add android: to attributes and replace ?android:attr with #style for what I copy to my styles.xml file).
So far I haven't had to mess with layouts at all (even the presence of radio buttons in spinner dialogs on Gingerbread is determined by an xml attribute: android:checkMark).
If a style has no parent attribute (like Widget.Holo.DropDownItem.Spinner) then its parent is the same style minus the last element (e.g.: Widget.Holo.DropDownItem)
I have a couple custom preference items -- one that displays a swatch of the currently selected color, and another one that displays a thumbnail.
I have a custom layout for these that matches up very well, and have found that I can make the text appearance match by using android:textAppearance="?android:attr/textAppearanceLarge" as part of the TextView's xml. The problem is, while these generally look fine, they must not be the appearance the 'official' preferences use, since the colors end up wrong on some devices. In particular I'm porting my application to the Nook Color, and it uses a light grey background and black text color on the preference screen instead of black background/light grey text. My text color in this situation stays the same, but the rest of my layout is themed appropriately.
I'm really unsure what I'm supposed to do here to make my text match up with the 'official' theme. Should I be using obtainStyledAttributes and running though my layout to set things? The tutorials I've seen on using that so far have been really baffling, and it seems like there must be a textAppearance or style I can set in the XML to fix this.
You've to define your own application theme which inherits from the official theme you want. Actually, you can just define a theme MyTheme extending the Theme.Light for example.
Create an res/values/styles.xml file like that:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Light">
</style>
</resources>
Then, you just have to apply your theme using the android:theme attribute of the application entity of your AndroidManifest.xml:
<application android:theme="#style/MyTheme">
[...]
</application>