Snoopy Snoopy - 6 months ago 19
PHP Question

Android Create Button Automatically When Data is Received

So I have an android project that gets data from php using JSON.
I want that if i received a data, two buttons will AUTOMATICALLY be created which is accept and decline and if i receive another data another set of two buttons will be created. I am thinking to create buttons inside the loop but I don't know if it's possible in android? Is this possible?


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(;   

To add buttons dynamically in a loop and add listener to it, you can do like this:

for(int i=0;i<nob;i++) {
Button btn = new Button(this);
final int index = i;
btn.setOnClickListener(new OnClickListener() {
    void onClick(View v) {
        Log.i("TAG", "The index is" + index);