Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buttons should be as wide as the biggest

I want all Button to fit the text, so there is no line breaks in the Buttons, but also all the Button should has the same width: as wide as the biggest.

I achieved this with setting all the Button layout_width's to be WRAP_CONTENT in the layout:

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    ...

</LinearLayout>

Then i search for the biggest value, and set all the buttons to be same sized:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);


    ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
    int size = 0;
    for (int i = 0; i < layout.getChildCount(); ++i) {
        View child = layout.getChildAt(i);
        if (child.getWidth() > size) {
            size = child.getWidth();
        }
    }

    for (int i = 0; i < layout.getChildCount(); ++i) {
        LayoutParams params = layout.getChildAt(i).getLayoutParams();
        params.width = size;
        layout.getChildAt(i).setLayoutParams(params);
    }
}

Is there any cleaner, more elegant solution for this?

like image 856
WonderCsabo Avatar asked Dec 06 '13 08:12

WonderCsabo


People also ask

How wide should buttons be?

Button Size Standard Buttons that were over 72 pixels also had low accuracy. The highest accuracy was found with buttons between 42-72 pixels. This means that 42 pixels is the minimum and 72 pixels is the maximum button size that's most optimal for users.

Should mobile buttons be full width?

Depending on layout, orientation and purpose but it's a good rule of thumb to add at least an 8dp margin. Keep in mind touch space limitations, for example on Android, Material Design deems as good practice to have buttons that are at least 48x48 dp wide so the area of the buttons is comfortable to use for the users.

What is the average Button size?

A 1.5” button is the most popular size (unless you want to make pocket mirrors, then look to our 2.25, 3", or 3.5" fabric covered button makers). Popular with teachers, who add them to school IDs. And nurses, who wear them on stethoscopes. Fabric buttons can be made in any size but there are stipulations.


1 Answers

Wrap, all the button inside a common parent Say linearlayout having attributes as wrap_content, wrap_content. use the attributes Match_Parent for your button, now programmatically you just calculate the size of biggest text and set it as parent width, and call layout over it, this will make sure all the child it contains gets resized efficiently.

<LinearLayout  <!--Parent-->
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <!-- Child -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
like image 156
Techfist Avatar answered Nov 05 '22 16:11

Techfist