19

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?

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251

5 Answers5

14

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).

Jean-Philippe Jodoin
  • 4,536
  • 1
  • 25
  • 28
13

Instead of android:layout_width="wrap_content" you can use a fixed pixelsize like for example android:layout_width="20px" or a better way: android:layout_width="20dp"

you can also set the width programatically in the code: Button.setWidth(int width)

Concerning your update:

I don't know if it is a good solution, but i works. You could use

 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);

to read the screen resolution and then set the button sizes according to metrics.widthPixel and metrics.heightPixel

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • what is pd in 20pd.. as shown in above eg. Instead of android:layout_width="wrap_content" you can use a fixed pixelsize like for example android:layout_width="20px" or a better way: android:layout_width="20pd" –  Sep 03 '11 at 04:19
  • 1
    i believe it should have been `density pixel` `dp` http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android – Marek Sebera Jan 21 '12 at 09:44
  • dp is density independent pixel. The idea is that if you use this Android will scale it up or down depending on the screen size. You can see the developer pages for more info: http://developer.android.com/guide/practices/screens_support.html – Jonny May 28 '12 at 08:00
7

Use minWidth and minHeight to set the width and height of a button.

<Button android:layout_height="wrap_content"
 android:id="@+id/Button01" 
 android:layout_below="@id/output" 
 android:text="7" 
 android:minWidth="100dp">
</Button>
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
4

The best and easiest way to set a button dynamically is

 Button index=new Button(this);
 int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());             
 int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());

The height and width in pixels px. 45 being the height in dp and 42 being the width in dp.

 index.setLayoutParams(new <Parent>.LayoutParams(width, height));

So, for example, if you've placed your button within a TableRow within a TableLayout, you should have it as TableRow.LayoutParams

 index.setLayoutParams(new TableRow.LayoutParams(width, height));
Ali Kahoot
  • 3,371
  • 2
  • 22
  • 27
0

add android:minwidth line in that code.

Iamat8
  • 3,888
  • 9
  • 25
  • 35
Rathiga Jesika
  • 357
  • 4
  • 16