Quiz App Source Code using Java
Android Tutorials Point
Welcome Dear Viewers,
This is an only place for Source code for All Apps. So to make Quiz App copy these codes into your App and Run any time. Literally I have explained each steps in this Tutorial.
I will show you their XML code and Java code for more clarification. So Lets start our Source Code.
Demo of the App
Screen Shot - 1
Screen Shot - 2
Screen Shot - 3
XML Code (Design)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:padding="24dp"
android:background="#ED0956"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/total_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Total Question"
android:textColor="#EEF0F1"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This will be question"
android:layout_marginTop="150dp"
android:textColor="#F1E7E7"
android:textSize="24dp"
android:layout_above="@+id/choices_layout"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/choices_layout"
android:layout_centerInParent="true"
android:orientation="vertical">
<Button
android:id="@+id/ans_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#2196F3"
android:text="Ans A"
android:textColor="@color/black"
android:textSize="24sp"/>
<Button
android:id="@+id/ans_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#2196F3"
android:text="Ans B"
android:textColor="@color/black"
android:textSize="24sp"/>
<Button
android:id="@+id/ans_c"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#2196F3"
android:text="Ans C"
android:textColor="@color/black"
android:textSize="24sp"/>
<Button
android:id="@+id/ans_d"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#2196F3"
android:text="Ans D"
android:textColor="@color/black"
android:textSize="24sp"/>
</LinearLayout>
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#0FDF17"
android:text="Submit"
android:layout_below="@+id/choices_layout"
android:textColor="#F6F3F3"
android:textSize="24sp"
/>
</RelativeLayout>
........................................................
Question Answer Activity (.java)
public class QuestionAnswer {
public static String question []={
"Which Design Platform is your favourite?",
"Which Company owns the Android?",
"Which Language is your favourite?"
};
public static String choices [][]={
{"Canva" , "Paint" , "Adobe" , "PowerPoint"},
{"Google" , "Apple" , "Samsung" , "Nokia"},
{"Java" , "HTML" , "CSS" , "JavaScript"}
};
public static String correctAnswers []={
"Canva" ,
"Google",
"Java"
};
}
.........................................................
Main Activity (Java Code)
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView questionTextView;
TextView totalQuestionTextView;
Button ansA ,ansB , ansC , ansD;
Button btn_submit;
int score = 0;
int totalQuestion = QuestionAnswer.question.length;
int currentQuestionIndex = 0;
String selectedAnswer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
totalQuestionTextView = findViewById(R.id.total_question);
questionTextView = findViewById(R.id.question);
ansA = findViewById(R.id.ans_a);
ansB= findViewById(R.id.ans_b);
ansC = findViewById(R.id.ans_c);
ansD = findViewById(R.id.ans_d);
btn_submit = findViewById(R.id.btn_submit);
ansA.setOnClickListener(this);
ansB.setOnClickListener(this);
ansC.setOnClickListener(this);
ansD.setOnClickListener(this);
btn_submit.setOnClickListener(this);
totalQuestionTextView.setText("Total question :"+totalQuestion);
loadNewQuestion();
}
private void loadNewQuestion() {
if (currentQuestionIndex == totalQuestion){
finishQuiz();
return;
}
questionTextView.setText(QuestionAnswer.question[currentQuestionIndex]);
ansA.setText(QuestionAnswer.choices[currentQuestionIndex][0]);
ansB.setText(QuestionAnswer.choices[currentQuestionIndex][1]);
ansC.setText(QuestionAnswer.choices[currentQuestionIndex][2]);
ansD.setText(QuestionAnswer.choices[currentQuestionIndex][3]);
}
private void finishQuiz() {
String passStatus = "";
if (score > totalQuestion +0.60){
passStatus = "Passed";
}else {
passStatus = "Failed";
}
new AlertDialog.Builder(this)
.setTitle(passStatus)
.setMessage("Score is "+"Out Of "+totalQuestion)
.setPositiveButton("Restart",(dialogInterface, i) -> restartQuiz())
.setCancelable(false)
.show();
}
private void restartQuiz() {
score = 0;
currentQuestionIndex = 0;
loadNewQuestion();
}
@Override
public void onClick(View view) {
ansA.setBackgroundColor(Color.WHITE);
ansB.setBackgroundColor(Color.WHITE);
ansC.setBackgroundColor(Color.WHITE);
ansD.setBackgroundColor(Color.WHITE);
Button clickedButton = (Button) view;
if (clickedButton.getId()==R.id.btn_submit){
if (selectedAnswer.equals(QuestionAnswer.correctAnswers[currentQuestionIndex])){
score++;
currentQuestionIndex++;
loadNewQuestion();
}else {
selectedAnswer = clickedButton.getText().toString();
clickedButton.setBackgroundColor(Color.MAGENTA);
}
}
}}
........................................................
All the Best 😇
Best Regards
Android Tutorials Point
Playlist of Quiz App YouTube Link
https://www.youtube.com/playlist?list=PLLpZnoKq_RexsGst0tCKFUhpw9KzNz_JE
Comments
Post a Comment