0

Here is the code for my check box.

if (type.equalsIgnoreCase("checkbox")){

                String checkBoxText = dataObj.getString("checkboxname");
                checkBox = dynamicviews.CreateCheckbox(context,value,checkBoxText);

                id = R.id.gl + i + 9;
                if (j == 2) {
                    j = 0;
                    tableRow = new TableRow(context);
                    tableRow.setPadding(0, 10, 0, 10);
                    tableLayout.addView(tableRow);
                }

                j++;
                tableRow.addView(checkBox);
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                        String string = checkBox.getText().toString();
                        Log.i("checkbox",string);
                    }
                });
            }

and below code is for creating checkbox

public CheckBox CreateCheckbox(Context context,String checkName,String checkBoxText){
    checkBox = new CheckBox(context);
    checkBox.setGravity(Gravity.CENTER);
    checkBox.setTextAlignment(Gravity.CENTER);
    checkBox.setText(checkBoxText);
    checkBox.setTextColor(Color.WHITE);
    checkBox.setBackgroundResource(R.drawable.custom_rdbtn);
    checkBox.setButtonDrawable(new StateListDrawable());
    checkBox.setCompoundDrawablePadding(10);
    return checkBox;
}

and problem is am getting the value of last created checkbox for each one.

1
  • You could try storing the checkbox instances in an ArrayList<CheckBox>. Commented Jun 16, 2016 at 14:53

2 Answers 2

2
 checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                        String string = buttonView.getText().toString();
                        Log.i("checkbox",string);


                    }
                });

i got the solution

Sign up to request clarification or add additional context in comments.

Comments

0

You are adding the checkBoxobject to the tableRow BEFORE you set its setOnCheckedChangeListener - so it will only work for the last checkBox you've created.

To solve this, you simply need to do set the setOnCheckedChangeListener first, and then call the tableRow.addView(checkBox) - see the code below:

//first, set the setOnCheckedChangeListener on this checkBox,
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)     
    {   String string = buttonView.getText().toString();
       Log.i("checkbox",string);
    }
});
//only now do you add the view
tableRow.addView(checkBox);

Give it a try, I hope it helps.

1 Comment

Noted. Instead of checkBox.getText(), it should be buttonView.getText() - the onCheckedChanged method passes the checkBox view (CompoundButton) whose checked state has changed. I updated the code accordingly and it should work. The biggest issue was adding the checkBoxobject before setting the listener.

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.