Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import android.annotation.SuppressLint; import android.os . Bundle; import android.view.View; import android.widget.ProgressBar; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class mcqs extends AppCompatActivity

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class mcqs extends AppCompatActivity {
private TextView questionText;
private RadioGroup optionsGroup;
private TextView progressText;
private ProgressBar progressBar;
private String[] questions;
private String[][] options;
private int[] correctAnswers;
private int currentQuestionIndex =1;
private int score =0;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mcqs);
questionText = findViewById(R.id.questionText);
optionsGroup = findViewById(R.id.optionsGroup);
progressText = findViewById(R.id.progress_text);
progressBar = findViewById(R.id.progress_bar);
// Initialize questions, options, and correct answers
questions = getResources().getStringArray(R.array.questions_mcqs);
options = new String[][]{
getResources().getStringArray(R.array.options_1),
getResources().getStringArray(R.array.options_2),
getResources().getStringArray(R.array.options_3),
getResources().getStringArray(R.array.options_4),
getResources().getStringArray(R.array.options_5),
getResources().getStringArray(R.array.options_6),
getResources().getStringArray(R.array.options_7),
getResources().getStringArray(R.array.options_8),
getResources().getStringArray(R.array.options_9),
getResources().getStringArray(R.array.options_10)
// Add more options arrays as needed
};
correctAnswers = new int[]{0,0,0,0,0,0,0,0,0,0}; // Adjust correct answers based on your quiz logic
loadQuestion();
optionsGroup.setOnCheckedChangeListener((group, checkedId)->{
RadioButton selectedRadioButton = findViewById(checkedId);
int selectedAnswerIndex = optionsGroup.indexOfChild(selectedRadioButton);
// Check if the selected answer is correct
if (selectedAnswerIndex == correctAnswers[currentQuestionIndex]){
score++;
Toast.makeText(this, "Correct!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Incorrect!", Toast.LENGTH_SHORT).show();
}
currentQuestionIndex++;
if (currentQuestionIndex < questions.length){
loadQuestion();
} else {
// End of quiz
questionText.setText("Quiz Completed! Your score: "+ score +"/"+ questions.length);
optionsGroup.setVisibility(View.GONE);
}
});
}
private void loadQuestion(){
questionText.setText(questions[currentQuestionIndex]);
((RadioButton) optionsGroup.getChildAt(0)).setText(options[currentQuestionIndex][0]);
((RadioButton) optionsGroup.getChildAt(1)).setText(options[currentQuestionIndex][1]);
((RadioButton) optionsGroup.getChildAt(2)).setText(options[currentQuestionIndex][2]);
((RadioButton) optionsGroup.getChildAt(3)).setText(options[currentQuestionIndex][3]);
progressText.setText((currentQuestionIndex +1)+"/"+ questions.length);
progressBar.setProgress(currentQuestionIndex +10);
optionsGroup.clearCheck();
}
}
I dont know why but in my string files there are 10 qutions but the quizz ends after the first question

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions