JimT
JimT

Reputation: 99

How to align a TextView to the right with LinearLayout

I have two textView objects that I am using to display the result of the calculations in my app. They are showing up to the right of the TextView so I wanted to know if there is a way through the xml file to position the TextView to the right. This is my xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.jtryon.rectanglecalc.MainActivity$PlaceholderFragment" >

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/width_string"
            android:textSize="16sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/width_edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"      
            android:inputType="numberDecimal" > 

        <requestFocus />
        </EditText>       

    </LinearLayout>

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/height_string"
        android:textSize="16sp"
        android:textStyle="bold" /> 

    <EditText
        android:id="@+id/height_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal" />     

    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold" 
            android:text="@string/area_string" />

        <TextView
            android:id="@+id/area_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp" />

    </LinearLayout>

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

        <TextView
            android:id="@+id/perim_string"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold"
            android:text="@string/perim_string" />


        <TextView
            android:id="@+id/perim_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp" />

        </LinearLayout>    

</LinearLayout>

How it should look

Upvotes: 2

Views: 21572

Answers (4)

asif juneja
asif juneja

Reputation: 61

You can do something like this

  <LinearLayout
    android:gravity="end"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/ic_call_white"
        />

</LinearLayout>

Please check that Linear Layout has an attribute named GRAVITY and inner control(ImageView) has an attribute named LAYOUT_GRAVITY both are different

Upvotes: 3

Marcio Oliveira
Marcio Oliveira

Reputation: 821

Make sure your view matches the parent, then add the gravity of its content to the right:

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/width_string" />
</LinearLayout>

Hope it helped ;)

Upvotes: 14

mungaih pk
mungaih pk

Reputation: 1911

Just add the line

android:gravity="right"

to the textview you want aligned left.

Upvotes: 2

itsben
itsben

Reputation: 1027

Sure. Just change the order of the elements inside the LinearLayout. Make sure the EditText field is first and then the TextView field. Easy.

Upvotes: 0

Related Questions