12

If I want to make my TextView width exactly equal to the width of its parent, I can use

android:layout_width="fill_parent"

What about if I want to make it half the width of the part? Or, in general, set the width relative to the parent's width?

Edit: I am using Relative Layout

My screen looks like this.

My screen looks like this.

Pranit Bankar
  • 453
  • 1
  • 7
  • 21
Karen Tsirunyan
  • 1,938
  • 6
  • 19
  • 30

6 Answers6

16

The quick and dirty way is like this:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff0000"
            />

    </LinearLayout>

</RelativeLayout>

You have to wrap it in another container, then use only half the weight sum of the parent.

Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
9

Use android:layout_weight="0.5" it will work for linearLayout

<LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:gravity="center"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/feedback"
                    android:layout_width="wrap_content"
                    android:layout_weight="0.5"
                    android:textSize="15pt" />
DjHacktorReborn
  • 2,908
  • 2
  • 20
  • 29
1

First of all don't use "fill_parent" it a deprecated tearm, use "match_parent".

Secondly where do you want to place the half parent TextView in the parent?

As you are using RelativeLayout this action is a little harder, try to do it from code, some thing like this:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
int myWidth = (int) (parentHeight * 0.5);
super.onMeasure(MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY),  
heightMeasureSpec);
}

reference: How to size an Android view based on its parent's dimensions

Community
  • 1
  • 1
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
1

You may want to set the weightSum of the parent LinearLayout, something like this:-

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:padding="6dp"
    android:weightSum="2">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/hello"
        android:layout_weight="1" />

</LinearLayout>
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

If you want to use RelativeLayout, then best approach is to put a dummy view at the center of the screen and put the first view to the right and second to the left hand side of the dummy view like this;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_contentt"
        android:background="#ff0000"
        android:layout_toLeftOf="@id/dummyView/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
         android:layout_toRightOf="@id/dummyView"/>
     <View
        android:id="+@id/dummyView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:layout_centerInParent="true"/>

</RelativeLayout>
Oguz Ozcan
  • 1,694
  • 19
  • 26
0

Wrap your TextView with a Linear Layout, like @r-j and @djhacktorreborn have suggested, and then put the whole thing into a Relative Layout.

Anton Cherkashyn
  • 5,719
  • 6
  • 43
  • 80