Preferred method for XML layouts: explicit attributes or styles.xml? - android

Currently I'm mixing explicit attributes (layout_width, height, alignment) on my various XML layouts with coded styles in styles.xml, plus outsourcing colors from colors.xml.
From your experience, what's the recommended way to organize an android app's layouts?

Usings styles is way more organized and makes the xml code easier to read, I think that the visual editor for layouts sucks, so the best way to edit is still to manual edit text the file. Easier reading is a big plus.
I use style anywhere im repeating layouts, something that is very useful is that any explicits attributes will override those of the style, so you can use styles and if what you need is almost the same, you just explicitly redifine the attribute.
Any time you need to fix a layout issue you can update the style and not need to update it a million times.
On the other hand if you are just using that layout "style" once, there is no reason to write an actual style for it, just do it all explicitly.

Related

Using style instead of android:textAppearance

android:textAppearance allows you to apply an additional layer of styling for certain attributes on text views. I'm wondering whether the flexibility of this feature outweighs its complexity.
Is android:textAppearance required for any technical or visual reason?
What might be the consequences of relying solely on style throughout a project and omitting the use of android:textAppearance in layouts?
What if it were omitted from styles too?
Here's two guides that recommend the use of android:textAppearance:
Styling Views on Android (Without Going Crazy)
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.
Best practices for happy Android resources
To ensure consistent-looking TextViews, do not define any of the TextAppearance attributes in a normal style, but always set a TextAppearance from your library on that style’s android:textAppearance field.
Looking at TextView.java, there don't appear to be any attributes that are defined by android:textAppearance that are not able to be set on a TextView directly. So there doesn't appear to be any technical reason that one needs to use them over defining attributes in styles directly, at least in one's ability to configure how a TextView looks.
This means that you can avoid using it in your layouts as well as your styles without any adverse effects, provided that you override all the associated attributes. The base theme defines many different standard android:textAppearances for the various widgets, so you should check that all the widgets are properly overridden.
Based on my experience, android:textAppearance is most useful if you wish your app to appear to integrate into the rest of the device. So if you need big text, you can use android:textAppearance="?android:attr/textAppearanceLarge" and now your text is large! No need to know how many sp that means for the given device/screen size/etc..
However if your app is highly styled and you'd otherwise be overriding all the text sizes anyhow, the value-add of android:textAppearance diminishes. You can certainly use it as referenced in the posts above, but if that doesn't fit into your styling system, then feel free to omit it. It's just another tool to help you get a good-looking app across all devices.
I would also recommend making extra-sure that you try your app across a variety of devices if you choose not to use it, just to make sure that you didn't neglect to override a default android:textAppearance.

Override a style in Android without modifying layout file

My experience up until now when dealing with styles has been to create a style.xml file and create the properties I want for the style. If I want my style to be based on an existing style, I use the parent attribute. I then specify the style inside of my layout file on the controls that I want to apply the style to.
Where I am at a loss is when I want to use system styles and only update certain properties. I am wondering whether I can leave the layout files alone and not bother applying any styles to the controls. Instead, I would somehow update the property of the system style and that would update everywhere in my app where that style is already being used by default.
More specifically, I want to change the background color of the Actionbar but haven't found a way of doing it other than the way I described above.
You're probably looking for themes, which are collections of styles, applied either globally throughout the application, or for each Activity in particular. Start with this document and investigate further.

Referencing an individual style attribute from layout xml?

Is it possible to do something like the following?
<LinearLayout android:id="#android:id/empty"
android:background="?android:style/Widget.ListView.overScrollFooter"
>
Left out all the unimportant layout stuff. I'm wondering if it's possible to reference one of the individual attributes of defined in a style?
Edit for more info: The default styles and attributes for many widgets are defined by Android, and customized further by phone manufacturers. That's how they can customize how a basic android widget looks. In my example, the footer of a listView will look different on a Samsung phone than on a HTC phone or on a default Google phone.
I would like to grab the attribute defined in the listview style (specifically the overscrollfooter drawable attribute), and use it as a background for one of my views. Technically speaking, I have a programmatic solution for this, but it's clunky, and requires that I repeat that code every time I use this view (which is in a lot of places).
No, I think a style is an all or none kinda of deal. I would place the footer in it's own style and import it into your primary style. That frees it up to be used (alone) in yout LinearLayout.

Style Editor - dynamically create and apply styles

I'm developing an app for text reading. I'm trying to increase the options for my users to customize the appearance of the text and the app itself. Creating multiple styles in my resources folder and switching among them at runtime seems simple enough using the view constructors that take a style parameter.
However I'm also considering going one step further and creating a style editor in my application that allows users to have full control over their experience.
I think that doing this using Android styles is basically out of the question, since the style ids are generated at compile time. I'm considering creating custom views that are light wrappers around the views that I need styled, manipulating the AttributeSet in the view constructor to apply my styles.
How should I dynamically create and apply styles to my views?
your approach is quite right as i tried several posts in several places, if you don't do it yourself, it wont be done :-) Android does not support dynamic theme as it seems.
I wanted mine to be downloadable as a plugin, you need, as you say create a wrapper and be able to extract extra parameters from the AtrributeSet OR add an extra HashTable parameter with the attributes you want to override.

Android theme customization

I am working on theme-ing my android app. After I went through 'Styles and Themes' in Android SDK and this article, I am still left with one question.
Simply put, how can I use android:textSize to be 14 dip for one TextView, while 18 dip for another TextView, inside a Theme?
I know how to do this with styles. I can define two different styles and make the two different TextViews to use those styles.
What I find difficult is how to do this with Theme. A theme lets me define let's say android:textSize, but then it's applicable to all the TextView s in all the layouts of the Activity.
Any idea how to do that?
Thanks.
You can also do it with creating a style, for instance, "subtext", and apply the subtext style to multiple elements. Check out the links I posted in this question.
https://stackoverflow.com/questions/2973928/how-to-make-theme-of-application-configurable-by-user/3680841#3680841

Categories

Resources