How to Add a Button Dynamically in Android

How to add a button dynamically in Android using Kotlin

You can create a button dynamically by calling the constructor of the button.

var myButton = Button(this);

'this' will be the activity.

how to create button dynamically and add click event to button in android

Better way of doing and you can easily customize it in better way.

MainActivity.java

public class MainActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ScrollView sv = (ScrollView) findViewById(R.id.myscroll);
LinearLayout ll = (LinearLayout) findViewById(R.id.mylinearLayout);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for (int i = 0; i < 20; i++) {

View view = inflater.inflate(R.layout.coustom_check_box_design, null);
CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox);
cb.setText("I'm dynamic!" + i);
//cb.isChecked();
ll.addView(view);
}
}
}

activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/myscroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">


<LinearLayout
android:id="@+id/mylinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>

coustom_check_box_design.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="vertical">

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox" />
</LinearLayout>

How to create Button Dynamically in android?

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

Adding buttons to Fragments dynamically

In the end, I have manged to do it. The second method had problem as there is no

btn = myView.findViewById(id_);.
This will be pointing to null although I have set my button's ID to be the value I stated. Using this method, it is easy to still get the value of the button by setting a separate onClick() method.

            for (int i = 0; i < ArrayOfNames.length; i++) {

final Button myButton = new Button(myView.getContext());
myButton.setText(RAnames[i]);
myButton.setId(i + 1);
myButton.setOnClickListener(this);

myButton.setBackgroundColor(getResources().getColor(R.color.colorAccent));
myButton.setTextSize(18);
myButton.setPadding(20, 0, 20, 0);

LinearLayout linearlayout = (LinearLayout) myView.findViewById(R.id.btnholder);
linearlayout.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0, 0, 0, 10);

linearlayout.addView(myButton, buttonParams);
}

How can I dynamically create a button in Android?

Firstly 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(R.id.layout1);
layout.addView(myButton);


Related Topics



Leave a reply



Submit