Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - two sentences, two styles, one TextView

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 ?

like image 980
senior Avatar asked Jan 25 '12 08:01

senior


People also ask

Can you have multiple styles inside TextView?

There are many ways to make multiple styles inside android TextView and in this example I have made multiple style with three different ways using HTML tags and Html. fromHtml().

What is Spannable string in android?

Text styling is one of the important aspects when it comes to enhancing the UI of an Android application. In Android, we can change the size, color, weight, style, etc of a text and make the text more attractive and appealing.


1 Answers

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.

like image 107
Alexei Avatar answered Sep 28 '22 10:09

Alexei