Harsh Lakhani
Harsh Lakhani

Reputation: 125

programmatically resize button

I have created button programmatically in following way :

    Button button = new Button(this);
    button.setText("Previous");
    setContentView(button);

I am getting button but full screen occupied. How to resize it and place it as per my need ?

Upvotes: 2

Views: 892

Answers (2)

Mohit Verma
Mohit Verma

Reputation: 3025

In your OnCreate method:

LinearLayout layout=new LinearLayout(this);

                Button button = new Button(this);
                button.setText("Previous");
                button.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                layout.setOrientation(LinearLayout.VERTICAL);
                layout.addView(button);
                setContentView(layout);

Upvotes: 2

Aprian
Aprian

Reputation: 1728

full screen occupied because you setContentView the button.

if what you want to do is resizing View in android. use LayoutParams. for example :

button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

doing that will resize the button to fit the text "Previous".

ok, you can try this tutorial.

Upvotes: 1

Related Questions