Working with Android TextView

Everything related to TextView customization in Android SDK

Spannable TextView:

A spannable TextView can be used in Android to highlight a particular portion of text with a different color, style, size, and/or click event in a single TextView widget.

Consider that you have defined a TextView as follows:

Then you can apply different highlighting to it as shown below:

Spannable color: In order to set a different color to some portion of text, a ForegroundColorSpan can be used, as shown in the following example:

Output created by the code above:

 

 

 

Spannable font: In order to set a different font size to some portion of text, a RelativeSizeSpan can be used, as shown in the following example:

Output created by the code above:

 

 

 

Spannable typeface: In order to set a different font typeface to some portion of text, a custom TypefaceSpan can be used, as shown in the following example:

However, in order to make the above code working, the class CustomTypefaceSpan has to be derived from the class TypefaceSpan. This can be done as follows:

Strikethrough TextView

Strikethrough the entire text

Output: This is a test strike

Strikethrough only parts of the text

Output: This is a test strike

TextView with image

Android allows programmers to place images at all four corners of a TextView. For example, if you are creating a field with a TextView and at same time you want to show that the field is editable, then developers will usually place an edit icon near that field. Android provides us an interesting option called compound drawable for a TextView:

You can set the drawable to any side of your TextView as follows:

Setting the drawable can also be achieved programmatically in the following way:

Setting any of the parameters handed over to setCompoundDrawables() to null will remove the icon from the corresponding side of the TextView.

Make RelativeSizeSpan align to top

In order to make a RelativeSizeSpan align to the top, a custom class can be derived from the class SuperscriptSpan. In the following example, the derived class is named TopAlignSuperscriptSpan:

activity_main.xml:

MainActivity.java:

TopAlignSuperscriptSpan.java:

Reference screenshot:

 

 

 

 

 

 

 

Here Have Some Customization of TextView In Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

19 − 15 =

Comment moderation is enabled. Your comment may take some time to appear.