Android Studio: Using CheckBox to set EditText enabled - android

In android studio, I am attempting to use a CheckBox to control whether a numerical EditText is enabled or disabled. The relevant code when the checkbox is clicked:
boolean checked = ((CheckBox) view).isChecked();
EditText yzEdit = (EditText) findViewById(R.id.yzEdit);
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.yzCheck:
if (checked){
yzEdit.setBackgroundColor(Color.parseColor("#FFFFFF"));
yzEdit.setTextIsSelectable(true);
yzEdit.setFocusable(true);
yzEdit.setFocusableInTouchMode(true);
yzEdit.setCursorVisible(true);
yzEdit.setEnabled(true);
}
else{
yzEdit.setBackgroundColor(Color.parseColor("#CCCCCC"));
yzEdit.setTextIsSelectable(false);
yzEdit.setFocusable(false);
yzEdit.setFocusableInTouchMode(false);
yzEdit.setCursorVisible(false);
yzEdit.setEnabled(false);
}
break;
}
Everything almost works. However, when I check the box, enabling yzEdit, then click on yzEdit, no user numerical keyboard pops up directly. So the user cannot enter any numbers into the newly enabled EditText (except in a roundout way of focusing on it through "Next" from a previous EditText.
Which property am I looking for that controls this behavior?

You need to use a listener on your checkbox, if you want to detect if an object has changed his state.
For your case, it's setOnCheckedChangeListener() to use on your checkbox.
You can take a look here : http://developer.android.com/reference/android/widget/CompoundButton.html
and :
Android: checkbox listener

Related

Android radio button click already checked button

I've got an activity that lets users choose a "type" of a thing using radio buttons, but when modifying that thing I want to pre-check the radio button for the type the thing already is, and allow the user to click that checked radio button again if they don't want to change the type before moving to the next activity. I want to pre-check because users might not remember which type the thing was when they go to modify it.
The problem is that the listener only triggers when the option changes, not when the user clicks the option that's already checked. That makes sense, seeing as the listener is called "OnCheckedChangeListener," but when I try adding an OnClick listener to catch the click of a checked button, nothing happens. It simply doesn't trigger, and I don't see any other listeners on the RadioGroup object that seem like they'd do what I want.
Here's the checked change listener, which works fine:
rgThingType.setOnCheckedChangeListener { radioGroup, i ->
val checkedRadioButton = radioGroup?.findViewById(i) as? RadioButton
when(checkedRadioButton?.id) {
// ...
}
goToActivity()
}
And here's the on click listener, which never triggers no matter what:
rgThingType.setOnClickListener {
goToActivity()
}
Now, I can add an OnClick to each radio button to accomplish this, ie:
rbThingType1.setOnClickListener {
thingType = 1
if(/* extra logic to prevent double activity start */) {
goToActivity()
}
}
but that is awkward and seems like an anti-pattern, given I already have the checked change listener, and in fact it screws things up as it causes the next activity to be triggered twice since both listeners get called, forcing me to keep/check extra state to avoid. Is there a proper way to do this?
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnClickListener(v -> {
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = findViewById(selectedId);
String slectedValue=radioButton.getText()
});

How to do something when EditText is activated

I'm looking for an option to do something when EditText is touched, but I want it to be writeable, cause I found few solutions for it, but then the EditText turned to be unwriteable and I could only click it.
EditText ed == //to its reference
ed.setFocusableInTouchMode(true);
ed.setOnTouchListener(/*add a listener */);
//also remember to add ed.requestFocus(); in your listener
i guess the above should work right?

Uncheck radio button if already checked?

I have a RadioGroup with 6 radio buttons inside it.
Is there a way to uncheck the radio buttons if the user click on an already checked radio button?
I mean is there any way to clear check the whole radio group or do not store the value if user clicks on an already checked radio button?
i know there exist ClearCheck() and setChecked(false), but i don't know how and where to use them because SetOncheckChanged listener only runs when the check state changes. It does not run when you click on an already checked radio button. I think I should use SetOnClickListener for radiogroup but i don't know how to find which radiobutton is checked?
Use ::
// if we already have a checked radiobutton and want to remember it
if(mCurrentlyCheckedRB == null)
mCurrentlyCheckedRB = (RadioButton) v;
mCurrentlyCheckedRB.setChecked(true);
}
//clicked the current or desire one to check it that allready stored
if(mCurrentlyCheckedRB == v)
return;
// else, uncheck the currently checked RadioButton, check the new radio button
mCurrentlyCheckedRB.setChecked(false);
((RadioButton)v).setChecked(true);
mCurrentlyCheckedRB = (RadioButton)v;

Is there anyway to get my edittext highlighted?

I disabled softkeypad in my application because I have my own custom keypad. But the problem is when I clicked on the edittexts in order to enter data through my custom keypad ,that edittexts are not getting highlighted at all. Even cursor is not visible inside that respective clicked edittext. Why there are always side effects while disabling soft keypad? I tried all the suggestions that are there in the sources including stackoverflow, but nothing worked. Can I get the perfect solution to get the edittext highlighted when clicked?
you need to call textView.requestFocus() when clicked this way your editText can be highlight
dont forget also to add in your XML File
this attribute android:focusableInTouchMode="true" to your EditText
I don't know why those side effects occur, but in this post there is a workaround how disable the keyboard and still have the cursor. That worked for me except that I also needed to request focus, so it's:
//disable keypad
et.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int inType = et.getInputType(); // backup the input type
et.setInputType(InputType.TYPE_NULL); // disable soft input
et.onTouchEvent(event); // call native handler
et.setInputType(inType); // restore input type
et.requestFocus(); // request focus
return true; // consume touch even
}
});

Android: How to enable my button back if EditText is not empty?

I have 2 EditTexts; 01 and 02. My button will be disabled once the activity is started and when these two EditText contain text, the button has to be enabled again. However my button is always disabled and can't enable it using button.setEnabled(true);.
Can anyone help me with this?
summit.setEnabled(false);
buttonEnable();
public void buttonEnable(){
if (feedback.length()>0 && email.length()>0){
summit.setEnabled(true);
}else{
summit.setEnabled(false);
}
}
You're correct about needing a TextWatcher. The afterTextChanged(Editable) method is the one you're interested in for something like this. Call your buttonEnable() method from it, and add the TextWatcher to any applicable text fields. (Looks like feedback and email from your sample.)
One easy way can also be to set onKeyListener to your editText(), then if there is something in editText(), set button enable if nothing disable it.

Categories

Resources