Let's say you have a style called RedHUGEText on your values/styles.xml:
<style name="RedHUGEText" parent="@android:style/Widget.TextView">
Just create your TextView as usual in the XML layout/your_layout.xml file, let's say:
And in the java code of your Activity you do this:
TextView textViewTitle = (TextView) findViewById(R.id.text_view_title);
It worked for me! And it applied color, size, gravity, etc. I've used it on handsets and tablets with Android API Levels from 8 to 17 with no problems.
Remember... this is useful only if the style of the text really depends on a condition on your Java logic or you are building the UI "on the fly" with code... if it doesn't, it is better to just do:
You can always have it your way!
This will only set a subset of attributes that can be defined in a <style> tag. It does the common ones (size, color, style), but doesn't apply others (including padding, background, gravity, etc)
there are two methods, one for api < 23, and one for api 23+. They appear identical except the one for <23 takes a context argument and the one for 23+ does not. Under the hood, the one for api 23+ calls the method for <23 and uses the member context for the textview.