Android disable button when checkbox not checked - android

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));
}
}
});

Related

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);
}
}
});

Enable and Disble Listview with Some Condition in Android

How to enable all the items in listview when check box is selected and disabled when check box is unchecked.I have used
if(checkbox.isChecked)
{
listview.setEnabled(false)
listview.setClickable(false)
}
else
{
listview.setEnabled(true)
listview.setClickable(true)
}
But it is not working.Any help would be greatly appreciated.
Thanks in Advance:)
if(yourcheckbox.isChecked()){
yourlistview.setClickable(true);}
else{
yourlistview.setClickable(false);}
You can run this as a background process, in a separate thread so it keeps getting checked, whether the checkbox is checked or not.
you can use OncheckedChangeListener method of Checkbox class
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked)
{
listview.setEnabled(false)
listview.setClickable(false)
}
else
{
listview.setEnabled(true)
listview.setClickable(true)
}
}
});

Click twice to check Radio Button?

I am trying to build a test application and I am using Radio buttons for the choices A,B,C,D. I am trying to calculate the result -> for example when press A increment 1, when press B increment 2 and so on. I was using Radio Group at first but I understood that if the user try to change the answer to his question from A to B the incremented result will be 3, not 2 as expected. So I switched to Radio Buttons and I try the following code:
rb1.setOnCheckedChangeListener(new
android.widget.CompoundButton.OnCheckedChangeListener (){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
i++;
if(rb2.isChecked())
{
i--;
rb2.setChecked(false);
}
}
});
rb2.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener (){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
i++;
if(rb1.isChecked())
{
i--;
rb2.setChecked(false);
}
}
});
And now the second Radio Button have to be clicked twice in order to be checked. Is this a bug and could it be fixed. I need the Buttons to change state after the first click. All ideas and advice will be welcomed. Thank you.
You can change your code to use a switch statement (as suggested in one of the comments) but if you want to continue with what you have, and to answer your question. In your code for rb2, you are saying that if rb1 is checked set rb2 to false. So, if you click rb1 (and it is now checked/pushed) and you click rb2 the first time you uncheck it, the 2nd time it stays checked.
So sticking with your coding style and procedure:
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
i++;
if(rb1.isChecked())
{
i--;
rb2.setChecked(false);
}
}
should be:
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
i++;
if(isChecked && rb1.isChecked())
{
i--;
rb1.setChecked(false);
}
rb2.setChecked(isChecked);
}
Make a similar change to your rb1 method.

How to receive a event on android checkbox check change?

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
}
}
});

Categories

Resources