4

Possible Duplicate:
how to add button dynamically in android?

How can I dynamically create a button in Android?

2
  • 2
    DynamicAndroidButton button = new DynamicAndroidButton(); The only work remaining is implementing this class. Commented Jun 10, 2010 at 1:42
  • If you had any rep, you could put up bounty :) Commented Jun 11, 2010 at 0:14

1 Answer 1

13

Firstly add the appropriate import to your Activity:

import android.widget.Button;

Then create a new button object within the onCreate method:

Button myButton = new Button(this);
myButton.setText("Press Me");

Finally add the button to the layout:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout1);
layout.addView(myButton);
Sign up to request clarification or add additional context in comments.

1 Comment

How can I set its width to fill_parent? The setWidth method of the Button accepts only int.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.