Nima Sa
Nima Sa

Reputation: 89

Can't Use Gravity on two textviews in one row

I use this code to make my two textviews have different gravity which are in the same row. but they don't.
code:

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView android:text="@string/text_ceo_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:id="@+id/txt_ceo_name"/>
            <TextView android:text="@string/text_ceo_name_title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:id="@+id/txt_ceo_name_title"/>
        </LinearLayout>

Upvotes: 1

Views: 230

Answers (4)

Renjith
Renjith

Reputation: 5803

Add a view to the left of textviews in parent layout and use layout_weight for 3 views. Like this:

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

           <View android:layout_width="0dip" 
              android:layout_height="wrap_content"
              android:layout_weight="1.0"/>

          <TextView android:text="text_ceo_name"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:gravity="center"
                android:id="@+id/txt_ceo_name"/>
            <TextView android:text="text_ceo_name_title"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:gravity="right"
                android:id="@+id/txt_ceo_name_title"/>
</LinearLayout>

This will set one text to center of layout and the other one to the right of layout.

Also android:gravity will set gravity to the child of the view you are using.

For setting gravity to your layout, use android:layout_gravity

Upvotes: 1

Yogesh Somani
Yogesh Somani

Reputation: 2624

Its working...

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="10">
        <TextView android:text="me"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:gravity="right"
            android:id="@+id/txt_ceo_name"/>
        <TextView android:text="You"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:gravity="right"
            android:id="@+id/txt_ceo_name_title"/>
    </LinearLayout>

Try giving weight as per you want.

Upvotes: 2

Rahul
Rahul

Reputation: 10625

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView android:text="@string/text_ceo_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/txt_ceo_name"/>
        <TextView android:text="@string/text_ceo_name_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:id="@+id/txt_ceo_name_title"/>

            </RelativeLayout>

Try this it may help you. One of your TextView width is fill_parent and other's TextView width is wrap_content. May be thats the problem also. Accept answer if it helps.

Upvotes: 0

Chen Doron
Chen Doron

Reputation: 106

LinearLayout will just not cut it in this case.

Use RelativeLayout.

Align one text view to "center in parent"
And the other text view to "Align to parent's right"

Upvotes: 0

Related Questions