I got the position of a radio in a radio group by using indexOfChild. How can I use this index to check the radio that has this particular index?
Try this
radioGroup.check(((RadioButton)radioGroup.getChildAt(index)).getId())
for (i in listCategory.indices){
if ((categoryRadioGroup[i] as RadioButton).isChecked) {
Toast.makeText(myContext, "$i", Toast.LENGTH_SHORT).show()
}
}
Related
I have a chip group and inside that I am adding Choice Chips programmatically and I have a button called Select All for selecting all if some of them selected and Same Button for Deselecting All chips in a single click.
Now Please guide me some proper way or It would be great if it can be done using chip group instead of ArrayList of chip
Thanks in advance :)
For deselecting you can use clearCheck and for selection you've to go through for loop
I have created a general Extension function for the above solution in kotlin
I think that this is the proper solution
fun ChipGroup.applyCheckedOnAll(isChecked: Boolean){
if (isChecked){
for (index in 0 until this.childCount) {
val chip:Chip = this.getChildAt(index) as Chip
chip.isChecked = true
}
}else {
this.clearCheck()
}
}
ChipGroup chipGroup = view.findViewById(R.id.chipGroup );
for (int i = 0; i < chipGroup.getChildCount(); i++) {
Chip chip = (Chip) chipGroup.getChildAt(i);
chip.setChecked(false);
}
Clears the selection. When the selection is cleared, no chip in this group is selected
chipgroup.clearCheck()
How could I change the state of a radio button in a radio group?
I get the value Male or Female from Firebase(stored before out of the radio group) and fill again the Layout. Now I have to convert the Male information to the radio button of the radio group
var rg = radioGruppe
rg!!.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener
{
group, checkedId ->
Log.i("test", "The ID is: ${group.checkedRadioButtonId}")
})
The ID is: 2131230874
But what have I to do now?
found it by myself:
rbMale = mView?.findViewById((R.id.rb_male))
rbFemale = mView?.findViewById((R.id.rb_female))
if(Gender == "Woman") {
radioGruppe!!.check(rbFemale!!.id)
} else {
radioGruppe!!.check(rbMale!!.id)
}
I am trying to work on radio groups. I have an ok button which when clicked selects one item from four inflated radio button groups. My problem is that, when the user does not select an option from one of the groups, I have a null pointer exception.
Here is my selection code
private void getSelectedValuesAdults()
{
rl=(RelativeLayout)findViewById(R.id.activity_selections);
int id1 = ((RadioGroup)findViewById(R.id.KSradioGrp)).getCheckedRadioButtonId();
int id2 = ((RadioGroup)findViewById(R.id.KTradioGrp)).getCheckedRadioButtonId();
int id3 = ((RadioGroup)findViewById(R.id.KDradioGrp)).getCheckedRadioButtonId();
int id4 = ((RadioGroup)findViewById(R.id.KSoradioGrp)).getCheckedRadioButtonId();
stringChoice[0]= ((RadioButton)findViewById(id1)).getText().toString().contains(generalKid[0].getName())==true?generalKid[0].getName():generalKid[1].getName();
stringChoice[1]= ((RadioButton)findViewById(id2)).getText().toString().contains(generalKid[2].getName())==true?generalKid[2].getName():generalKid[3].getName();
stringChoice[2]= ((RadioButton)findViewById(id3)).getText().toString().contains(generalKid[4].getName())==true?generalKid[4].getName():generalKid[5].getName();
stringChoice[3]= ((RadioButton)findViewById(id4)).getText().toString().contains(generalKid[6].getName())==true?generalKid[6].getName():generalKid[7].getName();
genCost[0]= ((RadioButton)findViewById(id1)).getText().toString().contains(generalKid[0].getName())==true?generalKid[0].getCost()+5:generalKid[1].getCost()+5;
genCost[0]= ((RadioButton)findViewById(id2)).getText().toString().contains(generalKid[2].getName())==true?generalKid[2].getCost()+5:generalKid[3].getCost()+5;
genCost[0]= ((RadioButton)findViewById(id3)).getText().toString().contains(generalKid[4].getName())==true?generalKid[4].getCost()+5:generalKid[5].getCost()+5;
genCost[0]= ((RadioButton)findViewById(id4)).getText().toString().contains(generalKid[6].getName())==true?generalKid[6].getCost()+5:generalKid[7].getCost()+5;
}
The Ok button jumps to the method above. I have used if(id1==-1||id2==-1||id3==-1||id4==-1) callDialogMessage() just before the assignment to stringChoice[0] where callDialogMessage() is a message that lets you know you have not selected from a radio group but my app still crashes. What can I do? Any help will be highly appreciated
On click of OK button get selected radio button Id using radio group .You can refer the below code.
int selectedId =radioButtonGroup.getCheckedRadioButtonId();;
// find the radiobutton by returned id
RadioButton radioButton = FindViewById<RadioButton>(selectedId);
if(radioButton !=null)
{
//do things here
}
Two buttons getting selected in the radio group.
I do not know where I am getting wrong. Please help me out.
final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);
rg.clearCheck();
rg.setId(Integer.valueOf(entry1.getKey()));
Log.v("rg getid", "rg"+rg.getId());
for(int i =0;i<values.size();i++){
// Create Button
final RadioButton btn = new RadioButton(Survay_MainActivity.this);
btn.setId(i);
btn.setTextColor(Color.parseColor("#000000"));
btn.setBackgroundColor(Color.TRANSPARENT);
btn.setGravity(Gravity.LEFT);
btn.setText(values.get(i));
rg.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
JSONObject quesAns = new JSONObject();
String ans=btn.getText().toString().trim();
try {
quesAns.put(String.valueOf(rg.getId()), ans);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jsonarray.put(quesAns);
Log.v("jsonarray", "jsonarray"+jsonarray);
}
});
}
views.addView(rg);
1) I am creating the RadioGroup out of the loop.
2) Adding radio button to the RadioGroup in the for loop
3) When the loop finishes the RadioGroup is added to the linerlayout.
You just need to change different ids for different radio button.
There may be some id clash in gen file.
Radio button1 : android:id="#+id/one
Radio button2 : android:id="#+id/two"
Radio button3 : android:id="#+id/three"
Hope this would help.
There are some things misplaced these changes you have to make out.
1.Change your OnClickListener by OnCheckChangeListener
2.Clear check your radioGroup after adding all radioButtons and before adding it in LinearLayout.
Your problem comes from the fact that you are setting an id for your items manually
rg.setId(Integer.valueOf(entry1.getKey()));
btn.setId(i);
Android works with id's generated automatically that are different. Setting the id's manually it might happen that you give the same ID to the radio group and the radio button.
I used your code and set the group to ID 3 and radios from 0 to 4.
Needless to say that after I click the button with id 3 it always stays on because the group has the same id.
So, try to remove the id setting part, or, if you insist make sure you always have distinct id's.
I faced the same problem. The cause is the RadioGroup's id is same as one of RadioButtons
the problem is this line. you didn't send where is this code belonged to. when you get out of this scope and come back you will create new RadioGroup. You should have one and only one RadioGroup so the RadioButtons have same RadioGroup and by default one of them will be selected. To this, you can define RadioGroup in an XML and use this to define variable for it RadioGroup rg = (RadioGroup) findViewById(R.id.YOURNAME); or you can define your RadioGroup as Instance variable(Global).
final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);
In my project, I get the value of the gender from the database as 0 and 1.
0 for male and 1 for female.
Based on this values, I need to check the corresponding RadioButton in a RadioGroup.
If 0 is pressed, radio0 will be checked or radio1 will be checked.
I don't know how to check the radio button based on this string values...
This is the code I tried:
String gendd=ViewProfileActivity.participantview.get(4);
System.out.println("Gender:::::"+gendd);
male=(RadioButton)findViewById(R.id.radio0);
female=(RadioButton)findViewById(R.id.radio1);
if(gendd.equals("0")){
male.setSelected(true);
female.setSelected(false);
}
else
{
male.setSelected(false);
female.setSelected(true);
}
But it fails. Can anyone help me?
Try this.
if(gendd.contains("0"))
{
male.setChecked(true);
female.setChecked(false);
}
else
{
male.setChecked(false);
female.setChecked(true);
}
You shoud use check(radioButtonId) method on parent RadioGroup.
E.g.
radioGroup.check(gendd.equals("0") ? R.id.radio0 : R.id.radio1);
(assuming the comparison holds as the types haven't been mentioned)