Sheetal Kahane
Sheetal Kahane

Reputation: 25

How to set checked Radio Button on Previous Button in Android?

Hi I'm beginner in Android. I have implementing a project which is including question and their answers options with radio button and two buttons next and previous.

1) I have use xml parser

2) When i select answers option pressing radio button and click next its appearing next question on the same Activity.

3) When i pressed the previous button it goes to previous question with answers option radio button on the same Activity.

Right now i want to Radio Button Checked in android while i go to previous question on click previous button which i selected. Can Someone help me how to do .Thanks to appreciate.

Here is my code.

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questions_screen);

        databaseHelper = new DatabaseHelper(this);
        databaseHelper.onOpen(sqdb);

        mAnimation = new AlphaAnimation(1.0F, 0.0F);
        mAnimation.setDuration(200L);
        mAnimation.setInterpolator(new LinearInterpolator());
        mAnimation.setRepeatCount(-1);
        mAnimation.setRepeatMode(2);


        txtViewQuestiontype = (TextView)findViewById(R.id.QuestionType_TXT);
        txtViewQuestion = (TextView)findViewById(R.id.Question_TXT);

        Options_RadioGroup = (RadioGroup)findViewById(R.id.RDOptions);
        AnswerOptionsA_RadioButton = (RadioButton)findViewById(R.id.RBOption_A);
        AnswerOptionsB_RadioButton = (RadioButton)findViewById(R.id.RBOption_B);
        AnswerOptionsC_RadioButton = (RadioButton)findViewById(R.id.RBOption_C);
        AnswerOptionsD_RadioButton = (RadioButton)findViewById(R.id.RBOption_D);

          button_CheckAnswer = (Button)findViewById(R.id.Button_Check_Answer_BTN);
          button_Previouse = (Button)findViewById(R.id.Button_Previous_BTN);
          button_Next  = (Button)findViewById(R.id.Button_Next_BTN);
          button_QuestionLimit = (Button)findViewById(R.id.Btn_QuestionLimit);
          button_Home = (Button)findViewById(R.id.btnHome);

         read_Questions_XML_File();

          button_Previouse.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    previousQuestionCalled(v);
                }
            });


            button_Next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    storingResult(); 
                    nextQuestionCalled(v);
                }
            });
    }

    public void storingResult()
    {
        int id = Options_RadioGroup.getCheckedRadioButtonId();
        System.out.println("id = " + id);

        if (id > 0)
        {
            AnswerOptions = (RadioButton) findViewById(Options_RadioGroup
                    .getCheckedRadioButtonId());

            String str_AnswerOptions = AnswerOptions.getText().toString()
                    .trim();
            System.out.println("rbVal = " + str_AnswerOptions);

            if (str_AnswerOptions
                    .equals(((Datastructure) Vectore_mquestionDatabaseStructure
                            .get(StaticClass.QuestionNumber)).Answer))
            {
                // Not StaticClass.isTest = false
                if (!StaticClass.isTest)

                {


                    String str_queNo = button_QuestionLimit.getText()
                            .toString().trim();
                    String strstrqueType = txtViewQuestiontype.getText()
                            .toString().trim();
                    String str_que = txtViewQuestion.getText().toString()
                            .trim();
                    String str_marks = "1";

                    databaseHelper.insertQueDetails(str_queNo, strstrqueType,
                            str_que, str_AnswerOptions, str_marks);

                    Toast.makeText(getApplicationContext()," Data Crated Right Succesfully ",Toast.LENGTH_SHORT).show();
                }

                if (StaticClass.isTest)
                {
                    StaticClass.resultOfTest = 1 + StaticClass.resultOfTest;
                    ((Datastructure) Vectore_mquestionDatabaseStructure
                            .elementAt(StaticClass.QuestionNumber))
                            .setResult_Of_Test(true);
                    return;
                }

            }
        }

        else if (id == -1)
        {
            Toast.makeText(this, "Please Select Any Option", Toast.LENGTH_LONG)
                    .show();
            try 
            {
                if (StaticClass.isTest)
                {
                    ((Datastructure) Vectore_mquestionDatabaseStructure
                            .elementAt(StaticClass.QuestionNumber))
                            .setResult_Of_Test(false);
                }

                if (!StaticClass.isTest)
                {
                    Toast.makeText(getApplicationContext(), "Wrong",
                            Toast.LENGTH_SHORT).show();
                }

                ((Datastructure) Vectore_mquestionDatabaseStructure
                        .get(StaticClass.QuestionNumber))
                        .setResult_Of_Test(false);

                return;
            }

            catch (Exception exception)
            {
                exception.printStackTrace();
            }
        }

    }


    public void nextQuestionCalled(View view)
    {
        //if animationForward == false
        if (animationForward)
        {
            //flag = false
            boolean flag;
            if (animationForward)
            {  flag = false; }

            else { flag = true;}

            animationForward = flag;
        }
        //Return The Number Of Element in this Vector > 0
        //-1 + 1
        if (-1 + Vectore_mquestionDatabaseStructure.size() > StaticClass.QuestionNumber)
        {
            //If StaticClass.isTest == false
            if (StaticClass.isTest) 
            {
                StaticClass.resultOfTest_Out_OF = 1 + StaticClass.resultOfTest_Out_OF;
                storingResult();
            }
            StaticClass.QuestionNumber = 1 + StaticClass.QuestionNumber;
            reHitting();

            view.clearAnimation();
            if (!StaticClass.isTest) {
                button_QuestionLimit.clearAnimation();
            }

            if (StaticClass.isTest) {
                button_QuestionLimit.clearAnimation();
            }

            return;
        }

        else
        {
            button_QuestionLimit.startAnimation(mAnimation);
            return;
        }
    }






    public void syoutF()
    {
        addVallues();
    }

    public void previousQuestionCalled(View view)
    {
        if (!animationForward)
        {
            boolean flag;

            if (animationForward) 
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            animationForward = flag;
        }
        if (StaticClass.QuestionNumber > 0)
        {
            if (!StaticClass.isTest)
            {
                StaticClass.QuestionNumber = -1 + StaticClass.QuestionNumber;
                reHitting();
                button_QuestionLimit.clearAnimation();
            }
            view.clearAnimation();

        } 
        else 
        {
            button_QuestionLimit.startAnimation(mAnimation);
        }

    }


    @SuppressLint("NewApi")
    public void reHitting() {
        addVallues();
        if (!animationForward)
        {
            overridePendingTransition(R.anim.anim_in,R.anim.anim_out);
            return;
        } 
        else 
        {
            overridePendingTransition(R.anim.anim_in,R.anim.anim_out);
            return;
        }
    }

    public void addVallues() {
        try {
            txtViewQuestiontype.setText(((Datastructure) Vectore_mquestionDatabaseStructure
                    .get(StaticClass.QuestionNumber)).Question_Type);

            txtViewQuestion.setText(((Datastructure) Vectore_mquestionDatabaseStructure
                    .get(StaticClass.QuestionNumber)).Question);

            AnswerOptionsA_RadioButton.setText(((Datastructure) Vectore_mquestionDatabaseStructure
                    .get(StaticClass.QuestionNumber)).OptionA);

            AnswerOptionsB_RadioButton.setText(((Datastructure) Vectore_mquestionDatabaseStructure
                    .get(StaticClass.QuestionNumber)).OptionB);

            AnswerOptionsC_RadioButton.setText(((Datastructure) Vectore_mquestionDatabaseStructure
                    .get(StaticClass.QuestionNumber)).OptionC);

            AnswerOptionsD_RadioButton.setText(((Datastructure) Vectore_mquestionDatabaseStructure
                    .get(StaticClass.QuestionNumber)).OptionD);
            QuestionLimitFetcher();
            Options_RadioGroup.clearCheck();
            return;
        } catch (ArrayIndexOutOfBoundsException arrayindexoutofboundsexception) {
            arrayindexoutofboundsexception.printStackTrace();
        }
    }


    public void QuestionLimitFetcher() {
        int i = Vectore_mquestionDatabaseStructure.size();
        button_QuestionLimit.setText((new StringBuilder(String.valueOf(1 + StaticClass.QuestionNumber)))
                                                               .append("/").append(i).toString());

        String strLimit = button_QuestionLimit.getText().toString().trim();
        System.out.println("strLimit = " + strLimit);
    }



    public void read_Questions_XML_File()
    {

        Bundle extras = getIntent().getExtras();
        String string_Chapter_Title = extras.getString("TOPIC_TITLE");
        System.out.println("value1 = " + string_Chapter_Title);

        try
            {
                if (string_Chapter_Title.equals("OOPs concepts"))
                {
                    String encodedXML = URLEncoder.encode("collections.xml");
                    db = SAXXMLParser.parse(getAssets().open(encodedXML));
                    i = Vectore_mquestionDatabaseStructure.size();
                    System.out.println("Vector Size i" + i);

                    int lastIndex = Vectore_mquestionDatabaseStructure.lastIndexOf(db.lastElement());
                    System.out.println("lastIndex = " + lastIndex);



                }

                else if (string_Chapter_Title.equals("Java virtual machine"))
                {
                    String encodedXML = URLEncoder.encode("java_keyword.xml");
                    db = SAXXMLParser.parse(getAssets().open(encodedXML));
                    i = Vectore_mquestionDatabaseStructure.size();
                    System.out.println("Vector Size i" + i);

                    System.out.println("Last element: " + (Integer)Vectore_mquestionDatabaseStructure.lastElement()); 
                }

            } 
            catch (IOException ioexception)
            {
                ioexception.printStackTrace();
            } 

            catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

            catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            syoutF();
        }

    }

Upvotes: 0

Views: 810

Answers (1)

Suhail Mehta
Suhail Mehta

Reputation: 5542

You can save the marked answer in shared Preferences against question number,means make Question number or any other valid unique key related to question as key for shared Preferences

like

editor.put("Q1",1);  // editor.put(QuestionNo, AnswerNumber);

Now when you move back first check if this question has key in shared Preferences, if yes the check the radiobutton according to the answerNumber saved .

Upvotes: 1

Related Questions