0

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

}
1
  • is your problem solved? Commented Aug 5, 2016 at 9:30

4 Answers 4

1
  LinearLayout ll = new LinearLayout(this);
   ll.setOrientation(LinearLayout.VERTICAL);  
    Button btn = new Button(this);
    btn.setText("Submit");
    btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    ll.addView(btn);
    sv.addView(ll);
    btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                 //code
    }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

@johnson glad it helped. Please accept and upvote the answer so that it can be helpful for others too. Thanks in advance. :)
0

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>

2 Comments

@johnson if it help you to manage it in better please vote up and tick mark.
thanks sohail but unfortunately i dont have enough points to upvote answers
0

See the below one it may help you:-

     LinearLayout ll = new LinearLayout(this);
     ll.setOrientation(LinearLayout.VERTICAL);

     Button btn = new Button(this);
     btn.setText("Submit");
     btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
     ll.addView(btn);
     sv.addView(ll);
     btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // implement the loop to get checked CheckBoxes
    }
    });

Comments

0

Try this to get the THe selected Checkbox using Arraylist

 public class MainActivity extends AppCompatActivity {

private ArrayList<String> mCheckList=new ArrayList<String>();

@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++) {
        final CheckBox cb = new CheckBox(this);
        cb.setText("check"+i);
        ll.addView(cb);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {
                    mCheckList.add(cb.getText().toString());
                }
                else
                {
                    mCheckList.remove(cb.getText().toString());
                }
            }
        });

    }

    Button button=new Button(this);
    button.setText("Submit");
    ll.addView(button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,mCheckList.toString() +"CheckBox Select",Toast.LENGTH_SHORT).show();
        }
    });
    this.setContentView(sv);


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.