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

johnson

I need to add a button to the activity in android.

And when I click this button I need to get which checkbox is checked and unchecked.My code is

package com.example.a;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);
        for(int i = 0; i < 20; i++) {
            CheckBox cb = new CheckBox(this);
            cb.setText("I'm dynamic!");
            ll.addView(cb);
            }
            this.setContentView(sv);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Sohail Zahid

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android: Dynamically create image button with click event

From Dev

how to add button click event in android studio

From Dev

how to create button dynamically and add the onclick event for html page

From Dev

How to add a click effect to a button on click event?

From Dev

Add spinner with click of a button dynamically in Android

From Dev

Add spinner with click of a button dynamically in Android

From Dev

how to create random UUID in Android when button click event happens?

From Dev

How to dynamically add directives on button-click

From Dev

How create datatables dynamically on button click

From Dev

How to dynamically create objects on the fly with click of a button

From Dev

How to add a row into datatable on button click event?

From Dev

How to add a button with click event on UITableViewCell in Swift?

From Dev

How to add a click event to a button in a chrome extension?

From Dev

How to add an item to a listview on a button click event?

From Dev

Button Click event not firing for dynamically created button

From Dev

How to create Button_Click() Event for Bootstrap Button?

From Dev

Create controls dynamically on button click

From Dev

Dynamically create and add new component on button click in Angular Dart

From Dev

In android, how do I dynamically swap a fragment by button click or other event?

From Dev

How to add button to a view dynamically in android xamarin?

From Dev

How to dynamically add listener to button on customAdapter in android

From Dev

How to add button to a view dynamically in android xamarin?

From Dev

How to save add button dynamically in android?

From Dev

Add Event to UI button click

From Dev

Add Button Click Event in RecyclerView

From Dev

Add Event to UI button click

From Dev

How to fire dynamically created button click event inside TableRow?

From Dev

How to catch click event with jQuery, on dynamically created button

From Java

How to add values to JSONObject on button click in android?

Related Related

  1. 1

    Android: Dynamically create image button with click event

  2. 2

    how to add button click event in android studio

  3. 3

    how to create button dynamically and add the onclick event for html page

  4. 4

    How to add a click effect to a button on click event?

  5. 5

    Add spinner with click of a button dynamically in Android

  6. 6

    Add spinner with click of a button dynamically in Android

  7. 7

    how to create random UUID in Android when button click event happens?

  8. 8

    How to dynamically add directives on button-click

  9. 9

    How create datatables dynamically on button click

  10. 10

    How to dynamically create objects on the fly with click of a button

  11. 11

    How to add a row into datatable on button click event?

  12. 12

    How to add a button with click event on UITableViewCell in Swift?

  13. 13

    How to add a click event to a button in a chrome extension?

  14. 14

    How to add an item to a listview on a button click event?

  15. 15

    Button Click event not firing for dynamically created button

  16. 16

    How to create Button_Click() Event for Bootstrap Button?

  17. 17

    Create controls dynamically on button click

  18. 18

    Dynamically create and add new component on button click in Angular Dart

  19. 19

    In android, how do I dynamically swap a fragment by button click or other event?

  20. 20

    How to add button to a view dynamically in android xamarin?

  21. 21

    How to dynamically add listener to button on customAdapter in android

  22. 22

    How to add button to a view dynamically in android xamarin?

  23. 23

    How to save add button dynamically in android?

  24. 24

    Add Event to UI button click

  25. 25

    Add Button Click Event in RecyclerView

  26. 26

    Add Event to UI button click

  27. 27

    How to fire dynamically created button click event inside TableRow?

  28. 28

    How to catch click event with jQuery, on dynamically created button

  29. 29

    How to add values to JSONObject on button click in android?

HotTag

Archive