Android - two sentences, two styles, one TextView
Question
I am tyring to display a TextView that contains two sentences and I want them to be one after the other like this:
AAAAAA: BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBB
where A... is the first word\part of sentence and B... is a second sentence.
the size of A... & B... isn't constant.
tried doing this:
<RelativeLayout
style="@style/f13"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAAA: " />
<TextView
style="@style/f15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
android:layout_toRightOf="@id/first_text" />
</RelativeLayout>
but i got:
AAAAAA: BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBB
and i want the second (B...) sentence to continue below the first sentence (A...) i should add that sentence A... & B... have different styles.
the style for A:
<style
name="f13">
<item name="android:typeface">sans</item>
<item name="android:textSize">15dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/grey_dark_emperor</item>
</style>
the style for B:
<style
name="f15">
<item name="android:typeface">sans</item>
<item name="android:textSize">17dp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">@color/grey_dark_emperor</item>
</style>
any thoughts ?
Solution
You can try to use the SpannableStringBuilder as follows:
String firstString = "AAAAAA: ";
String secondString = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(firstString + secondString);
stringBuilder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, firstString.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
stringBuilder.setSpan(new ForegroundColorSpan(Color.rgb(255, 0, 0)), firstString.length() + 1,
firstString.length() + secondString.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(stringBuilder);
This will make the first sentence to be bold, and the second one coloured in red.
OTHER TIPS
You could use HTML to display your text
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAAA: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" />
</RelativeLayout>
Use this as xml.
By uing single textview only you will be able to do it. you just need to append two string in single textview.
textview.setText(first_text+":"+second_text);
u have written the position of second text as right of first text.
android:layout_toRightOf="@id/first_text"
so as per my knowledge it is not possible to continue below the first sentence.it is always writing the second text only in the right side of first text.