1

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!

2
  • Why do you require this, may I ask? Commented Oct 26, 2016 at 13:19
  • I need to send an email of collected data, for which I need to format the data so it would look good in composed email. Commented Oct 26, 2016 at 13:22

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

https://developer.android.com/reference/java/util/Formatter.html

Please check above link for string formatting.

Comments

0

You can use Html.fromHtml() and format the string with HTML like;

String my_string = "<h1>MAIN TITLE</h1><br /><h2>SUBTITLE</h2><br />"
                   +"Detail 1 <br /> Detail 2 <br /><br/>"
                   +"<h4>Heading 1:</h4> User input data <br/>"
                   +"<h4>Heading 2:</h4> User input data"

Then

TextView myTextView = (TextView) findViewById(R.id.the_id_of_your_tv);
myTextView.setText(Html.fromHtml(my_string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.