How to Create Button Dynamically in Android

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

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

Dynamically creating Buttons and setting onClickListener

You could create a method that returns an onclickListener and takes a button as a parameter. And then use that method to set the onClicklistener in the first loop you have..

Update: code could be soemthing along these lines:

View.OnClickListener getOnClickDoSomething(final Button button)  {
return new View.OnClickListener() {
public void onClick(View v) {
button.setText("text now set.. ");
}
};
}

as a method in the activity and then use it in the loop like this

button.setOnClickListener(getOnClickDoSomething(button));


Related Topics



Leave a reply



Submit