Last active
November 11, 2015 13:07
-
-
Save jacksierkstra/3586db361bbb926585a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.alexm.iqtest; | |
import android.content.Intent; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.TypedValue; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.LinearLayout; | |
import android.widget.RadioButton; | |
import android.widget.RadioGroup; | |
import android.widget.ScrollView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class IqTest extends AppCompatActivity { | |
LinearLayout linearLayout; | |
TextView question; | |
RadioGroup[] answerGroup; | |
RadioButton[] answer; | |
Button finishButton; | |
Button restartButton; | |
TextView totalScore; | |
RadioButton[] checkedRadioButton; | |
ArrayList<RadioButton> correctAnswerRadios; | |
ArrayList<RadioButton> wrongAnswersRadios; | |
public static int score = 0; | |
public static int numberOfQuestions = 5; | |
public static int numberOfAnswers = 4; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.iq_test); | |
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView); | |
linearLayout = new LinearLayout(this); | |
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); | |
linearLayout.setLayoutParams(linearLayoutParams); | |
linearLayout.setOrientation(linearLayout.VERTICAL); | |
//linearLayout.setPadding(0, 110, 0, 0); | |
scrollView.addView(linearLayout); | |
TextView testTitle = new TextView(this); | |
testTitle.setText(R.string.title_test); | |
testTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); | |
testTitle.setGravity(Gravity.CENTER); | |
testTitle.setTextColor(Color.RED); | |
linearLayout.addView(testTitle); | |
List<Question> questions = new ArrayList<Question>(); | |
// Inserting Questions | |
questions.add(new Question("1. Cate cifre contine anul 2015?", 1)); | |
questions.add(new Question("2. Cate anotimpuri exista?", 2)); | |
questions.add(new Question("3. Care este capitala Americii?", 3)); | |
questions.add(new Question("4. In an a fost Revolutia Romana?", 4)); | |
questions.add(new Question("5. In an s-a terminat WWII?", 5)); | |
List<Answer> answers = new ArrayList<Answer>(); | |
// Inserting Answers / Correct Answer (1 for true, 0 for false) | |
answers.add(new Answer("2", 1, 0)); | |
answers.add(new Answer("3", 1, 0)); | |
answers.add(new Answer("4", 1, 1)); | |
answers.add(new Answer("5", 1, 0)); | |
answers.add(new Answer("2", 2, 0)); | |
answers.add(new Answer("3", 2, 0)); | |
answers.add(new Answer("4", 2, 1)); | |
answers.add(new Answer("7", 2, 0)); | |
answers.add(new Answer("NY", 3, 0)); | |
answers.add(new Answer("WA", 3, 1)); | |
answers.add(new Answer("CH", 3, 0)); | |
answers.add(new Answer("LA", 3, 0)); | |
answers.add(new Answer("1944", 4, 0)); | |
answers.add(new Answer("2011", 4, 0)); | |
answers.add(new Answer("1918", 4, 0)); | |
answers.add(new Answer("1989", 4, 1)); | |
answers.add(new Answer("1944", 5, 1)); | |
answers.add(new Answer("1948", 5, 0)); | |
answers.add(new Answer("1924", 5, 0)); | |
answers.add(new Answer("1914", 5, 0)); | |
// Reading all Questions | |
List<Question> questions = db.getAllQuestions(); | |
// Reading all Answers | |
final List<Answer> answers = db.getAllAnswers(); | |
// List of Correct Answers | |
correctAnswerRadios = new ArrayList<>(); | |
// List of Wrong Answers | |
wrongAnswersRadios = new ArrayList<>(); | |
for (Question qn : questions) { | |
RadioGroup answerGroup = new RadioGroup(this); | |
answerGroup.setOrientation(RadioGroup.VERTICAL); | |
for (Answer an : qn.getAnswers()) { | |
if (qn.getID() == an.getQuestion_id_answer()) { | |
RadioButton answer = new RadioButton(this); | |
answer.setText(an.getAnswer()); | |
answerGroup.addView(answer); | |
} | |
} | |
linearLayout.addView(answerGroup); | |
} | |
finishButton = new Button(this); | |
finishButton.setText(R.string.finishButton); | |
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); | |
finishButton.setLayoutParams(buttonParams); | |
linearLayout.addView(finishButton); | |
restartButton = new Button(this); | |
restartButton.setText(R.string.restartButton); | |
restartButton.setLayoutParams(buttonParams); | |
linearLayout.addView(restartButton); | |
totalScore = new TextView(this); | |
totalScore.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); | |
totalScore.setTextColor(Color.RED); | |
totalScore.setVisibility(totalScore.INVISIBLE); | |
linearLayout.addView(totalScore); | |
restartButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = getIntent(); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); | |
finish(); | |
startActivity(intent); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment