How to size two textview side by side and use 50% width of screen
11,461
Solution 1
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
.
Solution 2
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
Author by
Si8
Updated on August 01, 2022Comments
-
Si8 almost 2 years
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?
-
codeMagic over 10 years@MikeM. I haven't tried it but I wouldn't think so. That wouldn't leave room for the second
TextView
(I don't think). Last time I read the docs for it,0dp
should be used when usingweight
on aView
. -
Si8 over 10 years@MikeM. I will test and let you know :)
-
codeMagic over 10 years@MikeM. ok, thanks for the update. I would stick with
0dp
since that is what the Google devs sugges (AFAIK anyway) unless there is a reason you needmatch_parent
. I wonder if it makes a difference what thewidth
of theparent
is. I could see it working if it iswrap_content
... -
codeMagic over 10 years@MikeM. of course, I don't pretend to know everything Android so hearing other ideas and suggestions are always welcome
-
Illegal Argument about 10 years@codeMagic shouldnot the android:layout_weight="1" of linear layout be 2 and the textview doesnot maintain its position when the text doesnot fit in single line
-
radley almost 10 yearsUse match_parent if the other sibling will be dynamically added or removed. You also have to use match_parent if any sibling is using match_parent. Otherwise stick to 0dp; the OS likes it best.