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
}
Related
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 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
}
}
});
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 am making an application that has list and Radio Buttons. My Layout contains of the following : a list , A button .
The List contains of Radio buttons. Two radio Buttons. After checking the buttons. I want to make a loop that reads the values of how many are in Button one and how many in Button Two.
This is how I have done so far.
ListViewStudentEditAbsence country ;
for(int i=0;i<listViewStudentNames.size();i++) {
country = listViewStudentNames.get(i);
if (country.getAbsence()==1) {
Absence[i] = "1";
} else {
Absence[i] ="0";
}
}
But It doesn't seem to work.
In your CustomAdapter getView() method
int value=0; //globally
RadioGroup radioGroup = (RadioGroup) convertView.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.rbutton1) {
value=10; //a's value
} else if (checkedId == R.id.rbutton2) {
value=20; //b's value
}
}
});
https://stackoverflow.com/questions/11194515/android-get-value-of-the-selected-radio-button
Android getting value from selected radiobutton
Hope it works. This two links give you proper knowledge about fetching value.
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);
}
}
});