Is it possible to evenly distribute buttons across the width of a LinearLayout
Expanding on fedj's answer, if you set layout_width
to 0dp
and set the layout_weight
for each of the buttons to 1, the available width will be shared equally between the buttons.
How to evenly space round ImageButtons with LinearLayout
A solution I found was to use empty Space
s around and in between the buttons and have those take the extra space
<Space
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</Space>
I had to remove the weight from the actual buttons and set the width to wrap_content
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add_black_24dp"
android:background="@drawable/rounded"
android:contentDescription="@string/speak_button_desc"/>
Three buttons evenly divided linear layout
Try this, I have removed Properties you should use with RelativeLayout
and unnecessary with LinearLayout
, and buttons aligned horizontally,
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
and assigned Weightsum to LinearLayout along with button height to match parents
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="1">
<Button
android:id="@+id/resetDatesButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:onClick="resetDates"
android:text="Rese check dates" />
<Button
android:id="@+id/homeButtonSearch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:onClick="home"
android:text="home" />
<Button
android:id="@+id/submitchanges"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:onClick="submitChanges"
android:text="submit changes" />
</LinearLayout>
</RelativeLayout>
Result
Evenly distribute textviews across linear layout horizontally and wrap content
Fixed it by assigning weight to 1 to all TextViews except the last one.
public void drawLabels(List<String> labelsValues, ViewGroup container) {
for (int i = 0; i < labelsValues.size(); i++) {
String label = labelsValues.get(i);
TextView textView = new TextView(mContext);
textView.setText(label.trim());
if (i == labelsValues.size() - 1) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(params);
} else {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
textView.setLayoutParams(params);
}
container.addView(textView);
}
}
How to align horizontally 3 fix width buttons in LinearLayout?
Basically there are 3 steps involved.
Set weightSum="3" to the parent layout. This means that the sum of the entire layout_weights is 3.
Set layout_weight="1" to each of the individual buttons. So each individual button has 1/3rd the size of the parent.
Finally set layout_width="0dp", this is important because here you don't have to set the width of the view. It will be set automatically by the layout handler.
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button2"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button3"
android:layout_weight="1"/>
</LinearLayout>
Is it possible to evenly distribute buttons across the width of an android RELATIVE LAYOUT
How about throwing a LinearLayout inside your RelativeLayout, and adding the Buttons inside the LinearLayout (all of the buttons should have the same layout_weight).
Is it possible to evenly distribute the number of buttons in GridView?
If you're developing for API 21 and higher, GridLayout
supports the concept of "weight", which may be familiar to you from LinearLayout
. So you could give every cell these attrs:
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
And now your GridView
(of whatever size you gave it) would be divided into three even rows and three even columns.
If you have to support earlier API levels, your best option is to use ConstraintLayout
. With this strategy, you'd again give every child a width+height of 0dp, but now you'd set up chains of constraints so that each row was split evenly and each column was split evenly. Something like:
<View
android:id="@+id/child1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/child2"/>
<View
android:id="@+id/child2"
app:layout_constraintLeft_toRightOf="@id/child1"
app:layout_constraintRight_toLeftOf="@id/child3"/>
<View
android:id="@+id/child3"
app:layout_constraintLeft_toRightOf="@id/child2"
app:layout_constraintRight_toRightOf="parent"/>
Do the same thing in the vertical direction:
<View
android:id="@+id/child1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/child4"/>
<View
android:id="@+id/child4"
app:layout_constraintTop_toBottomOf="@id/child1"
app:layout_constraintBottom_toTopOf="@id/child7"/>
<View
android:id="@+id/child7"
app:layout_constraintTop_toBottomOf="@id/child4"
app:layout_constraintBottom_toBottomOf="parent"/>
Android ConstraintLayout : Three buttons equally distribute across the width
Here is a visual example.
- Select the views
- Right click and choose Chain > Create Horizontal Chain
See also
- ConstraintLayout: pack vs chain
Related Topics
Load Dimension Value from Res/Values/Dimension.Xml from Source Code
Fit Image in Imagebutton in Android
Adb - Android - Getting the Name of the Current Activity
Fragment Over Another Fragment Issue
Programmatically Set Left Drawable in a Textview
Android: When Should I Use a Handler() and When Should I Use a Thread
Add Padding on View Programmatically
How to Change Color in Circular Progress Bar
Disabling User Dragging on Bottomsheet
Viewpager in a Nestedscrollview
Cannot Resolve Symbol 'Appcompatactivity'
Slide a Layout Up from Bottom of Screen
How to Use View Pager with Views (Not with Fragments)
How Does the Getview() Method Work When Creating Your Own Custom Adapter
Further Understanding Setretaininstance(True)
Validate Username and Email Crashed and Cannot Insert to Firebase Database
How to Create Xxhdpi, Xhdpi, Hdpi, Mdpi and Ldpi Drawables from a Large Scale Image