RadioButton Issue get checked when sending? - android

Having two RadioButtons and button send.
When I click on the send button the 2 RadioButtons are checked.
How to avoid this?
enter image description here

You need to group the radio buttons so inside the xml add the radio buttons inside this tag:
<RadioGroup> .....</RadioGroup>
then in your activity do this:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup); //declare the radiogroup
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.radiobtn1) {
//code here
} else if(checkedId == R.id.radiobtn2) {
//code here
} else {
//code here
}
}
});

If you want only one RadioButton to be checked at once, keep them inside a RadioGroup.

Just Put your RadioButtons inside RadioGroup

Related

Prevent form submission if radio button isn't checked

Working on an education android app. One provides answers through EditText input, radio buttons and CheckBox.
I want a check for the RadioGroup that when no RadioButton is clicked it will prompt the student and prevent submission. I succeeded in EditText but unsucessful for RadioButton.
public void checkEmptyButtons(View view){
RadioGroup rg = (RadioGroup) findViewById(R.id.myRg);
if (rg.getCheckedRadioButton()==-1{
// this is where I have a problem
//I don't even know how to search on the //dev.anderoid site
}
}
That's where am stucked. Thanks in advance
.
you need radioGroup checked Change Listener:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//here you will get callback everytime radio button is checked along with id. You can have boolean checked here to know if any of your radio button is checked
}
});
Use getCheckedRadioButtonId() in stead of getCheckedRadioButton().
if (rg.getCheckedRadioButtonId() == -1) {
//no radio buttons are checked
}
else {
//any one of the radio buttons is checked
}

save edit text value on changing radio button

I have two radio buttons in a radio group and three text edit fields in one layout.
Now when I switch radio button I would able to save data of edit text for pervious selected radio button and vice versa
Please help me.
You can do using radio group.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
if(checkedId == R.id.radiobutton1)
{
//get data from edittext and save then clear edit text
}else
{
//get data from edittext and save then clear edit text
}
}
});

RadioButton Clear Check Action

I have a simple program that has 3 radio buttons in a group. When I click on any of the radio buttons I have a Toast appear to let the user know which button they have selected.
My problem I am facing is I have a onClick event for a button that clearCheck the radio group. When that happens, the Toast message appears again with the previous selected information from the radio button.
How do I prevent the toast from happening when I clearCheck the radio group?
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Radio Group and attach click handler
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.clearCheck();
// Attach CheckedChangeListener to Radio Group
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
if(null!=rb && checkedId > -1){
Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
public void onClear(View v){
// Clears the radio buttons when the clear button is pressed
radioGroup.clearCheck();
}
That is because clearing the check is changing the checked status of the radio group. You need to set a flag to let the listening know it should ignore that change.
Just a simple boolean declared in the class, flagged in onClear and checked in onCheckChanged (and reset the flag in onCheckChanged also).

How to display/ hide some portion of the form on radio button selection in android?

I have a requirement where the form has to ask "do you have valid license:" and in radio choice 1)yes 2)no . on selection of yes the fields to fill licenseno. , licensee_name and take license photo should appear and on selection of no these thing should be hidden and it should continue with filling of the rest of the form fields .
For your concern try like this
Get Radio button like this :
RadioButton rbone = (RadioButton) findViewById(R.id.rone);
RadioButton rbtwo = (RadioButton) findViewById(R.id.rtwo);
Get Radio Group like this
radioGroup = (RadioGroup) dialog.findViewById(R.id.rg);
And then fallow like this
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (rbone.isChecked() == true) {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
}else{
view2.setVisibility(View.VISIBLE);
view1.setVisibility(View.GONE);
}
}
});
Try to write the child View Like licenseno and name and photo inside one layout and give one id to that layout the programmatically you can hide visibility of that block.
Like :
LinearLayout ly=(LinearLayout)findViewById(R.id.LayoutID);
ly.setVisibility(View.INVISIBLE);
OR
ly.setVisibility(View.GONE);
According to Your Radio Button Click Event you can achieve this
You can set view.VISIBLE and view.INVISIBLE for controls to visible and invisible
Try this.. If your radio buttons belongs to the same group
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radioYes){
view.setVisibility(View.VISIBLE);
}else if(checkedId == R.id.radioNo){
// Depends of your requirement
view.setVisibility(View.INVISIBLE);
// or
view.setVisibility(View.GONE);
}
}
});

How to set OnClickListener on a RadioButton in Android?

I have two RadioButtons inside a RadioGroup. I want to set OnClickListener on those RadioButtons. Depending on which RadioButton is clicked, I want to change the text of an EditText. How can I achieve this?
I'd think a better way is to use RadioGroup and set the listener on this to change and update the View accordingly (saves you having 2 or 3 or 4 etc listeners).
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
});
Hope this will help you...
RadioButton rb = (RadioButton) findViewById(R.id.yourFirstRadioButton);
rb.setOnClickListener(first_radio_listener);
and
OnClickListener first_radio_listener = new OnClickListener (){
public void onClick(View v) {
//Your Implementaions...
}
};
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb=(RadioButton)findViewById(checkedId);
textViewChoice.setText("You Selected " + rb.getText());
//Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
}
});
The question was about Detecting which radio button is clicked, this is how you can get which button is clicked
final RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View radioButton = radio.findViewById(checkedId);
int index = radio.indexOfChild(radioButton);
// Add logic here
switch (index) {
case 0: // first button
Toast.makeText(getApplicationContext(), "Selected button number " + index, 500).show();
break;
case 1: // secondbutton
Toast.makeText(getApplicationContext(), "Selected button number " + index, 500).show();
break;
}
}
});
For Kotlin Here is added the lambda expression and Optimized the Code.
radioGroup.setOnCheckedChangeListener { radioGroup, optionId ->
run {
when (optionId) {
R.id.radioButton1 -> {
// do something when radio button 1 is selected
}
R.id.radioButton2 -> {
// do something when radio button 2 is selected
}
// add more cases here to handle other buttons in the your RadioGroup
}
}
}
Hope this will help you. Thanks!
You could also add listener from XML layout: android:onClick="onRadioButtonClicked" in your <RadioButton/> tag.
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pirates"
android:onClick="onRadioButtonClicked"/>
See Android developer SDK- Radio Buttons for details.
Since this question isn't specific to Java, I would like to add how you can do it in Kotlin:
radio_group_id.setOnCheckedChangeListener({ radioGroup, optionId -> {
when (optionId) {
R.id.radio_button_1 -> {
// do something when radio button 1 is selected
}
// add more cases here to handle other buttons in the RadioGroup
}
}
})
Here radio_group_id is the assigned android:id of the concerned RadioGroup. To use it this way you would need to import kotlinx.android.synthetic.main.your_layout_name.* in your activity's Kotlin file. Also note that in case the radioGroup lambda parameter is unused, it can be replaced with _ (an underscore) since Kotlin 1.1.
Just in case someone else was struggeling with the accepted answer:
There are different OnCheckedChangeListener-Interfaces. I added to first one to see if a CheckBox was changed.
import android.widget.CompoundButton.OnCheckedChangeListener;
vs
import android.widget.RadioGroup.OnCheckedChangeListener;
When adding the snippet from Ricky I had errors:
The method setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener) in the type RadioGroup is not applicable for the arguments (new CompoundButton.OnCheckedChangeListener(){})
Can be fixed with answer from Ali :
new RadioGroup.OnCheckedChangeListener()
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()
});

Categories

Resources