Give equal width to multiple TextView objects

I want to design a layout with 3 TextView objects, which should be of equal width and fill the screen. There should be a little gap between the 3 view objects.

What attribute I should use?
allsolutionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simonetCommented:
Will they be side-by-side ?

If so, you can use RelativeLayout, like this:

 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="TextView" />

</RelativeLayout>

Open in new window


In each <TextView> object, the property android:layout_marginLeft determine the gap between each of the (2 & 3) and the left margin of the screen (for textView1).

The width of each one is determine by android:layout_width.

0
allsolutionsAuthor Commented:
Yes, they will be side by side. In your code above, which attribute will ensure that there is no blank area / unutilised space in the screen after the 3rd TextView? It should work irrespective of screen width, so if you hard code width as 70dp and screen is bigger, I want that all 3 TextView objects should stretch equally to fill the entire screen.
0
simonetCommented:
Good question! Here's a different proposition:

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dip"
	android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dip"
	android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dip"
	android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="TextView" />
</LinearLayout>

Open in new window


The secret here are these 2 lines in each TextView:


        android:layout_width="0dip"
      android:layout_weight="1"

What they mean is: each textView should be size zero per default, and since each has the same weight (1), all the space in the row should be equally divided between them.

Spacing between textView 2 and 3 is defined by
   
      android:layout_marginLeft="10dp"


You can use that LinearLayout inside other layouts.

This way they will always have the same width (each about 1/3 of the screen, minus the spacing between them), independently of the screen size or position (portrait/landscape).

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
allsolutionsAuthor Commented:
Great !
I will check it out and accept the solution.

Can you also have a look at my other Android related questions?
0
allsolutionsAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Android

From novice to tech pro — start learning today.