How to achieve auto button width in android layout
I have several buttons listed vertically and I need them all to have the same width but also display all the text inside.
Basically, I need the width for all of them as the wrapped width of the largest one.
Hope I explained it well.
Now ... I already have one layout that works on my Samsung Galaxy S2 (4.1.2), but on another phone - Samsung GT-N7100 (note2) and Android 4.4.2 - it doesn't work - 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>
source to share
You can expand LinearLayout
so that all of its children have their maximum width:
// LinearLayoutButtonsHolder.java
package github.yaa110.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class LinearLayoutButtonsHolder extends LinearLayout {
public LinearLayoutButtonsHolder(Context context) {
super(context);
}
public LinearLayoutButtonsHolder(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LinearLayoutButtonsHolder(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (getChildCount() == 0) return;
getChildAt(getChildCount() - 1).addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
for (int i = 0; i < getChildCount(); i ++) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
getChildAt(i).getMeasuredHeight());
getChildAt(i).setLayoutParams(lp);
}
}
});
}
}
Now in the xml file of your layout use LinearLayoutButtonsHolder
instead of LinearLayout
. Don't forget to set android:layout_width
each one Button
to wrap_content
:
<github.yaa110.widget.LinearLayoutButtonsHolder
android:id="@+id/adapter"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_height="wrap_content">
<Button
android:text="Long text for test"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:id="@+id/opt1"/>
<Button
android:text="2"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:id="@+id/opt2" />
<Button
android:text="3"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:id="@+id/opt3" />
<Button
android:text="4"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:id="@+id/opt4" />
<Button
android:text="5"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:id="@+id/opt5" />
</github.yaa110.widget.LinearLayoutButtonsHolder>