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:
How do I modify the code to make it ALWAYS 50%/50% no what text is inside the textview?
Change the width
of your TextView
s 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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With