Android Text Appearance Android 01.11.2016

android_text_appearance.png

Android device screens come in all shapes and sizes. Android developers often include text in their applications that needs to be readable, regardless of what device the application is running on. By using some simple methods, developers can write one application whose text will display reasonably well for all sorts of devices, including supporting the user's own text size preferences, with little extra work.

When you want your text to be flexible, based on the user preferences, defne text font sizes using SP (scalable point) units. The Android platform allows dimensional values to be defined in a variety of ways. When it comes to text sizes, you will want to use density-independent units like DP (device-independent pixels) and SP. The SP unit is perfect for text sizes, as it is sensitive to the user's display settings.

When you don't want your text to scale no matter what, use absolute pixel sizes with the px unit. There may be some situations when you do not want your text to scale or change size. While this is discouraged, as it may make font sizes unreadable on some devices, here's how you can do it if you have a good reason for doing so. Simply use one of the absolute units, such as the PX (pixels).

The Android platform defines a set of relative font size styles that you can use in your applications: Small, Medium, and Large. These font sizes are built upon the SP unit type, so they will scale with user preferences. The Text Appearance property is using a style to control the size of the text.

Text Appearance Large
android:textAppearance="?android:attr/textAppearanceLarge"

Text Appearance Medium
android:textAppearance="?android:attr/textAppearanceMedium"

Text Appearance Small
android:textAppearance="?android:attr/textAppearanceSmall"

Usage

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall" />

Result

android_text_appearance_ex.png

You can use one of following snippet to set color of TextView.

You can use various functions from the Color class to set the color.

textView.setTextColor(Color.parseColor("#FFFFFF"));

Color.rgb and Color.argb

textView.setTextColor(Color.rgb(200,0,0));
textView.setTextColor(Color.argb(0,200,0,0));

If you want to define your color in an XML file, you can do this:

<color name="errorColor">#f00</color>

You can use it like so:

textView.setTextColor(ContextCompat.getColor(context, R.color.errorColor));

You can also insert plain HEX, like so:

textView.setTextColor(0xAARRGGBB);

We can define style in styles.xml

<style name="TextAppearance.Title" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textSize">20sp</item>
</style>

and use in layout

<TextView
    ...
    android:textAppearance="@style/TextAppearance.Title"/>

or in code

TextView tv = findViewById(R.id.tv);
tv.setTextAppearance(R.style.TextAppearance_Title);

How to set gradiant color in TextView

We can set gradiant color in TextView using following snippet.

tv1 = findViewById(R.id.tv1);
tv2 = findViewById(R.id.tv2);
tv3 = findViewById(R.id.tv3);

int[] color = {Color.DKGRAY,Color.CYAN};
float[] position = {0, 1};
Shader shader1 = new LinearGradient(0, 0, 0, 50,color,position, Shader.TileMode.MIRROR);
tv1.getPaint().setShader(shader1);

Shader shader2 = new LinearGradient(0, 0, 0, 200,color,position, Shader.TileMode.REPEAT);
tv2.getPaint().setShader(shader2);

Shader shader3 = new RadialGradient(0, 3, 5, color[0], color[1], Shader.TileMode.REPEAT);
tv3.getPaint().setShader(shader3);

Result

android_textview_gradient.png