Kiran Malvi Kiran Malvi - 1 month ago 10
Android Question

Formatting multiple string into one string

So my requirement is as follows:
I've multiple strings stored in different string variables, lot of them are hardcore string, some of them are user inputs, while some are values from database(online as well as offline database.)
I want to combine all the strings in single String variable but I want each letter formatted differently as shown in following image.

Formatted String should look something like this

where as my normal string would be like this:


MAIN TITLE SUBTITLE: Detail 1 Detail 2 Heading 1: User Input Data Heading 2: User Input Data.


Thank you!

Answer

Spannable String is your solution.

Refer this https://developer.android.com/reference/android/text/SpannableString.html

Also refer this http://androidcocktail.blogspot.in/2014/03/android-spannablestring-example.html

Here is an example:

final SpannableString text = new SpannableString("Hello World");
text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "World".length(),text.length(),
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.BLACK), 4, text.length() - 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
Comments