Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the first character much larger than other in a TextView

Tags:

android

I would like to display a paragraph of long text as follow. Here is the snapshot from Flipboard.

enter image description here

In order to achieve such effect, I tried to use 2 TextViews.

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:text="T" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="76dp"
        android:text="op British supermarket Tesco (LSE: TSCO) (NASDAQOTH: TSCDY.US) is due to announce its half year results on" />

</LinearLayout>

I get the following result.

Quite close. But not what I want. This is because the 2nd line and the 3rd line is not align to the left most. The left most space for 2nd line and 3rd line, is occupied by the TextView with large font.

enter image description here

Is there any better technique I can use, such as the one in Flipboard's?

like image 773
Cheok Yan Cheng Avatar asked Sep 26 '13 14:09

Cheok Yan Cheng


People also ask

How do I limit characters in TextView?

To set the maximum length for text in TextView widget, set the maxLength attribute with required number of length.

How do you capitalize the first letter in android?

The on-screen keyboards of Android devices will capitalize the first letter of a sentence by default thanks to a built-in "Text correction" tool. Gboard, the default keyboard app for Android devices, allows you to change this feature through the gear icon located at the top of the keyboard.


1 Answers

By using a single TextView with BufferType.SPANNABLE will solve the problem.

final SpannableString spannableString = new SpannableString(title);
int position = 0;
for (int i = 0, ei = title.length(); i < ei; i++) {
    char c = title.charAt(i);
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
        position = i;
        break;
    }
}
spannableString.setSpan(new RelativeSizeSpan(2.0f), position, position + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//titleTextView.setText(spannableString.toString());
titleTextView.setText(spannableString, BufferType.SPANNABLE);
like image 174
Cheok Yan Cheng Avatar answered Sep 21 '22 14:09

Cheok Yan Cheng