0

Im newbiew to programming, i want create some quiz app. all the answer just use radio button user must choose : 1 or 2 or 3 or 4, all the question have same radiobutton text, the answer just from 1 to 4. i already read how to loop dynamic just radiobutton but how to loop RadioGroup because i just need the number for each question and insert into array,

this is my xml :

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >


            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:textColor="@color/black"
                android:text="0"
                android:button="@null"
                android:drawableTop="@android:drawable/btn_radio" 
                android:gravity="center"
                android:layout_weight="1"/>

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:textColor="@color/black"
                android:text="1" 
                android:button="@null"
                android:drawableTop="@android:drawable/btn_radio" 
                android:gravity="center"
                android:layout_weight="1"/>

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:textColor="@color/black"
                android:text="2" 
                android:button="@null"
                android:drawableTop="@android:drawable/btn_radio" 
                android:gravity="center"
                android:layout_weight="1"/>

            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:textColor="@color/black"
                android:text="3" 
                android:button="@null"
                android:drawableTop="@android:drawable/btn_radio" 
                android:gravity="center"
                android:layout_weight="1" />

        </RadioGroup>

and this is the result : http://tinypic.com/r/2uzs50g/8

i thought if i can just loop radiogroup dynamic with same radiobutton for each question i can just get radiogroup id and insert into array. if you have other sugestion how i should create something like that please tell. thanks for your help all

ikki
  • 23
  • 1
  • 8

3 Answers3

0

Try this :-

RadioGroup rg = (RadioGroup) findViewById(radiogroupid);
String rg_selected_data = "";
int radioButtonId= rg.getCheckedRadioButtonId();
    switch (radioButtonId) {

        case YouRadioButtonIdOne:
            rg_selected_data = "1";
            break;

        case YouRadioButtonIdtTwo:
            rg_selected_data = "2";
            break;

        case YouRadioButtonIdtThree:
            rg_selected_data = "3";
            break;

        case YouRadioButtonIdtFour:
            rg_selected_data = "4";
            break;
    }

Now you can find which radiobutton is selected.

Try this code if you face any problem I will help you. :)

Biswajit
  • 1,829
  • 1
  • 18
  • 33
  • first i thank you sir for your answer, actually i dont under stand how to apply your code because what i want is looping but you give me switch case, i already create the xml and looping this is the screen shoot [link](http://tinypic.com/r/2uzs50g/8) but thanks you sir for the answer i hope u can help me because i already searh and learn for this mather almost 3 weeks thanks – ikki Dec 09 '14 at 07:04
  • Yes sir I will help you. Tell me what is your objective. – Biswajit Dec 09 '14 at 10:34
  • Please help me out on this one, Im trying to produce 4 radio buttons in a for loop dynamically depending upon the user input waht i want is to have radio buttons like *Radio Button1 *RadioButton2 *RadioButton3 *RadioButton4 // *Radio Button1 *RadioButton2 *RadioButton3 *RadioButton4 // *Radio Button1 *RadioButton2 *RadioButton3 *RadioButton4 // *Radio Button1 *RadioButton2 *RadioButton3 *RadioButton4 // and so forth .. depending upon the loop ! and get the values, thanks for your help Sir @Biswajit – ikki Dec 11 '14 at 23:59
  • You mean you want a screen looks like your picture?? If that is your problem then you need to create a custom list row and show that row in every row of the list. I will help you in that. :) – Biswajit Dec 12 '14 at 04:11
0

This solution will run code every time a radio button is checked. It goes in your onCreate(). Source is from here.

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
int questionCounter = 0;
int numberCorrect = 0;
//Question 1 is A, Q2 is C, Q3 is D, Q4 is B, Q5 is A again
final char[] correctAnswersList = {'a', 'c', 'd', 'b', 'a'};
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // checkedId is the RadioButton selected
        switch (checkedId) {
        case (R.id.radio_button_a):
            if (questionCounter==0 || questionCounter==4){
                numberCorrect++;
            }
            break;
        }
        case (R.id.radio_button_b):
            if (questionCounter==3){
                numberCorrect++;
            }
            break;
        }
        case (R.id.radio_button_c):
            if (questionCounter==1){
                numberCorrect++;
            }
            break;
        }
        case (R.id.radio_button_d):
            if (questionCounter==2){
                numberCorrect++;
            }
            break;
        }
    }
});

There is a problem with my code, which was on purpose, since it's finals season in America, and students like to come here last minute to cheat. You'll find it if you look. You wouldn't use looping with this kind of problem, I'm not sure why that's a requirement.

Community
  • 1
  • 1
Eli Rising
  • 485
  • 1
  • 3
  • 15
  • thank you for your answer Eli, but your answer is not what i looking for [Please click here](http://tinypic.com/r/2uzs50g/8) do you have any suggetion how to make like in my picture thanks for your help – ikki Dec 09 '14 at 07:22
  • I'm not sure how that's related to looping. If you want to update the question on a loop, you would have to delve into `AsyncTask`s so that you don't crash the UI thread. Your request isn't clear, I don't understand the requirement about looping. – Eli Rising Dec 09 '14 at 07:28
  • i'm sorry but can you give me direction or some logic/pointerr or anything how i can [create this](http://tinypic.com/r/2uzs50g/8) , example 10 question with just from 1 to 4 like in the pic and when i click button all the answer already insert into array, i really appreciate your help – ikki Dec 09 '14 at 08:04
  • Look up the methods for RadioGroup on Android Developers documentation. You can use a loop to add as many RadioGroups as you want to a vertical LinearLayout. In the loop in OnCreate, you would make a new RadioGroup object(in each iteration), set it up with buttons and text, then add it to an ArrayList from a higher scope. This is so you can reference the radiogroups later. That's the best I can help with. Some key words to help with your work would be: RadioGroup, LinearLayout programatically, ArrayList. – Eli Rising Dec 09 '14 at 08:16
0

for all who want create some quesioner like my self this code will help you, this code will loop RadioGroup and get the value from allradiogroup use List. and thanks for all people who help

        private List<RadioGroup> allradioGroup = new ArrayList<RadioGroup>();
        private RadioGroup radioGroup;

        private List<RadioButton> allRadio = new ArrayList<RadioButton>();
        private RadioButton radioButton;


      // place inside oncreate or whatever you want 

                                                         //your linear layout ID
        LinearLayout linear = (LinearLayout) findViewById(R.id.lintes);
        LinearLayout.LayoutParams layoutParams = new 
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


         for (int i = 0; i < 20; i++) {

                        /* Defining RadioGroup */
                        radioGroup = new RadioGroup(this);
                        radioGroup.setOrientation(RadioGroup.HORIZONTAL);

                        allradioGroup.add(radioGroup);


                        /* Displaying Radio Buttons */
                        for (int j = 0; j < 4; j++) {
                            radioButton = new RadioButton(this);
                         //you can set your button ID here
                            radioButton.setId(j);

                            allRadio.add(radioButton);
                          //set text for each ID
                            if (allRadio.get(j).getId() == 0) {
                                radioButton.setText("0");
                            } else if (allRadio.get(j).getId() == 1) {
                                radioButton.setText("1");
                            } else if (allRadio.get(j).getId() == 2) {
                                radioButton.setText("2");
                            } else if (allRadio.get(j).getId() == 3) {
                                radioButton.setText("3");
                            }
                                //number 4 is total radiobutton

                                allradioGroup.get(i).addView(allRadio.get(i*4+j),j,layoutParams);
                        }

                        // set text
                        TextView soal = new TextView(this);
                        soal.setText("\n"+i+".loop your question here");
                        linear.addView(soal);

                        linear.addView(allradioGroup.get(i));

                    }

and this code will get value from list, just loop (i) and you will get all the value

int id[i] = allradioGroup.get(i).getCheckedRadioButtonId();
ikki
  • 23
  • 1
  • 8