You can create a button in the code with:
Button myButton = new Button(); myButton.setLayoutParams(myLayoutParams); myButton.setText(myText);
To display Toast, add onClickListener:
myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {
Then, to add them to your screen, you will need to have a container defined in xml to hold them, like LinearLayout. Get LinearLayout with:
LinearLayout myContainer = findViewById(R.id.myButtonId); //Make sure you set the android:id attribute in xml
You probably want this code outside of your loop, perhaps right in front of it, so you don't get a layout at every iteration.
Then, in a loop, after your button is configured:
myContainer.addView(myButton);
Finally, a note on the myLayoutParams
variable. When you set the options for the view, they must match the parent view of your widget. Therefore, if you put a Button in a LinearLayout, it will look like
//This will make LayoutParameters with layout_width="wrap_content" and layout_height="fill_parent" LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT);
You can also put this outside your loop and reuse it for each button.