Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make my TextView width half of its parent?

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.

like image 240
Karen Tsirunyan Avatar asked Mar 10 '13 09:03

Karen Tsirunyan


5 Answers

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" />
like image 101
DjHacktorReborn Avatar answered Nov 17 '22 01:11

DjHacktorReborn


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.

like image 38
Christopher Perry Avatar answered Nov 17 '22 02:11

Christopher Perry


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>
like image 1
Rahul Avatar answered Nov 17 '22 02:11

Rahul


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

like image 1
Emil Adz Avatar answered Nov 17 '22 00:11

Emil Adz


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>
like image 1
Oguz Ozcan Avatar answered Nov 17 '22 00:11

Oguz Ozcan