4

I want to set text in a single line TextView on both left and right side of the only line. For this I use a SpannableStringBuilder.

Here is an example :

mSpannableStringBuilder.append("leftright");
mSpannableStringBuilder.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_NORMAL), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableStringBuilder.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), 4, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(mSpannableStringBuilder, BufferType.SPANNABLE);

But it doesn't work ... Any suggestions please ? Thanks you

GBouerat
  • 480
  • 3
  • 13
  • possible duplicate of [Android TextView Justify Text](http://stackoverflow.com/questions/1292575/android-textview-justify-text) – CommonsWare Jun 17 '12 at 13:26
  • Possible duplicate of [Make a certain part of a android-textview align to the right](http://stackoverflow.com/questions/14520808/make-a-certain-part-of-a-android-textview-align-to-the-right) – M. Reza Nasirloo Jul 23 '16 at 21:11
  • Working solution http://stackoverflow.com/a/36488501/2970351 – M. Reza Nasirloo Jul 23 '16 at 21:12

3 Answers3

0

I just use in the xml file as the below

<RelativeLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:gravity="center_horizontal" > <TextView android:text="AAA:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#3b5998" android:layout_marginRight="5px" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <TextView android:id="@+id/maine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Not Set" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#0040FF" android:layout_alignParentRight="true" android:layout_alignParentTop="true"
/> </RelativeLayout>

Zarli
  • 15
  • 5
0

Check if your xml tag contains "android:textAlignment" or "android:gravity" properties. In positive case, remove it.

Jean Toledo
  • 71
  • 1
  • 7
0

It can be done with the span, but requires a linefeed between the left and right string which sort of sucks. If there is no linefeed, it can only align the line left or right, but not split as left and right.

Frank
  • 605
  • 1
  • 8
  • 14