Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView Align text to Right and Left

Tags:

android

I'm trying to have a TextView that has two pieces of text, one aligned against the far left and one against the far right. I can put spaces between the two words, but I was wondering if there is a better technique?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/background_common"
    android:id="@+id/LinearLayout0123">

   <TextView
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:textColor="@color/Black"
        android:textStyle="bold"
        android:textSize="15dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/selector_social_facebook_twitter"
        android:text="Right Text                                  Left Text" />
</LinearLayout>
like image 441
c12 Avatar asked May 23 '12 17:05

c12


People also ask

How to align text vertically in TextView android?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How to set TextView alignment in android?

To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View. TEXT_ALIGNMENT_CENTER in activity file.

How to align text to left in android studio?

To left align the text in this Textview through layout file, set the android:textAlignment attribute for the TextView to “viewStart”, as shown in the following activity_main. xml layout file.

What method do we use to specify the alignment of the text within a TextView?

So the gravity attribute specifies how to align the text inside the TextView layout_gravity specifies how to align/layout the TextView element itself.


1 Answers

Here's another method:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_common">
    <TextView
        android:text="Left Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"/>
    <TextView
        android:text="Right Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"/>
</FrameLayout>

Layout_gravity tells the parent where to put the child. In a FrameLayout, children can go anywhere and are unobstructed by other children. If there is overlap, the last child added is on top. Good luck!

like image 103
Zaid Daghestani Avatar answered Oct 11 '22 03:10

Zaid Daghestani