May be this duplicate question but I didn't find answer for me.
My ClickListener
for CheckBox
doesn't work.
This xml:
<CheckBox
android:id="@+id/checkbox_visibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/mat_card_padding"/>
This code from Activity:
mVisibilityCheckBox = (CheckBox) findViewById(R.id.checkbox_visibility);
mVisibilityCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// this method doesn't call
Toast.makeText(SettingsActivity.this, "isChecked - " + mVisibilityCheckBox.isChecked(), Toast.LENGTH_SHORT).show();
}
});
UPD I add code from answer - but this doesn't work for me(
mVisibilityCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// this doesn't work
Toast.makeText(SettingsActivity.this, "isChecked - " + mVisibilityCheckBox.isChecked(), Toast.LENGTH_SHORT).show();
}
}
});
mVisibilityCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
Toast.makeText(SettingsActivity.this, "isChecked - " + mVisibilityCheckBox.isChecked(), Toast.LENGTH_SHORT).show();
}
}
});
check this example.this could help you. http://www.bipinrupadiya.com/2013/08/android-checkbox-setoncheckedchangelist.html
Edited: the code required here is shown below:
CheckBox hin = (CheckBox) findViewById(R.id.chkHindi);
hin.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1) , Toast.LENGTH_LONG).show();
}
} );
Add this code and it will work
mVisibilityCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mVisibilityCheckBox.isChecked()) {
// this doesn't work
Toast.makeText(SettingsActivity.this, "isChecked - " + mVisibilityCheckBox.isChecked(), Toast.LENGTH_SHORT).show();
}
}
});
Hope this helps
mVisibilityCheckBox.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(satView.isChecked()){
System.out.println("Checked");
}else{
System.out.println("Un-Checked");
}
}
});
Simply add clickable="true"
in CheckBox xml.
For example :
<CheckBox
android:id="@+id/checkbox_visibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/mat_card_padding"
android:clickable="true"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.