Reputation: 11109
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
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
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
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
Reputation: 13785
Things to remember:
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>
Upvotes: 2
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