Vamsi Challa
Vamsi Challa

Reputation: 11109

set width and height of components programmatically

Application developed in Android 2.3.3

I am developing a calculator application.

Question 1 ::: I have around 16 buttons. Is there a way where in I can set the width and height of all the buttons using a loop (or) without it. I want all the buttons to be uniform.

Question 2 ::: What do you think of this practice? Good or Bad? Please explain why? Let's say I have 4 buttons in each row. If I get the width and height of the screen programmatically and then divide (width/4) and add the margin for each button and then set the width (width/4 + margin) of the buttons respectively, will that somehow solve the problem of displaying in screens with different sizes?

Upvotes: 3

Views: 8900

Answers (5)

jeet
jeet

Reputation: 29199

Best way to equally provide the width or height of views according to device width and height, is to use weight property, lets take an example, we have a linear layout, with four buttons, of equal width:

<LinearLayout android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">
     <Button android:layout_width="0dip"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="button1"/>
     <Button android:layout_width="0dip"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="button2"/>
     <Button android:layout_width="0dip"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="button3"/>
     <Button android:layout_width="0dip"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="button4"/>
</LinearLayout>

However, if you still want to provide layout width and height at runtime, you can use setLayoutParams(new LayoutParams(100, 100)); method.

Upvotes: 5

Ajay S
Ajay S

Reputation: 48602

Steps to set the width and height of view at run time

First get layout params using view.getLayoutParams().

Second then set the height and width value then last set the layout params of view.

LayoutParams params = null;
Button button = (Button)findViewById(R.id.button);
params = button.getLayoutParams();
params.height = 70;
params.width = 70;
button .setLayoutParams(params);

Upvotes: 2

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

Try This code

LayoutParams params = null;
Button button = (Button)findViewById(R.id.button);
params = button .getLayoutParams();
params.height = 100;
params.width = 100;
button .setLayoutParams(params);

Upvotes: -2

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

Things to remember:

  • set the android:layout_width of the children to "0dp"
  • set the android:weightSum of the parent (edit: as Jason Moore noticed, this attribute is optional, because by default it is set to the children's layout_weight sum)
  • set the android:layout_weight of each child proportionally (e.g. weightSum="5", three children: layout_weight="1", layout_weight="3", layout_weight="1")

Example:

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="1" />

 <Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="1" />

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="1" />

 <Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="1" />

  <Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="1" />

</LinearLayout>

enter image description here

Upvotes: 2

Matt Clark
Matt Clark

Reputation: 28609

You could do it by setting up your elements in the XML and then modifying the height and width in the Java code:

Button btn1 = (Button)findViewById(R.id.btn1);    
LayoutParams layoutParams = btn1.getLayoutParams();
layoutParams.width(int width);
layoutParams.height(int height);
btn1.setLayoutParams(layoutParams);

Or you could just use Java to create the whole layout:

for(int x = 0; x<3; x++){
    for(int y=0; y<3; y++){
        Button btn = new Button(this);
        ... //Set layoutParams and button info for each button.
    }    
}

Or one more option would be to use a linearLayout and use element weights.

<Button
    android:id="@+id/register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    weight=".3" />

Upvotes: 0

Related Questions