How to create Button Dynamically in android?

50,722

Solution 1

Create/Remove button onClick of + button and - button as below:

  public void onClick(View v) {

     switch(v.getId()){
     case (R.id.plusbutton):
                 Button myButton = new Button(this);
                 myButton.setText("Add Me");

                 LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
                 LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                 ll.addView(myButton, lp);
                 break;.
     case (R.id.minusbutton):
                 Button myButton = new Button(this);
                 myButton.setText("Remove Me");

                 LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
                 LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                 ll.removeView(myButton, lp);
                 break;
           }
         }

Solution 2

this is for create button dynamically in android

LinearLayout row2 = (LinearLayout) findViewById(R.id.hll2);
Button ivBowl = new Button(this);
ivBowl.setText("hi");
LinearLayout.LayoutParams layoutParams = new  LinearLayout.LayoutParams(70, 70);
layoutParams.setMargins(5, 3, 0, 0); // left, top, right, bottom
ivBowl.setLayoutParams(layoutParams);
row2.addView(ivBowl);

Solution 3

LinearLayout mainLayout = (LinearLayout)findViewById(R.id.yourlayoutidthatisonethepicture);

Button addButton =new Button(this);
addButton.setText("add");

mainLayout.addView(addButton);

to remove is the same just change this "mainLayout.addView(addButton)" to removeView or setVisibility of button to View.GONE

Solution 4

It's pretty simple.

    Button button1=new Button(context);
    button1.setText("test");
    button1.setId(id);
containerlayout.add(button1);

Hope this helps you.

Solution 5

If you want to create dynamic view (like EditText,textview etc) then just use this code and run it in your application.

MyActivity.java://your java file

 LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
 EditText et = new EditText(v.getContext());
 et.setText("My new Edit Text);
 et.setMinLines(1);
 et.setMaxLines(3);
 ll.addView(et);

In XML File:

 <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/TextView01"
android:layout_below="@+id/relativeLayout1"
android:orientation="vertical" >

Share:
50,722
Smit Christian
Author by

Smit Christian

Updated on January 18, 2020

Comments

  • Smit Christian
    Smit Christian over 4 years

    Links

    I want to create a page like this. these 7 buttons are already exist but if user want to add more categories (button) then he can do using + button and delete using - button. Any idea or tutorial for making this?

  • Smit Christian
    Smit Christian about 11 years
    Thank you sir :) appreciate your help
  • Smit Christian
    Smit Christian about 11 years
    what is buttonlayout? how to create that
  • Smit Christian
    Smit Christian about 11 years
    Still not getting "R.id.yourlayoutidthatisonethepicture" please tell me
  • Awais Usmani
    Awais Usmani almost 10 years
    i m getting issues with height. i am using addButton.setHeight(20) but nothing happes , if u guide my why is so?