Radio button shuffling in android - android

I am trying to shuffle radio button in android, here's is the code what i have done till now, but as I proceed further I am not getting what should be the code or how to do that.
Please correct if I am wrong, or doing some silly mistake as I am new to it.
ArrayList<RadioButton> arrayText = new ArrayList<>();
arrayText.add(ropt0);
arrayText.add(ropt1);
arrayText.add(ropt2);
arrayText.add(ranswer);
Collections.shuffle(arrayText);
I am not getting what next after Collections.shuffle(arrayText);
should be the code
Please suggest

I think you don't quite understand what are you doing and what do you want. In your code you just adding all your RadioButtons to ArrayList and then shuffle this list. This is same as:
ArrayList<Stirng> list = new ArrayList<String>();
list.add("a1");
list.add("a2");
Collections.shuffle(list);
You see, this isn't connected to UI in any way. If you want to shuffle buttons in UI (so user will see it) you have 2 approaches:
Create and add your radio buttons in the code. Define some RadioGroup in view XML file and then create and add your RadioButtons
RadioGroup lay = (RadioGroup) findViewById(R.id.my_radio_group);
List<RadioButton> buttons = new ArrayList<>();
RadioButton rb1 = new RadioButton(this);
RadioButton rb2 = new RadioButton(this);
buttons.add(rb1);
buttons.add(rb2);
Collections.shuffle(buttons);
for(RadioButton rb:
buttons){
lay.addView(rb,new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
Shuffle just a right button position. Create N buttons in XML. And then set all texts in your code, so you can decide which button is the right one.
List<String> answers = new ArrayList<>(4); // 4 - button number
//Fill the list here
int rightAnswerPosition = ...; //Decide it when you fill your list
rb1.setText(answers.get(0));
//.. and so on

Related

How to get all of unchecked radio buttons in android

I have 16 Radio Group in my layout and i have 40 Radio Button . I want to get which Radio Button is unchecked in Radio Groups. I want to know how can i know is there any unchecked Radio Button in my layout thanks
You should probably group all of your buttons like so:
RadioGroup rg = (RadioGroup) findViewById(R.id.my_radio_group);
List<RadioButton> radioButtonsList = new ArrayList<>();
for(int i = 0; i < rg.getChildCount(); ++i) {
RadioButton b = rg.getChildAt(i);
if(b.isChecked()) radioButtonsList.add(b);
}
Do it for all of your groups and you'll have all your unchecked buttons in a list.
Also you can use:
int checkedRadioButtonId = rg.getCheckedRadioButtonId()
to get only checked button's id.
ArrayList<RadioGroup> radioGroupList = new ArrayList<RadioGroup>();
RadioGroup group1 = (RadioGroup)findViewById(...);
RadioGroup group2 = (RadioGroup)findViewById(...);
.
.
RadioGroup group16 = (RadioGroup)findViewById(...);
radioGroupList.add(group1);
radioGroupList.add(group2);
.
.
radioGroupList.add(group16);
and later you can check which is checked or not with this
for(RadioGroup radioButtonGroup:RadioGroupList){
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
}
or if it's the RadioButtons you are interested in then add them in an ArrayList the same way and loop in that list like this
for(RadioButton radioButton:radioButtonList){
boolean isChecked = radioButton.isChecked();
}
You can check the state of radio buttons by using the isChecked() method.
This question has already been answered here:
How to check if "Radiobutton" is checked?

Two radio buttons getting selected in Radio Group

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

android: radio buttons inside radio buttons

I am quite new to Android and this is my first application, so please correct if my question is not clear and I will gladly add more information about what I am trying to achieve.
I have a radio group built dynamically. Inside this radio group I would like to have another radio group, depending on the radio button chosen from the first group.
So, let's say I have an array list of items and for each item I have some sizes available (i.e: XS, S, L). If I check the radio button "XS", I would like to have another radio group with the available colors for the selected size, XS.
The way I have built this is by creating a radio group and it's radio buttons dynamically. Inside the method onCheckedChanged(), I am calling the method createRadioButtonsForAvailableColors(). This one creates the radio buttons with the necessary colors for the checked size, but once I check another size in the upper radio group, the new colors available for this size are added to the colors shown for the size selected before.
Thank you.
Here is my on create method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_product_details);
sizesList = getSizes(getIDFromPreviousActivity());
createRadioButtonsForAvailableSizes(sizesList);
}
Here is my creation for the radio group containing the radio buttons with the available sizes:
// creates the radio buttons with the available sizes
public void createRadioButtonsForAvailableSizes(ArrayList<String> sizeList) {
productDetailsLayout = (LinearLayout) findViewById(R.id.productDetailsLayout);
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
int n = sizeList.size();
final RadioButton[] rb = new RadioButton[n];
for(int i=0; i< sizeList.size(); i++) {
colorList = getColors(getIDFromPreviousActivity(),sizeList.get(i));
rg.setOrientation(RadioGroup.HORIZONTAL);
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(sizesList.get(i).toString());
rb[i].setId(getIDForRadioButton(sizesList.get(i).toString()));
rb[i].setButtonDrawable(R.drawable.radiobuttonunchecked);
rb[i].setOnCheckedChangeListener(this);
}
productDetailsLayout.addView(rg);
productDetailsLayout.setPadding(50, 50, 50, 50);
}
Here is the creation of the colors (same as for the sizes):
// create radio buttons for available colors
public void createRadioButtonsForAvailableColors(ArrayList<String> colorList) {
Log.d("createRadioButtonsForAvailableColors","");
productDetailsLayout = (LinearLayout) findViewById(R.id.productDetailsLayout);
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
int n = colorList.size();
final RadioButton[] rb = new RadioButton[n];
for(int i=0; i< colorList.size(); i++) {
Log.d("color"+i,colorList.get(i));
rg.setOrientation(RadioGroup.HORIZONTAL);
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(colorList.get(i).toString());
rb[i].setId(getIDForRadioButton(colorList.get(i).toString()));
rb[i].setButtonDrawable(R.drawable.radiobuttonunchecked);
rb[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override public void onCheckedChanged(CompoundButton button, boolean isChecked) {
button.setButtonDrawable(isChecked ? R.drawable.radiobuttonchecked : R.drawable.radiobuttonunchecked);
}
});
}
productDetailsLayout.addView(rg);
productDetailsLayout.setPadding(50, 50, 50, 50);
}
Here is my onCheckedChange method:
#Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
button.setButtonDrawable(isChecked ? R.drawable.radiobuttonchecked : R.drawable.radiobuttonunchecked);
String text = button.getText().toString();
int productID = getIDFromPreviousActivity();
ArrayList<String> colorList = getColors(productID, text);
createRadioButtonsForAvailableColors(colorList);
}
The inner group is created right, but upon checking one size in the first group, it builds the subgroup radio buttons normally. If I click one size, it shows the available colors. But, on changing the size checked, the buttons showing the colors of the now selected size are added to the buttons shown before for the previous size selected. How can I cancel the inner buttons created when I selected first time the size and show only the available colors for the currently selected size?
I think this is maybe not the right approach to build it. So, I have 2 questions:
Usually, what would be the best way to do this? Since my inner radio group depends on the radio button chosen from the first group, I assume the creation of the inner group should be called inside the onCheckedChange() method. Should I start another activity from here or can I do it all in one activity?
If my approach is correct, can you please tell me how to delete the inner radio buttons created, in case the radio button from the main group is changed?
Thank you
So, if I understood this, the problem comes when you change the value of the sizes radio button, meaning the colors don't disappear.
Well, Java is all about a lot of boilerplating, and there's not pretty much to say (I'd probably go for a similar way).
You could add this inside the createRadioButtonsForAvailableColors method, just before you start adding content:
productDetailsLayout.removeViews(0, productDetailsLayout.getChildCount());
RemoveViews will remove any amount of child views starting from a position (hence I amb passing it 0 and the total of children).

Android RadioGroup checks more than one RadioButton?

I am using RadioGroup, added RadioButton rdbut to RadioGroup rdgrp like rdgrp.addView(rdbut).
for(int j=0;j<3;j++)
{
RadioGroup rdgrp = new RadioGroup;
for(int i=0;i<=10;i++)
{
RadioButton rdbut = new RadioButton(this);
rdbut.setText("RadioButtion"+i);
rdbut.setId(i);
rdbut.setTag("somename");
rdgrp.addView(rdbut);
}
}
the above code shows how I initialize the radiogroup and radio button. after I run the this code, in emulator/mobile , i am able to check 2 radio buttons at a time.
What could be the problem?
Change your code like this.
RadioGroup rdgrp[] = new RadioGroup[3];
For(int j=0;j<3;j++)
{
RadioButton rdbut[] = new RadioButton[10];
For(int i=0;i<=10;i++)
{
rdbut[i].setText("RadioButtion"+i);
rdbut[i].setId(j*100+i);
rdbut[i].setTag("somename");
rdgrp[j].addView(rdbut[i]);
}
}
You have created three different Radio Group. You can select only one radio button from a single group.So from three groups you can select three buttons But there is no inter-group relationship. You can select radio buttons from different group simultaneously. In your case you can select three buttons at max.
Thanks
Sunil
Use Some thing like this xml design in user layout file.
<TableLayout
android:id="#+id/tbl_layoutchoice"
style="#style/InfoTableView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip" >
<RadioGroup
android:id="#+id/SelectLayout_Group"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RadioGroup>
</TableLayout>
and For Use this RadioGroup in Activity 's OnCreate() Method and findView like this
mRadioGroup = (RadioGroup) this.findViewById(R.id.SelectLayout_Group);
and Then Use Below Code With Your Require Change To Add Radio Button in One RadioGroup.Use Also Below Require Declaration for Create Radio Button Dynamically.
ArrayList<String> layoutlist = new ArrayList<String>(3);
int index = -1;
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
for (String layout : layoutlist) {
RadioButton r = new RadioButton(this);
index++;
r.setText(layout);
r.setId(index);
r.setLayoutParams(lp);
r.setTextAppearance(this, R.style.TextBase);
mRadioGroup.addView(r);
}
So don't forget to add your String Value in layoutlist before for loop .and R.style is some Require Style for Text Show in RadioButton.

