Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to size two textview side by side and use 50% width of screen

I have the following code which has 2 textviews side by side in 4 rows:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:layout_weight="1.1"
    android:gravity="center"
    android:weightSum="4" >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" >
        <TextView
            android:id="@+id/tvCodeNameDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cnsdsdsf:"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#ff0000" />
        <TextView
            android:id="@+id/tvCodeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#00FF00" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" >
        <TextView
            android:id="@+id/tvVersionDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="vnddf: "
            android:layout_weight="1"
            android:gravity="center"
            android:background="#00ff00" />
        <TextView
            android:id="@+id/tvVersion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#ff0000" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" >
        <TextView
            android:id="@+id/tvReleaseDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="rdsdfsdfsfsdf: "
            android:layout_weight="1"
            android:gravity="center"
            android:background="#FF0000" />
        <TextView
            android:id="@+id/tvRelease"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#00FF00" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" >
        <TextView
            android:id="@+id/tvAPIDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="apdd: "
            android:layout_weight="1"
            android:gravity="center"
            android:background="#00ff00" />
        <TextView
            android:id="@+id/tvAPI"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#ff0000" />
    </LinearLayout>
</LinearLayout>

It displays the following:

enter image description here

How do I modify the code to make it ALWAYS 50%/50% no what text is inside the textview?

like image 349
Si8 Avatar asked Oct 16 '13 15:10

Si8


2 Answers

Change the width of your TextViews both to 0dp instead of wrap_content

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center" >
    <TextView
        android:id="@+id/tvCodeNameDetail"
        android:layout_width="0dp"              <!-- here -->
        android:layout_height="wrap_content"
        android:text="cnsdsdsf:"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#ff0000" />
    <TextView
        android:id="@+id/tvCodeName"
        android:layout_width="0dp"              <!-- and here -->
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#00FF00" />
</LinearLayout>

When using weight in a horizontal LinearLayout you need to have a width of 0dp. Likewise, in a vertical LinearLayout, you would want a height of 0dp.

like image 81
codeMagic Avatar answered Nov 01 '22 17:11

codeMagic


The code worked for me, but I substracted both of "0dp" arguments and instead used only "weight=1". I am using a custom row maker for a list view and it worked :D

like image 45
Alejo Avatar answered Nov 01 '22 16:11

Alejo