Android: programmatically adding buttons to a layout

前端 未结 5 2085
梦如初夏
梦如初夏 2020-11-29 07:40

I\'m trying to get an add button to add another button to the layout, based on the edittext to the left of the button. The point is for a person to list the rooms in their

5条回答
  •  执念已碎
    2020-11-29 08:03

    Try this :

        //the layout on which you are working
        LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
    
        //set the properties for button
        Button btnTag = new Button(this);
        btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        btnTag.setText("Button");
        btnTag.setId(some_random_id);
    
        //add button to the layout
        layout.addView(btnTag);
    

提交回复
热议问题