Johann
Johann

Reputation: 2017

Limit the width of buttons in Android

I have two buttons positioned horizontally next to each other and want to limit the width of the buttons when the screen resolution goes up. For mdpi, the two buttons should be equal length and take up the entire width of the screen. If you then switch to a higher resolution, the two buttons can grow in width if necessary but only up to a maximum width of say 100dp. I have not been successful in accomplishing this. The two buttons end up being the same size but they fill the entire width of the screen. I have tried putting the buttons in a table, in just a LinearLayout and have tried using the maxWidth on various combinations but to no avail. If I use a table, I have tried stretching the columns and not stretching them. That didn't help either. Any suggestions?

Upvotes: 1

Views: 5949

Answers (3)

Kevin Coppock
Kevin Coppock

Reputation: 134664

You say you've tried maxWidth...but you don't show any examples of what you tried, so here's my suggestion, which I'm nearly certain should work for you:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxWidth="100dp"
        android:text="Button 1"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxWidth="100dp"
        android:text="Button 2"
        />
</LinearLayout>

Upvotes: 2

Egor
Egor

Reputation: 40203

You can try creating different layouts for different resolutions. This is not the best way of solving your problem, but this will surely work. Good luck!

Upvotes: 0

Gangnus
Gangnus

Reputation: 24464

You haven't put your xml view here. How can we find an error? Probably, you have used background image and width set to wrapcontent ? Change the source image size to smth small

Upvotes: 0

Related Questions