Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dynamically create a button in Android? [duplicate]

Tags:

android

Possible Duplicate:
how to add button dynamically in android?

How can I dynamically create a button in Android?

like image 944
James Avatar asked Jun 10 '10 01:06

James


1 Answers

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);
like image 96
Dan Kilpatrick Avatar answered Dec 04 '22 19:12

Dan Kilpatrick