When an ImageButton is clicked a checkBox is created dynamically in my app.
The problem I'm facing is with toast messages not showing or let's say the listener I am using for the checkboxes to show a toast message when the checkbox is checked is working only for the current/latest checkbox that has been created. Not for the checkboxes that had been created previously.
Problem,
I saw that the problem could be with the variable (toDoCheckBox) I am using to save the newly created checkbox instance and every time it's getting overriding dynamically as I create newer checkboxes. So for the previous checkboxes, there is no reference pointing to them in order for the listener to work. (the problem could be this or not, i don't know for sure) Even if this is the problem i don't know how to solve it
What i tried is that, creating the instance of checkbox on both onCreate as well as the createCheckBox method so that I could assign the listener in both the places.
- on the onCreate()
- on the createCheckbox()
Only the latest created checkbox works and shows the toast messages
Can someone help me or guide me with the problem,
I want the toast message to show for every checkboxes
i posted only the code that could be of some use
private CheckBox toDoCheckBox;
private EditText defaultEditText;
private List<CheckBox> listCheckBoxes;
private int previousViewId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.defaultEditText = findViewById(R.id.defaultEditText);
toDoCheckBox = new CheckBox(this);
this.listCheckBoxes = new ArrayList<>();
this.checkBoxListener = new View.OnClickListener(){
@Override
public void onClick(View view){
if(toDoCheckBox.isChecked()){
toDoCheckBox.setChecked(true);
Toast.makeText(MainActivity.this,"You did a good work, keep it up!",Toast.LENGTH_SHORT).show();
}
}
};
toDoCheckBox.setOnClickListener(checkBoxListener);
}
public void createCheckBox() {
//CREATING A CHECKBOX
toDoCheckBox = new CheckBox(this);
toDoCheckBox.setText(defaultEditText.getText().toString());
toDoCheckBox.setTextSize(20);
toDoCheckBox.setId(View.generateViewId());
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();
ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
newParams.width = params.width;
newParams.height = params.height;
//Constraints
newParams.startToStart = params.startToStart;
newParams.endToEnd = params.endToEnd;
newParams.topToTop = params.topToTop;
newParams.topToBottom = params.topToBottom;
//Margins
newParams.leftMargin = params.leftMargin;
newParams.topMargin = params.topMargin;
newParams.bottomMargin = params.bottomMargin;
newParams.rightMargin = params.rightMargin;
constraintLayout.addView(toDoCheckBox, -1, newParams);
previousViewId = toDoCheckBox.getId();
defaultEditText.setVisibility(View.INVISIBLE);
createEditText();
toDoCheckBox.setOnClickListener(checkBoxListener);
this.listCheckBoxes.add(toDoCheckBox);
totalCheckBox++;
}
Aucun commentaire:
Enregistrer un commentaire