I have a few Button listed vertically and I need all of them to have the same width, but also to display all text inside.
Basically I need width for all of them as a wrapped width of the largest one.
Hope I explained it well.
Now... I already have one layout that is working on my Samsung Galaxy S2 (4.1.2), but on friend's phone - Samsung GT-N7100 (note2) and android 4.4.2 - it is not working - some text is not displayed in the Button.
This is my layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="word"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:textSize="30sp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:id="@+id/word"/>
<TextView
android:text="14/36\nM: 1"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:id="@+id/score"
android:layout_toRightOf="@+id/word"
android:gravity="right|center"
android:paddingRight="10dp"/>
<LinearLayout
android:layout_centerInParent="true"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/adapter">
<Button
android:text="1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt1"/>
<Button
android:text="2"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt2" />
<Button
android:text="3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt3" />
<Button
android:text="4"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt4" />
<Button
android:text="5"
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/opt5" />
</LinearLayout>
<Button
android:text="Next"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:id="@+id/next"/>
</RelativeLayout>
I have done this using XML only (no code) - for two TextViews. But the same technique should work for arbitrary number of buttons as well.
Note: Make sure the background color/shape for each TextView is set via its background of individual layout container and not on the TextView itself.
I know this question was asked 3 years ago - but I think I saw same/similar questions elsewhere for a solution that involves XML only. So hope this helps someone in the future.
Try setting android.width="auto"
You can extend the
LinearLayout
to force all its childs to have the max width:Now in the xml file of your layout, use
LinearLayoutButtonsHolder
rather thanLinearLayout
. Do not forget to set theandroid:layout_width
of eachButton
towrap_content
:use linearlayout like this