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
Related
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.
What is the difference between let's say android.R.style.TextAppearance_large and android.R.attr.textAppearanceLarge?
The attr part is just a thing which can been styled e.g. with a theme while that style is already one defined style.
So you can refer a style without knowing the style attributes. That is really helpful if you want to define a control which can been styled in multiple ways. Like for a button you have a holo style or some other device typical styles.
If you know C you can compair it with a prototype (or header file) while the style is the implementation.
Im searching for an android gui-library with more components i could use in my app.
Example:
A microcontroller sends the rpm of a motor (via bluetooth) to my smartphone.
And i want to use my smartphone in order to show the received rpm in progressbar-like element.
But the normal progressbar looks ugly, and i would have to progressbar.setClickable(false); .
As i said before
Im searching a library with more gui-elements(optional: i could modify, customize the outward appearance on my own)
Do anybody of you know about such a library ?
Thanks so far.
If you're just concerned about the external appearance of your GUI elements, I don't think there's any support for different UI elements than the ones provided. However, have you looked into a universal Theme and style for your application?
Here are some excerpts from that document:
A style is a collection of properties that specify the look and format
for a View or window. A style can specify properties such as height,
padding, font color, font size, background color, and much more. A
style is defined in an XML resource that is separate from the XML that
specifies the layout.
A theme is a style applied to an entire Activity or application,
rather than an individual View (as in the example above). When a style
is applied as a theme, every View in the Activity or application will
apply each style property that it supports. For example, you can apply
the same CodeFont style as a theme for an Activity and then all text
inside that Activity will have green monospace font.
Here are some resources which talk about themes:
Mobile Orchard Article
Android Engineer Article
Let me know if that's what you wanted. Themes give you almost infinite possibilities to modify outwards appearance.
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.
How would you implement different color themes in your app?
All I can see now is plain set color onCreate every activity and control...
Also, how would you store different color schemes in xml?
Just an entries of with different names?
Thank you!
Use custom Themes, which are declared in XML. They are very similar to CSS, if you've used them before.
EDIT:
Here's a better example of changing the theme at run time