0

"I want to create a quiz section inside fragment tab and the structure should be as below

Question1

RadioGroup1

RadioButton1

RadioButton2

RadioButton3

RadioButton4

Question2

RadioGroup2

RadioButton5

RadioButton6

RadioButton7

RadioButton8

Question3

RadioGroup3

RadioButton9

RadioButton10

RadioButton11

RadioButton12

With the below code i could able to create just one question and one radio button, i want to make it dynamic in the future

private void creatRadioButtons() {
        group = new RadioGroup(this.getContext());
       // group = (RadioGroup) view.findViewById(R.id.radioGroup);
        int[] panels = getResources().getIntArray(R.array.no_of_solar_panels);
        int no_of_que = 2;

       for(int i = 0; i < no_of_que; i++){
          // textView = (TextView) view.findViewById(R.id.question);
           textView = new TextView(this.getContext());
           textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
           textView.setText("Question number " + i);
           linearLayout.addView(textView);
           for (int j = 0;  j< panels.length; j++){
               button = new RadioButton(this.getContext());
               button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
               button.setText(String.valueOf(panels[j]));
               group.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT));
               group.addView(button);

           }
           linearLayout.addView(group);

       }


    }

" This is how my screen is coming up now

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Krishna
  • 39
  • 9
  • If you want such kind of implementation then You need to add Horizontal Scroll View to handle if Radio Button Choices width exceeds than width of screen. Is it okay? – Abhishek Jan 10 '18 at 09:04
  • with the above code i am not able to achieve the above quiz structure, my code is not able generate more than one question and one radio group – Krishna Jan 10 '18 at 09:08
  • I will help you with gson to create such layout in dynamic way. – Abhishek Jan 10 '18 at 09:11
  • i am getting error with the above code, and the error is as follows . java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. – Krishna Jan 10 '18 at 09:45
  • Yes. I will suggest you take Linear Layout for Single Question And Choices and Add it to main linear layout. – Abhishek Jan 10 '18 at 09:52
  • give me the proper questions. I will write the code and will give you. – bnayagrawal Jan 10 '18 at 09:57

4 Answers4

1

Try This I Already Did Quiz Kind Of Application With Question Type Fill In The Blank, Match The Following, Check Box, Radio Button

This is activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/rel_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/questionsLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    </LinearLayout>

</ScrollView>

</RelativeLayout>

This is my QuestionChoiceVo.java (This class to hold question and it's choices)

import java.io.Serializable;
import java.util.ArrayList;


public class QuestionChoiceVo implements Serializable {

String question;

ArrayList<String> choiceArrayList;

public QuestionChoiceVo(String question, ArrayList<String> choiceArrayList) 
{
    this.question = question;
    this.choiceArrayList = choiceArrayList;
}

public String getQuestion() {
    return question;
}

public void setQuestion(String question) {
    this.question = question;
}

public ArrayList<String> getChoiceArrayList() {
    return choiceArrayList;
}

public void setChoiceArrayList(ArrayList<String> choiceArrayList) {
    this.choiceArrayList = choiceArrayList;
}
}

This is my MainActivity class.

public class MainActivity extends AppCompatActivity {

LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    initialiseView();

}

private void initialiseView() {

    linearLayout = (LinearLayout) findViewById(R.id.questionsLinearLayout);

    ArrayList<QuestionChoiceVo> questionChoiceVoArrayList = new ArrayList<>();

    QuestionChoiceVo mQuestionChoiceVoOne = new QuestionChoiceVo("Question One", new ArrayList<String>() {{add("Choice One");add("Choice Two");add("Choice Three");}});
    QuestionChoiceVo mQuestionChoiceVoTwo = new QuestionChoiceVo("Question Two", new ArrayList<String>() {{add("Choice One");add("Choice Two");add("Choice Three");}});
    QuestionChoiceVo mQuestionChoiceVoThree = new QuestionChoiceVo("Question Three", new ArrayList<String>() {{add("Choice One");add("Choice Two");add("Choice Three");}});

    questionChoiceVoArrayList.add(mQuestionChoiceVoOne);
    questionChoiceVoArrayList.add(mQuestionChoiceVoTwo);
    questionChoiceVoArrayList.add(mQuestionChoiceVoThree);

    prepareQuestionAnswerLayout(questionChoiceVoArrayList);

}

private void prepareQuestionAnswerLayout(ArrayList<QuestionChoiceVo> questionChoiceVoArrayList) {

    for (QuestionChoiceVo mQuestionChoiceVo : questionChoiceVoArrayList) {

        LinearLayout mSingleQuestionLinearLayout = new LinearLayout(this);

        mSingleQuestionLinearLayout.setOrientation(LinearLayout.VERTICAL);

        mSingleQuestionLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        TextView mTextView = new TextView(this);

        mTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        mTextView.setText(mQuestionChoiceVo.getQuestion());

        mTextView.setTextSize(20f);

        mSingleQuestionLinearLayout.addView(mTextView);

        RadioGroup mChoiceRadioGroup = setUpChoices(mQuestionChoiceVo);

        RelativeLayout.LayoutParams radioGroupLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        mChoiceRadioGroup.setLayoutParams(radioGroupLayoutParams);

        mSingleQuestionLinearLayout.addView(mChoiceRadioGroup);

        linearLayout.addView(mSingleQuestionLinearLayout);
    }

}

private RadioGroup setUpChoices(QuestionChoiceVo mQuestionChoiceVo) {

    RadioGroup radioGroup = new RadioGroup(this);

    radioGroup.setId(View.generateViewId());

    for (int i = 0; i < mQuestionChoiceVo.getChoiceArrayList().size(); i++){

        RadioButton radioButton = new RadioButton(this);

        radioButton.setText(mQuestionChoiceVo.getChoiceArrayList().get(i));

        radioButton.setTextSize(18f);

        RadioGroup.LayoutParams radioGroupLayoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        radioGroupLayoutParams.setMargins(10, 10, 10, 10);

        radioButton.setPadding(10, 10, 10, 10);

        radioButton.setLayoutParams(radioGroupLayoutParams);

        radioButton.setId(View.generateViewId());

        radioGroup.addView(radioButton);

    }

    return radioGroup;
}


}

Output As Your Requirement :

enter image description here

Abhishek
  • 3,348
  • 3
  • 15
  • 34
1

Please try this code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/parentLayout"/>
</ScrollView>

MainActivity.java

public class MainActivity extends AppCompatActivity {

private LinearLayout parentLayout;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=MainActivity.this;
    parentLayout=findViewById(R.id.parentLayout);
    createRadioButtons();

}
private void createRadioButtons(){
    //Number pf questions
    int[] panels = new int[]{1,2,3,4,5};
    int no_of_que = 20;

    TextView question;
    RadioGroup radioGroup;
    RadioButton radioButton;
    for (int i=0;i<no_of_que;i++){
        question = new TextView(context);
        question.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        question.setText("Question: "+i);
        parentLayout.addView(question);

        radioGroup=new RadioGroup(context);
        radioGroup.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        parentLayout.addView(radioGroup);

        for(int j=0;j<panels.length;j++){
            radioButton=new RadioButton(context);
            radioButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));;
            radioButton.setText("Radio Button");
            radioGroup.addView(radioButton);
        }
    }
}
   }
Mayank Panchal
  • 355
  • 1
  • 10
0

You should use RecyclerView instead of just adding fix number of question at design time,via RecyclerView you can add as much as question you want dynamically.

Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
0

You need to use recyclerview to create dynamic no. of questions. In recycler layout, create a question with four radio buttons.

For recycler example, see this