9

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 ?

senior
  • 435
  • 5
  • 13
  • 1
    Are you using 2 textviews for a reason? I think this can be easily done with a single TextView. That way the wrapping will be automatically done the way you want it. – Raoul George Jan 25 '12 at 08:45
  • 2
    have a look at using [SpanableString in a single TextView](http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring) – Adil Soomro Jan 25 '12 at 08:47
  • u said that the size of A&B are not constant..that means the value is dynamic rt? – andro-girl Jan 25 '12 at 09:21
  • i am using or better want to use, one textview – senior Jan 25 '12 at 09:22

6 Answers6

16

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.

Alexei
  • 1,028
  • 1
  • 11
  • 17
  • can i add support for styles ? i failed to mention that sentence A... and B... have different styles – senior Jan 25 '12 at 09:26
  • 2
    Yes, you can use [TextAppearanceSpan](http://developer.android.com/reference/android/text/style/TextAppearanceSpan.html) as a parameter to setspan function to set the text color, size, style, and typeface. – Alexei Jan 25 '12 at 09:51
  • 1
    i have tried so many solution but this one really helped me for doing widget's texview in different colors for two sentences in single textview. – Happy Feb 08 '17 at 06:15
2

You can use HTML tags to get the desired effect with a single text view. Check this and this and also this.

Community
  • 1
  • 1
manjusg
  • 2,275
  • 1
  • 22
  • 31
  • i want to add different styles for each sentence so i need support for layout styles – senior Jan 25 '12 at 09:24
  • Did u checked the tags in commonsware blog that i had mentioned. There are many params to change the style of the text you want. Can't you use for each sentence and get your problem solved?.. If you are still facing the issue don't hesitate to post what you tried with html tags – manjusg Jan 25 '12 at 09:42
1

You could use HTML to display your text

myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
endian
  • 4,761
  • 7
  • 32
  • 54
0
<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.

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
0

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);
Ravi Bhojani
  • 1,032
  • 1
  • 13
  • 24
-1

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.

andro-girl
  • 7,989
  • 22
  • 71
  • 94