How To Adjust Text Alignment To Both Corners Of A Textview
Currently my layout looks like this: Layout code:
Solution 1:
You can add one relative layout with horizontal orientation and add two text views inside it and then configure their orientation. For example;
<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_alignParentStart="true"android:layout_height="wrap_content"android:text="Name:"android:layout_alignParentLeft="true" /><TextViewandroid:layout_width="wrap_content"android:layout_alignParentEnd="true"android:text="Tiwari Ji"android:layout_height="wrap_content"android:layout_alignParentRight="true" /></RelativeLayout>
Aligning one TextView to start of parent, other one to the end of parent should give you the desired result.
You can see what the above code renders on screen: Properly Aligned text
The same feat can also be achieved using LinearLayout
<LinearLayoutandroid:layout_width="match_parent"android:orientation="horizontal"android:layout_margin="16dp"android:layout_height="wrap_content"><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="Name:" /><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:gravity="right"android:text="Tiwari Ji"android:layout_height="wrap_content" /></LinearLayout>
Here I've given equal weight to the TextViews inside the LinearLayout, by giving them android:layout_weight="1"
and android:layout_width="0dp"
.
And then by giving android:gravity="right"
ensured that the text inside the layout is aligned towards the end of the right-most edge of the view.
Happy to help, you can ask if you have any queries.
Post a Comment for "How To Adjust Text Alignment To Both Corners Of A Textview"