How to create Button Dynamically in android?

后端 未结 5 898
抹茶落季
抹茶落季 2020-11-30 05:02

\"Links\"

I want to create a page like this. these 7 buttons are already exist but if user want to add more cat

5条回答
  •  遥遥无期
    2020-11-30 05:46

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

提交回复
热议问题