Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set width of Button in Android

How can I set a fixed width for an Android button? Everytime I try to set a fixed width it fills the current parent (the RelativeView). Here is my XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/relativelayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

    <EditText android:layout_height="wrap_content" android:editable="false" android:layout_width="fill_parent" android:id="@+id/output"></EditText>

    <Button android:layout_height="wrap_content" android:id="@+id/Button01" android:layout_below="@id/output" android:text="7" android:layout_width="wrap_content"></Button>
    <Button android:layout_below="@id/output" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button02" android:layout_toRightOf="@+id/Button01" android:text="8"></Button>
    <Button android:layout_below="@id/output" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button03" android:layout_toRightOf="@+id/Button02" android:text="9"></Button>
</RelativeLayout>

How would I give it a FIXED width?
UPDATE
Say I have several buttons that I want the same size as each other and 1/3 of the entire view (Portrait), then I want a button with double the width. Then a button with double the height. How could I accomplish this other that manually sizing them?

like image 709
Mohit Deshpande Avatar asked May 09 '10 00:05

Mohit Deshpande


People also ask

How to set Button width in android?

To accomplish what you want, you can use a LinearLayout with a weightsum. For example, you can put a WeightSum of 9. If you put the weight of three button inside to 1 each. They will take 1/3 of the place, and you can put 2 for another object.

How to set Button height and width in android?

To create a button with fixed height and width, you can give values in both px or in dp. giving values in dp is more convenient , bec android automatically scale the height and width in ldpi,mdpi and hdpi devices. Show activity on this post. Show activity on this post.

How to adjust Button size in android studio?

Solution 1: please use dimens. xml for specifying with and height for buttons. for tablet,please create folder values-sw600dp,values-sw720dp and put your dimens.


1 Answers

To accomplish what you want, you can use a LinearLayout with a weightsum. For example, you can put a WeightSum of 9. If you put the weight of three button inside to 1 each. They will take 1/3 of the place, and you can put 2 for another object. This object will be twice as big as the other.

http://developer.android.com/reference/android/widget/LinearLayout.html#setWeightSum(float)

Edit: I would like to add that for this to work correctly, you have to set the width or height to 0px (depending on if it's an horizontal or a vertical layout).

like image 199
Jean-Philippe Jodoin Avatar answered Oct 08 '22 16:10

Jean-Philippe Jodoin