How to receive a event on android checkbox check change? - android

What would be the correct way of receiving and sending an event when a check box gets enabled or disabled?
In C# I could just easily double click and all the code would be done for me. But in android it appears to be a bit more obscure. I thought of using the touch event handlers but then if the user has a keyboard it won't detect the change since it's not touch. I figure android should have a native event for check box state change.

CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});

Since CheckBox (eventually) extends View, you can use a standard OnClickListener to detect when the CheckBox is actually tapped by the user (as opposed to the ListView updates):
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( ((CheckBox)v).isChecked() ) {
// perform logic
}
}
});

In Kotlin:
checkBoxView.setOnCheckedChangeListener { _, isChecked ->
print("checked: $isChecked")
}

Try this
CheckBox checkbox=(CheckBox)findViewById(R.id.checkbox);
checkbox.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (checkbox.isChecked())
{
//Perform action when you touch on checkbox and it change to selected state
}
else
{
//Perform action when you touch on checkbox and it change to unselected state
}
}
});

Related

Disable Android checkbox listener

I am working on a data collection application that while in an active state locks the screen to prevent errant user interaction. I would like to "lock" the checkbox while in this state to prevent the user from checking or unchecking the box. While other buttons on the screen still "click" when in this state, (listener is active) their events are not executed when the boolean bLockedScreen == true. I'd like for my boolean flag (bLockedScreen) when true to disable the check box listener.
What is the best way to go about doing this? TIA
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
ckbxVerbose = (CheckBox) v.findViewById(R.id.ckbxVerbose);
return v;
}
public void verbose(){
if(ckbxVerbose.isChecked()){
//do something
}
Update: Resolved this by putting,
ckbxVerbose.setEnabled(false);
in the method that set the boolean flag to disable screen widgets. Will likely do this as well with the other buttons that are inactive while data is being collected. Thank you Prince Ali for your answer.
You can disable or enable the CheckBox by calling setEnabled and setting its value to false or true. Here is a minimal working example involving 2 CheckBoxes, if the first CheckBox is checked, then disable the second one, otherwise enable it:
ckbxVerbose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if ( isChecked ) {
ckbxVerbose2.setEnabled(false);
} else {
ckbxVerbose2.setEnabled(true);
}
}
}
);
Another example can be:
CharSequence str = txt.getText();
if ( str.equals("Checked") ) {
ckbxVerbose2.setEnabled(false);
} else {
ckbxVerbose2.setEnabled(true);
}

Check box only works once?

In my Android app there is a password editText than normally displays the dots instead of letters, under it there is a check box that is labeled Show Password. When the check box is checked for the first time the password displays but if unchecked it does not re-hide. I switched the state from hide at start to show at start and the first uncheck of the check box hides but then won't un-hide on subsequent clicks. The only code involved with this is:
public void chkShowKey_click ( View v ) {
if ( showKey.isChecked ( ) ) {
txtPassKey.setInputType ( 144 );
} else {
txtPassKey.setInputType ( 128 );
}
}
What is wrong? Is the isChecked() not changing before the if statement checks the value?
When I try:
YourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { #Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { //NOW USE THE BOOLEAN TO CHECK IF CHECKED } } );
I get
and
Further testing with this code
chkShowKey.setOnCheckedChangeListener ( new CompoundButton.OnCheckedChangeListener ( ) {
#Override
public void onCheckedChanged ( CompoundButton buttonView, boolean isChecked ) {
if ( isChecked ) {
txtPassKey.setInputType ( InputType.TYPE_TEXT_VARIATION_NORMAL );
FancyToast.makeText(MainActivity.this,"Show",FancyToast.CONFUSING,FancyToast.LENGTH_SHORT,false).show();
} else {
txtPassKey.setInputType ( InputType.TYPE_TEXT_VARIATION_PASSWORD );
FancyToast.makeText(MainActivity.this,"Hide",FancyToast.CONFUSING,FancyToast.LENGTH_SHORT,false).show();
}
//FancyToast.makeText(MainActivity.this,"Checkbox changed!",FancyToast.CONFUSING,FancyToast.LENGTH_SHORT,false).show();
}
}
);
Shows that the event is firing and the isChecked is changing, everything is working EXCEPT txtPassKey.setInputType ( InputType.TYPE_TEXT_VARIATION_PASSWORD );
Try Below code according to your Requirement. It will be work just change according to your Checkbox id.
Event for Checkbox
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()) {
case R.id.chk1:
if (checked)
{System.out.println("if Part);}
else
{System.out.println("Else Part);}
break;
Perform your logic
}
}
You can simply set an setOnCheckedChangeListener and Then check if Checked inside the Click Event and Deal with editText Accordingly
YourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
//NOW USE THE BOOLEAN TO CHECK IF CHECKED
}
}
);
If you r trying to show adn Hide password Follow below
To show or hide dots instead of the password set the PasswordTransformationMethod:
yourEditText.setTransformationMethod(new PasswordTransformationMethod());
of course you can set this by default in your edittext element in the xml layout with
android:password
To re-show the readable password, just pass null as transformation method:
yourEditText.setTransformationMethod(null);

