Check box only works once? - android

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

Related

Android Studio programmatically check a Checkbox and call its OnCheckedChange

Can I programmatically check a checkbox and make it call whatever it is coded to do once checked or unchecked?
For example, if I have a checkbox like this
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(getContext(), "You checked the checkbox!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getContext(), "You unchecked the checkbox!", Toast.LENGTH_SHORT).show();
}
}
});
And then would've called
checkBox.setChecked(true);
my checkbox would appear checked but it wouldnt make the toast.
You can do any of the following
1. Create the listener object separate and call it manually when you call setChecked
2. Extract the method for implementation of onCheckedChanged and call it manually on your change.
As the name suggest setOnCheckedChangeListener, it only calls your callback if the checkbox value actually changes.
So if it's already checked (true), and then you call checkbox.setChecked(true), the value hasn't changed, so your callback won't be called.
Try to do checkbox.setChecked(false), and it should be working correctly.
That's my best guess, without seeing the rest of your code / xml.
Android widgets' click or touch events are not simulated. you can change states of of widgets like disabling ,enabling ,checked or unchecked programmatically but todo any task when state changes , you have to change their states manually by touching on that widget.
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(getContext(), "You checked the checkbox!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getContext(), "You unchecked the checkbox!", Toast.LENGTH_SHORT).show();
}
}
});
Just Cheked it. it will work for me.
if you add checkboxes in LinearLayout (let's call it checkLayout) like this :
val widget = AppCompatCheckBox(checkLayout.context)
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
widget.tag = "something you can specify later"
checkLayout.addView(widget)
the code for get the selected checkbox is like this:
checkLayout.children.forEach { cb ->
if(cb is AppCompatCheckBox && cb.isChecked){
// here you have your checkbox and by tag maybe you can do whatever you want
}
}
and for dynamic added RadioButton you can add them in RadioGroup (let's call it radioLayout) and get the selected like :
val rb: AppCompatRadioButton? = radioLayout.findViewById(radioLayout.checkedRadioButtonId)
if(rb.tag == "something"){
//your code here
}

Go to another activity when all fields are not empty

I have 1 activity and 3 fragments in this activity.
In 3 fragments I have booleans returning true when checkbox is checked or edittext is not empty..
I want to unable button when editTexts are not empty and my checkbox is checked..
When I added something like sexMan.isChecked() to method EditTextCompleted()
my app crushing....
So how can I connects this conditions in one place?
Checkbox Listener
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
mSendInfoFromBmrFragment.sexChecked(true);
switch (buttonView.getId()){
case R.id.sexMan:
mSendInfoFromBmrFragment.sex(BmrCalculation.SEX_MAN);
sexMan.setChecked(true);
sexWoman.setChecked(false);
break;
case R.id.sexWoman:
sexMan.setChecked(false);
sexWoman.setChecked(true);
mSendInfoFromBmrFragment.sex(BmrCalculation.SEX_WOMAN);
break;
Edit Text listener
#Override
public void afterTextChanged(Editable s) {
if ( s == weightEditText.getEditableText()){
mSendInfoFromBmrFragment.getweight(Integer.parseInt(s.toString()));
mWeight = Integer.parseInt(s.toString());
} else if ( s == heightEditText.getEditableText()) {
mSendInfoFromBmrFragment.getheight(Integer.parseInt(s.toString()));
mHeight = Integer.parseInt(s.toString());
} else if ( s == ageEditText.getEditableText()){
mSendInfoFromBmrFragment.getage(Integer.parseInt(s.toString()));
mAge = Integer.parseInt(s.toString());
}
if ( (!(weightEditText.getEditableText().toString().equals("")) && !(heightEditText.getEditableText().toString().equals("")) && !(ageEditText.getEditableText().toString().equals("")))) {
mSendInfoFromBmrFragment.editTextComplete(true);
} else {
mSendInfoFromBmrFragment.editTextComplete(false);
}
}
Interface
public interface sendInfoFromBmrFragment {
public void getheight(int height);
public void getweight(int weight);
public void getage(int age);
public void editTextComplete(boolean editTextComplete);
public void sex(String sex);
}
For the Radio Buttons you can declare an array if you know their number and they are static, do this as follow:
public Boolean AllRadiosAreChecked;
private Boolean Checked[] =
{
false, // sexMan is checked ?
false // sexWoman is checked ?
};
Then, in the same activity ovverride the OnCheckedChanged
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
switch (buttonView.getId()){
case R.id.sexMan:
Checked[0]=true;
break;
case R.id.sexWoman:
Checked[1]=true;
break;
Then you check anywhere you want if all Radio Buttons are checked:
AllRadiosAreChecked=true;
for (int i = 0; i < 2; i++)
{
if (!Checked[i])
{
AllRadiosAreChecked= false;
}
}
if(!AllRadiosAreChecked){
// NOT all radio buttons are checked
}
else{
// All radio buttons are checked enter code here
}
This conditions in the array must be declared to group radios.
Good Luck
You can add callbacks to your checkboxes and then they all are true you need call method in your parent activity (for example send in activity fragment ID and boolean value). To call method from activity you need to use the next code
((ParentActivity)getActivity()).yourMethod();
And then activity received all booleans from all fragment you need to change Button visibility.
If you have edittext you can add textchangelistener to it, if you have checkbox you can add listener of it, in listener you can check validation. Based on validation you can enable or disable the button for next activity. You can have that button to be static to access from other class.
In every listener you can call one public static method in which you will check all validation every time, if it satisfies then method will return true and you can enable button.

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.

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 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