1

Create a button by this code is possible but I want click on a button and create new button(or anything else) again and again. How can do it?

Button b = new Button();
tr.setLayoutParams(new TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.WRAP_CONTENT));
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(b,index);
tl.addView(tr, new TableLayout.LayoutParams(
        TableLayout.LayoutParams.MATCH_PARENT,
        TableLayout.LayoutParams.WRAP_CONTENT));

5 Answers 5

1

Add Onclick listener to the button and add the button to the existing table

 b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      // add button in table
       tr.addView(b,index);
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0
public class test extends Activity {

    private List<Button> mButtons = new ArrayList<Button>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teeest);

        final LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout);


        Button b = new Button(this);
        b.setText("Add Dynamic Button");
        b.setBackgroundColor(Color.GREEN);
        b.setVisibility(View.VISIBLE);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Button b1 = new Button(teeest.this);
                b1.setText("DYNAMIC BUTTON" + mButtons.size() + "");
                b1.setWidth(100);
                b1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(teeest.this, "New Dynamic Button clicked", Toast.LENGTH_SHORT).show();
                    }
                });

                mButtons.add(b1);
                ll.addView(b1);
            }
        });

        Button bb = new Button(this);
        bb.setText("Remove Dynamic Button");
        bb.setBackgroundColor(Color.RED);
        bb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mButtons.size()>0) {
                    ll.removeViewAt(mButtons.size() + 1);
                    mButtons.remove(mButtons.size() - 1);
                }
            }
        });

        ll.addView(b);
        ll.addView(bb);

    }
}

Comments

0
Button newButton(){
 Button b = new Button();
            tr.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            b.setText("Dynamic Button");
            b.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            tr.addView(b,index);
            tl.addView(tr, new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
}

newButton().setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        newButton();
    }
})

You can write code like this.... Try yourself...

Comments

0

If you want to create a new Button every time when any button is clicked, you can implement OnClickListener in your activity, and override onClick method.

Example snippet:

public class MainActivity extends Activity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //your code
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //create a new button or textview
    }
}

Comments

0
Button b = new Button();
...
b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }
})

1 Comment

This does not fully answer the question. It shows how to respond to a click, but does not attempt to answer the requirement of creating a new View.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.