Domanda

I have a TO DO List and when the CheckBox is ticked. I want the color in the ListView row where the CheckBox is to change.

This is the code I have done but it does not work.

public class CheckBoxCheck extends Activity{

CheckBox check;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   check = (CheckBox) findViewById(R.id.checkBox1);
   check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            check.setBackgroundColor(Color.BLUE);
        }else{
            check.setBackgroundColor(Color.BLACK);
        }

    }
});

}
 }

Could You Please Help.

È stato utile?

Soluzione 2

This should be pretty easy. The checkbox can have a text associated. So simply put the text in the checkbox and give the text a color which is a . In the selector xml you can easily specify the colors for the normal and the checked state.

Altri suggerimenti

You should try this:

checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkBox.isChecked()) {
                    listView.setBackgroundColor(Color.parseColor("#008000"));
                } else {
                    listView.setBackgroundColor(Color.parseColor("#FF0000"));
                }
            }
        });

I have a TO DO List and when the CheckBox is ticked. I want the color in the ListView row where the CheckBox is to change.

The code you posted does not show any ListView. So that would be the first problem.

If you're trying to change the row color of a listview, you need to implement getView() of the adapter that feeds the ListView and update the view that is returned appropriately.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top