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
}
}
});
Related
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
}
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
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).
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);
}
}
});
I have an application form with one radio group that consists of two radio buttons (self delivery and company delivery). While inserting the details everything is working fine and I can get the text of radio button selected by the code below:
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
if (radioButton.isChecked()) {
OPTIONS=radioButton.getText().toString();
if (OPTIONS.equals("SelfDelivery")) {
deliveryAddress.setVisibility(View.GONE);
deliveryEdit.setVisibility(View.GONE);
} else {
deliveryAddress.setVisibility(View.VISIBLE);
deliveryEdit.setVisibility(View.VISIBLE);
}
}
}
});
When I press the edit button and go to edit page, I want the radio button to also be selected according to the user.
For example: If the user is selecting the company delivery (in the edit page the company delivery should be selected in the radio group).
I do not know how to get the selected id and make the radio group to be selected in the edit page.
// try this
// on edit try this way
RadioButton radioButtonSelf;
RadioButton radioButtonCompany;
radioButtonSelf = (RadioButton) findViewById(R.id.selfradiobuttonid);
radioButtonCompany = (RadioButton) findViewById(R.id.radioButtonCompany);
if(OPTIONS.equals("SelfDelivery")){
radioButtonSelf.setChecked(true);
radioButtonCompany.setChecked(false);
}else{
radioButtonCompany.setChecked(true);
radioButtonSelf.setChecked(false);
}