com.sweetiepiggy.littlepro.QuestionFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.sweetiepiggy.littlepro.QuestionFragment.java

Source

/*
Copyright (C) 2014 Sweetie Piggy Apps <sweetiepiggyapps@gmail.com>
    
This file is part of Little Pro.
    
Little Pro is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
    
Little Pro is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Little Pro; if not, see <http://www.gnu.org/licenses/>.
*/

package com.sweetiepiggy.littlepro;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class QuestionFragment extends Fragment {
    private static final String ARG_QUESTION_ID = "question_id";
    private static final String ARG_QUESTION = "question";
    private static final String ARG_ANSWER_CHOICES = "answer_choices";
    private static final String ARG_ANSWER = "answer";
    private static final String ARG_POINTS = "points";
    private static final String ARG_SUBMITTED = "submitted";
    private Question mQuestion = null;
    private String mAnswer = null;
    private boolean mSubmitted = false;

    public static QuestionFragment newInstance(Question question, boolean submitted) {
        QuestionFragment fragment = new QuestionFragment();
        Bundle args = new Bundle();
        args.putLong(ARG_QUESTION_ID, question.getId());
        args.putString(ARG_QUESTION, question.getQuestion());
        args.putBoolean(ARG_SUBMITTED, submitted);
        args.putStringArray(ARG_ANSWER_CHOICES, question.getAnswerChoices().toArray(new String[0]));
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            Bundle bundle = getArguments();
            if (bundle != null) {
                loadState(bundle);
            }
        } else {
            loadState(savedInstanceState);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putLong(ARG_QUESTION_ID, mQuestion.getId());
        savedInstanceState.putString(ARG_QUESTION, mQuestion.getQuestion());
        savedInstanceState.putStringArray(ARG_ANSWER_CHOICES, mQuestion.getAnswerChoices().toArray(new String[0]));
        savedInstanceState.putString(ARG_ANSWER, getAnswer());
        savedInstanceState.putInt(ARG_POINTS, mQuestion.getPoints());
        savedInstanceState.putBoolean(ARG_SUBMITTED, mSubmitted);
        super.onSaveInstanceState(savedInstanceState);
    }

    public String getAnswer() {
        return getAnswer(getView());
    }

    public Long getQuestionId() {
        return mQuestion == null ? -1 : mQuestion.getId();
    }

    public Question getQuestion() {
        return mQuestion;
    }

    private void loadState(Bundle bundle) {
        long id = bundle.getLong(ARG_QUESTION_ID, -1);
        String question = bundle.getString(ARG_QUESTION);
        List<String> answerChoices = new ArrayList<String>(
                Arrays.asList(bundle.getStringArray(ARG_ANSWER_CHOICES)));
        mAnswer = bundle.getString(ARG_ANSWER);
        int points = bundle.getInt(ARG_POINTS);
        if (bundle.getBoolean(ARG_SUBMITTED, false)) {
            mSubmitted = true;
        }

        mQuestion = new Question(id, question, answerChoices, points);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_question, container, false);
        ((WebView) view.findViewById(R.id.question)).setBackgroundColor(Color.TRANSPARENT);
        ((WebView) view.findViewById(R.id.question)).loadData(mQuestion.getQuestion(), "text/html", "utf-8");

        List<String> answerChoices = mQuestion.getAnswerChoices();
        if (answerChoices.isEmpty()) {
            createFillInBlank(view);
        } else {
            createMultipleChoice(view, answerChoices);
        }

        return view;
    }

    public void onSubmit(String correctAnswer) {
        mSubmitted = true;
        View view = getView();
        if (view != null) {
            view.findViewById(R.id.answer).setEnabled(false);

            RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.answerRadioGroup);
            for (int i = 0; i < radioGroup.getChildCount(); ++i) {
                radioGroup.getChildAt(i).setEnabled(false);
            }
        }
    }

    private void createFillInBlank(View v) {
        v.findViewById(R.id.answerRadioGroup).setVisibility(View.GONE);
        TextView answerView = (TextView) v.findViewById(R.id.answer);
        answerView.setText(mAnswer);
        if (mSubmitted) {
            answerView.setEnabled(false);
        }
    }

    private void createMultipleChoice(View v, List<String> answerChoices) {
        v.findViewById(R.id.answer).setVisibility(View.GONE);
        RadioGroup radioGroup = (RadioGroup) v.findViewById(R.id.answerRadioGroup);
        for (String answerChoice : answerChoices) {
            RadioButton radioButton = new RadioButton(getActivity());
            radioButton.setText(answerChoice);
            radioButton.setTextAppearance(getActivity(), R.style.Entry);
            radioGroup.addView(radioButton);
            if (answerChoice == mAnswer) {
                radioButton.toggle();
            }
            if (mSubmitted) {
                radioButton.setEnabled(false);
            }
        }
    }

    private String getAnswer(View v) {
        if (v != null) {
            int selectedRadio = ((RadioGroup) v.findViewById(R.id.answerRadioGroup)).getCheckedRadioButtonId();
            mAnswer = selectedRadio == -1 ? ((EditText) v.findViewById(R.id.answer)).getText().toString()
                    : ((RadioButton) v.findViewById(selectedRadio)).getText().toString();
        }
        return mAnswer;
    }
}