activity_online_exam.xml
for the activity:
<!-- activity_online_exam.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" tools:context=".OnlineExamActivity"> <TextView android:id="@+id/question_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" /> <RadioGroup android:id="@+id/answers_radio_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/option1_radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/option2_radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> <RadioButton android:id="@+id/option3_radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3" /> <RadioButton android:id="@+id/option4_radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 4" /> </RadioGroup> <Button android:id="@+id/submit_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Submit" /> </LinearLayout>
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; public class OnlineExamActivity extends AppCompatActivity { private TextView questionTextView; private RadioGroup answersRadioGroup; private Button submitButton; private String[] questions; private String[] correctAnswers; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_online_exam); // Initialize views questionTextView = findViewById(R.id.question_text_view); answersRadioGroup = findViewById(R.id.answers_radio_group); submitButton = findViewById(R.id.submit_button); // Initialize exam data questions = getResources().getStringArray(R.array.questions); correctAnswers = getResources().getStringArray(R.array.correct_answers); // Display first question displayQuestion(0); // Set submit button click listener submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int selectedRadioButtonId = answersRadioGroup.getCheckedRadioButtonId(); if (selectedRadioButtonId != -1) { RadioButton selectedRadioButton = findViewById(selectedRadioButtonId); String selectedAnswer = selectedRadioButton.getText().toString(); checkAnswer(selectedAnswer); } else { Toast.makeText(OnlineExamActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show(); } } }); } private void displayQuestion(int questionIndex) { String question = questions[questionIndex]; questionTextView.setText(question); RadioButton option1RadioButton = findViewById(R.id.option1_radio_button); RadioButton option2RadioButton = findViewById(R.id.option2_radio_button); RadioButton option3RadioButton = findViewById(R.id.option3_radio_button); RadioButton option4RadioButton = findViewById(R.id.option4_radio_button); option1RadioButton.setText("Option 1"); option2RadioButton.setText("Option 2"); option3RadioButton.setText("Option 3"); option4RadioButton.setText("Option 4"); } private void checkAnswer(String selectedAnswer) { int questionIndex = answersRadioGroup.indexOfChild(findViewById(answersRadioGroup.getCheckedRadioButtonId())); String correctAnswer = correctAnswers[questionIndex]; if (selectedAnswer.equals(correctAnswer)) { Toast.makeText(this, "Correct Answer!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Wrong Answer!", Toast.LENGTH_SHORT).show(); } } }
In this example, the OnlineExamActivity
class extends AppCompatActivity
.
Inside the onCreate
method, the layout views are initialized, and the exam data is retrieved from the resources. The first question is displayed.
The submit button's click listener checks if an answer is selected and calls the checkAnswer
method to validate the answer.
The displayQuestion
method updates the question text view and radio buttons with the current question and options.
The checkAnswer
method compares the selected answer with the correct answer for the current question and displays a toast message indicating whether the answer is correct or wrong.
Make sure to add the questions
and correct_answers
string array resources to the res/values/strings.xml
file:
<!-- strings.xml --> <resources> ... <string-array name="questions"> <item>Question 1</item> <item>Question 2</item> <item>Question 3</item> <item>Question 4</item> </string-array> <string-array name="correct_answers"> <item>Option 2</item> <item>Option 3</item> <item>Option 1</item> <item>Option 4</item> </string-array> ... </resources>
Remember to replace the questions
array with the actual questions and correct_answers
array with the correct answers for your online examination.
This example provides a basic structure for an online examination management system. You can customize it by adding more questions, options, and additional features such as scoring, timers, and result calculation.