How to get Focus on EditText after checked CheckBox?

When user launch app then I don't want to allow user to edit in EditText. Now what I want that when user click on CheckBox then user able to edit EditText but below is my code and when I checked the checkbox it is not giving focus. how can I achieve this ?
cbIsGap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
editText1.setFocusable(true);
editText1.setClickable(true);
editText2.setClickable(true);
editText2.setFocusable(true);
}else {
editText1.setFocusable(false);
editText1.setClickable(false);
editText2.setFocusable(false);
editText2.setClickable(false);
}
}
});
editText.setFocusableInTouchMode(true);
editText.requestFocus();
try this when checked CheckBox
txtMobile.requestFocus();
You should use EditText.requestFocus();
MethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);
According to my experience with checkboxes, onClickListener provide reliable results as compared to checkedChangeListener. So I would suggest to use clickListener like this
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkbox.isChecked())
{
txtConsAcNo.setFocusable(true);
txtConsAcNo.setClickable(true);
txtMeterSrMo.setClickable(true);
txtMobile.setFocusable(true);
}else{
txtConsAcNo.setFocusable(false);
txtConsAcNo.setClickable(false);
txtMeterSrMo.setFocusable(false);
txtMeterSrMo.setClickable(false);
}
}
});

Android disable button when checkbox not checked

I have a multiple checkbox and a button. What should I do to disable the button if none of the checkbox is check and enable the button if it's checked?
Try with this,
Button mButton=(Button)findViewById( R.id.button01);
CheckBox mCheckBox= ( CheckBox ) findViewById( R.id.checkbox01);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
mButton.setEnabled(true);
}else{
mButton.setEnabled(false);
}
}
});
When you enter in that view make the button disabled (in your XML), and whenever user hit any of the check-boxes manage one global variable e.g if the global count is > 1 then make the button enable in that activity.
Manage the global variable in a way that if user is turning on the check box then increment it and if he is turning off the checkbox decrease the counter.
I hope you got the concept.
Basically it is all about managing the count how many checkboxes are turned on; if more then one is turned on make the button enabled else make it disabled.
Instead of enabling and disabling the button you can use setVisibility() method for button.
In the following manner.
Button btn =(Button)findViewById( R.id.mybutton);
CheckBox checkBox= ( CheckBox ) findViewById( R.id.checkbox01);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
btn.setVisibility(VISIBLE);
}
else{
btn.setVisibility(GONE);
}
}
});
By using this method you can set the visibility of your view.Your button will be visible only if checkBox is cheaked otherwise your button will not be visible.Let me know it works or not for you.
Use this:
myButton.setEnabled(false);
See this question for more details.
you can disable button by using below code.
mBtn.setEnabled(false);
and can enable it later by using below code
mBtn.setEnabled(true);
mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mButton.setEnabled(isChecked);
}
});
as simple as that :)
try this
termsAndConditionsCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (termsAndConditionsCheckBox.isChecked() && privacyPolicyCheckBox.isChecked()){
agreebutton.setEnabled(true);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
}
else {
agreebutton.setEnabled(false);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
}
});
privacyPolicyCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (termsAndConditionsCheckBox.isChecked() && privacyPolicyCheckBox.isChecked()){
agreebutton.setEnabled(true);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
}
else {
agreebutton.setEnabled(false);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
}
});

how to change the checked state of Toggle Button and CheckBox

I have to two Class in my App
Setting(for saving the settings)
main Class
I defined the Toggle button and check box in Setting class for On and Off.
now when I call the setting class and from the main class and the change the checked state of Toggle Button and Check Box and the return to main class and if I again call the Setting class the checked state of the Toggle button and check box is not change it come back to it original state please help
My CODE:
yes.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox) v).isChecked())
sound.toggle();
}
});
sound.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
Log.v("CheckBoxActivity", (isChecked ? "checked" : "not checked"));
}
});
}
public void doClick(View view) {
Log.v("CheckBoxActivity", ((CheckBox) view).isChecked() ? "checked" : "not checked");
}
This probably will be the problem as each time your activity starts,it would take default selection state for the components.
To achieve you goal,you need to store what state you need for a toggle button and checkbox.And then you will have to set the according state of the same each time you load activity,that mean in onCreate() just before you start defining onClick() on those components.
You can use SharedPreference object to store state at a time and use the same in onCreate() of you activity to set stored states of toggle button and checkboxes.

Categories

Resources