Hichem Acher
Hichem Acher

Reputation: 433

Split Text in multiple TextViews according to left space

How to split a String text (the blue one) between two TextViews so that it starts in one TextView and continues in the other one when there is no more room left. The maximum width of the two TextViews is not the same.

An example would be filling a form. The black text is static (two labels).

The result

Another approche could be having only one TextView (for the blue text) that has padding left and right but only for the first line. For each padding, the padding size would equal the label width.

Upvotes: 0

Views: 2191

Answers (1)

Ara Hakobyan
Ara Hakobyan

Reputation: 1692

you should do it programmatically

    int textSize = 16;

    textView2.setTextSize(textSize);
    textView1.setTextSize(textSize);

    final float scale = getResources().getDisplayMetrics().density;
    int dpWidthInPx  = (int) (100 * scale);

    int countTv1Chars = dpWidthInPx / textSize;
    String tv1String = string.substring(0, countTv1Chars);
    String tv2String = string.substring(countTv1Chars, string.length() - 1);
    textView1.setText(tv1String);
    textView2.setText(tv2String);

in xml

    <TextView

    android:id="@+id/tv1"
    android:lines="1"
    android:layout_width="100dp"
    android:layout_height="wrap_content" />

<TextView

    android:id="@+id/tv2"
    android:lines="1"
    android:layout_width="100dp"
    android:layout_height="wrap_content" />

Upvotes: 2

Related Questions