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
Tm.Getdeviceid() Is Deprecated
How to Create JSON Object Using String
Getlocationonscreen() VS Getlocationinwindow()
What Is the Use of Basecolumns in Android
How to Iterate Through the Id Properties of R.Java Class
Sha256Withrsa What Does It Do and in What Order
Jruby: Watir Is Hanging When Launching Browser
Jsoup Get Dynamically Generated HTML
How to Find Gcd, Lcm on a Set of Numbers
How to Get a String Between Two Characters
Noclassdeffounderror on External Library Project for Android
How to Print to the Console in Android Studio
Could Not Find Method in Parent or Ancestor Context
Endless Scrolling Listview Not Working
Java Apns Certificate Error with "Derinputstream.Getlength(): Lengthtag=109, Too Big."