creating Quick rate dynamically using radio buttons

I am creating an application which fetches a form for ratings, dynamically. But when I implement this code, the radio group selects 2 options (radio buttons). I want to take rating in the form of radio buttons and the user will be able to select only 1 radio button. Check my code:
for(int i=0;i<count;i++)
{
LinearLayout lay_hori=new LinearLayout(this);
lay_hori.setOrientation(LinearLayout.HORIZONTAL);
TextView optionText=new TextView(this);
optionText.setText(current_ques.option_text[i]);
optionText.setTextColor(Color.BLACK);
optionText.setWidth(110);
rg_rating[i]=new RadioGroup(this); //radio group
rg_rating[i].setMinimumHeight(30);
rg_rating[i].setId(i);
rg_rating[i].setOrientation(RadioGroup.HORIZONTAL);
for(int j=0;j<4;j++)
{
rb_rating[j]=new RadioButton(this); //radio buttons
rb_rating[j].setId(j);
rg_rating[i].addView(rb_rating[j]);
Log.i("rg_rating[i]", ""+rg_rating[i]);
Log.i("rb_rating[j]",""+ rb_rating[j]);
}
Log.i("rg_rating[i]Child", ""+rg_rating[i].getChildCount()); //it returns that each radio group has 4 child,correct
rg_rating[i].setOnCheckedChangeListener(checkRadioGroup);
lay_hori.addView(optionText);
lay_hori.addView(rg_rating[i]);
layout_main.addView(lay_hori);
}
but at run time in a radio group,radio buttons select 2 options rather than 1
I feel that adding an array of Radio Buttons in an array of Radio Groups is not a good idea.
I didn't get a chance to test your code, but can you try the following code?
I have just stopped using an array of Radio Buttons, because ultimately you're adding only one element to the radio group inside the j loop.
Please correct any errors that you might encounter.
for(int i=0;i<count;i++)
{
LinearLayout lay_hori=new LinearLayout(this);
lay_hori.setOrientation(LinearLayout.HORIZONTAL);
TextView optionText=new TextView(this);
optionText.setText(current_ques.option_text[i]);
optionText.setTextColor(Color.BLACK);
optionText.setWidth(110);
rg_rating[i]=new RadioGroup(this); //radio group
rg_rating[i].setMinimumHeight(30);
rg_rating[i].setId(i);
rg_rating[i].setOrientation(RadioGroup.HORIZONTAL);
for(int j=0;j<4;j++)
{
rb_rating=new RadioButton(this); //radio buttons
rb_rating.setId(j);
rg_rating[i].addView(rb_rating);
Log.i("rg_rating[i]", ""+rg_rating[i]);
Log.i("rb_rating[j]",""+ rb_rating.getId());
}
Log.i("rg_rating[i]Child", ""+rg_rating[i].getChildCount()); //it returns that each radio group has 4 child,correct
rg_rating[i].setOnCheckedChangeListener(checkRadioGroup);
lay_hori.addView(optionText);
lay_hori.addView(rg_rating[i]);
layout_main.addView(lay_hori);
}

Categories

Resources