Android radio button click already checked button - android

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

Related

Best practices to control buttons like update save and delete for sqlite in android

I want to have some edittexts and listview for data and buttons like add,update and delete. What is the best practice for controlling the buttons to be enabled or not, using a public method like..
public void CheckButtonStates(int Condition, List<ImageButton> tmlist)
{
switch (Condition)
{
case 1://Start
tmlist.get(0).setEnabled(true);//save button
tmlist.get(1).setEnabled(false);//update button
tmlist.get(2).setEnabled(false);//delete button
tmlist.get(0).setAlpha(1f);
tmlist.get(1).setAlpha(0.4f);
tmlist.get(2).setAlpha(0.4f);
break;
case 2://Add new
tmlist.get(0).setEnabled(true);//save button
tmlist.get(1).setEnabled(false);//update button
tmlist.get(2).setEnabled(true);//delete button
tmlist.get(0).setAlpha(1f);
tmlist.get(1).setAlpha(0.4f);
tmlist.get(2).setAlpha(1f);
break;
}
or there is another way more efficient?
I think the best way to control this situation is to control the visibility or the enabled of the buttons from buttons click.
When the app starts you have your edittexts empty and save button enable in order to save new data in sqlite database.
When your user click something from the listview take the data from selected list item fill the edittexts and set disable the save button and enable update and delete button.
When user click delete or update button save or delete data in sqlite and set your edittext fields empty and enable save button and disable update and delete buttons.
Example:
btnUpdate.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//Save data to sqlite
//Set your edittexts empty
btnUpdate.setVisibility(View.GONE);
btnDelete.setVisibility(View.GONE);
btnSave.setVisibility(View.VISIBLE);
}
});
Set a listener for each button and decide which functions should not be able due to the last function,it all depends on your needs. like this:
UpdateButton.setonclickListener(new.....)
{
//onclick:{
updateButton.setEnabled(true);
}
}
Hope you understand what I tried to do,if not tell me and I will update a full code.

Android Studio: Using CheckBox to set EditText enabled

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

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;

Android UI shows RadioButton as selected, although it is not

When my UI is recreated on orientation change, I use super.onCreate(savedInstanceState) and getLastNonConfigurationInstance() to access custom data I stored to fill the dynamic parts of my layout.
I have a RadioGroup which has two RadioButtons and is already defined in the XML file. The XML automatically makes the first one selected.
When an orientation change happens and the SECOND RadioButton is selected, everything seems to work fine; the second RadioButton is still selected in the UI.
But RadioGroup.getCheckedRadioButtonId() says the first RadioButton is selected. And I actually WANT the first one to be selected. But nothing changes when I call rb1.setChecked(true) - second one still shown as selected, and still the RadioGroup tells me the first one is selected (now it would make sense, but it's not shown).
This is REALLY strange behavior, does anyone have tips?
(edit)
Parts of my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.itemselected);
(...)
radioGroupServingType = (RadioGroup) findViewById(R.id.radioGroupServingType);
(...)
RadioButton radioOwnServing = (RadioButton) findViewById(R.id.radioOwnServing);
RadioButton radioUseServing = (RadioButton) findViewById(R.id.radioUseServing);
radioOwnServing.setOnClickListener(this);
radioUseServing.setOnClickListener(this);
//FIXME WTF
Log.d("", radioOwnServing.isChecked()+"/"+radioUseServing.isChecked()+" own/use checked");
radioOwnServing.setChecked(true);
Log.d("", radioOwnServing.isChecked()+"/"+radioUseServing.isChecked()+" own/use checked");
If I select radioUseServing in the UI and change the orientation, the log says true/false own/use checked both times - although radioUseServing is shown as selected in the UI.
By the way, logcat also outputs
W/asset(4040): deep redirect failure from 0x0103003e => 0x02060007, defStyleAttr=0x0101007e, defStyleRes=0x0103001a, style=0x00000000
when changing the orientation, sometimes multiple times. I haven't found anything with google on what that means.
I already spoke with you on IRC, but I believe that if you get the view, and post a runnable to it that will call .setChecked(), this will in effect cause the setChecked call to occur at the proper time, and thus avoiding having called setChecked before the view hierarchy was ready for it.
Something like this:
final View myRadioButton = findViewById(R.id.myradiobutton);
myRadioButton.post(
new Runnable() {
#Override
public void run() { myRadioButton.setChecked(true); }
}
);

Android RadioButton not able to unset using setChecked(false) method

If I set a radio button to be selected on the first time, it works fine. But if I unselect it by calling
((RadioButton) findViewById(R.id.ID)).setChecked(false);
then, later even if I try to make it selected by calling setChecked(true) will not work unless the user select it from the screen.
Have any one come across this? or is it only me?
if(Val != null){
if( ((RadioButton) findViewById(R.id.ID1)).getText().toString().trim().equals(Val))
((RadioButton) findViewById(R.id.ID1)).setChecked(true);
else if(((RadioButton) findViewById(R.id.ID2)).getText().toString().trim().equals(Val))
((RadioButton) findViewById(R.id.ID2)).setChecked(true);
}
else {
((RadioButton) findViewById(R.id.ID1)).setChecked(false);
((RadioButton) findViewById(R.id.ID2)).setChecked(false);
}
If the else part is executed atleast once then everything gets mess up.
When I step thro my debugger, I can see the execution goes in the correct path and setting it to true. It is getting executed only once, I checked that. And I am not resetting it back to false in any other part of the code.
I found the solution.
It is not possible to uncheck a particular radio button. You can only set the other item to true.
So to clear all the checked items, you should call the clearcheck() method on the RadioGroup.
So my else part is
else {
((RadioGroup) findViewById(R.id.ID0)).clearCheck();
}
Take one invisible radio button and check it. All other radio buttons of group will be unchecked automatically..

Categories

Resources