writing style format in android - android

I am in bit confusion to adding parent style to new style. But both are working. But i want to know which is correct way first one or second one? or is there any other way ?
<style name="customView" parent="viewline">
<item>....
and
<style name="customView" parent="#style/viewline">
<item>....
Thanks

Both are correct as you said. At here there is no any drawback to use any of them.
<style name="customView" parent="viewline">
denotes that the parent style is in same file. And
<style name="customView" parent="#style/viewline">
denotes that parent style is somewhere (may be i another file) in style directory. Thats it.

Related

is there a redundant inheritance in my style.xml? [duplicate]

I know that
we have not to use parent attribute. We prefix one style to another
style separating by a period(.)
so in this style, does it have a circular inheritance?
<style name="TextAppearance.A" parent="TextAppearance.A.B">
<item name="android:textAlignment">viewStart</item>
<item name="android:gravity">start</item>
</style>
TextAppearance.A.B inherits from TextAppearance.A because of android dots' syntax.
but TextAppearance.A inherits from TextAppearance.A.B because of android paretn syntax.
Is it really a problem?
Technically As per Android Documentation I dont think this is possible,
Because this will lead to duplication of style, If you refer to same as Diamond Problem it will be one of those, also android prevents you from inheriting from more than one style.
Further Imagine if you have one attribute which is defined in style A also in Style B, it will be a problem at compile time that which attribute to choose from both.
For More Details please refer to android documentation
https://developer.android.com/guide/topics/ui/look-and-feel/themes

Android: holo button with image

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.

What is the correct style parent definition

When looking at the examples of defining style parent I noticed two different approaches are used for explicit parenting:
Number one:
<style name="MyButton" parent="android:Widget.Button"/>
Number two:
<style name="MyButton" parent="#android:style/Widget.Button"/>
As far as I could see, they both seem to work. I wonder if both of these approaches are correct or there is one I should stick to?
Both are correct, but stick to this:
<style name="MyButton" parent="#android:style/Widget.Button"/>
As this accesses your current compileSdkVersion's resource directory.

styles and theme

I still not understanding the parent role in the android styles I have put my XML code into the style XML which works well. I have been trying to get my head round android styles and I just not getting it. Any help would be great.
ps. I have been reading all about styles on the net. But the information is good but could be a lot better
http://imageshack.us/a/img843/3083/holo.png
<style name="LLayout1">
In your style.xml just replace the above line with below line
<style name="LLayout1" parent="android:Theme">
This is because every style in XML must have a parent type.

Android style descriptions

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)

Categories

Resources