4

Possible Duplicate:
how to add button dynamically in android?

How can I dynamically create a button in Android?

Community
  • 1
  • 1
James
  • 13,523
  • 26
  • 66
  • 92

1 Answers1

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);
Dan Kilpatrick
  • 3,909
  • 1
  • 21
  • 15