<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
android:id="@+id/linear_layout"
// The layout and buttons are declared as
// final because they are referenced in the
// event handler in the inner class.
final LinearLayout layout = (LinearLayout)
findViewById(R.id.linear_layout);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
//CharSequence[ ] months = {
String[ ] months = {
"January ", "February", "March ", "April ", "May ",
"June ", "July ", "August ", "September",
"October ", "November", "December "};
for (int i = 1; i <= 12; i++) {
final Button btn = new Button(this);
btn.setId(i);
btn.setText(months[i - 1]);
btn.setLayoutParams(params);
layout.addView(btn);
}
btn.setOnClickListener(new View.OnClickListener( ) {
public void onClick(View v) {
Button b =(Button) v;
String label = b.getText( ).toString( );
System.out.println("Text: " + label);
Toast toast = Toast.makeText(
getApplicationContext(),
"Clicked Button:" + label,
Toast.LENGTH_LONG);
toast.show( );
}
});