Note - Double Click to Copy Code Contact Us!

Management System for Online Examination

Tech Doubility
Management System for Online Examination



First, let's create the XML layout file activity_online_exam.xml for the activity:
 
XML Code
  1. <!-- activity_online_exam.xml -->
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:padding="16dp"
  8. tools:context=".OnlineExamActivity">
  9.  
  10. <TextView
  11. android:id="@+id/question_text_view"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:textAppearance="?android:attr/textAppearanceMedium" />
  15.  
  16. <RadioGroup
  17. android:id="@+id/answers_radio_group"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:orientation="vertical">
  21.  
  22. <RadioButton
  23. android:id="@+id/option1_radio_button"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="Option 1" />
  27.  
  28. <RadioButton
  29. android:id="@+id/option2_radio_button"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="Option 2" />
  33.  
  34. <RadioButton
  35. android:id="@+id/option3_radio_button"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="Option 3" />
  39.  
  40. <RadioButton
  41. android:id="@+id/option4_radio_button"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="Option 4" />
  45.  
  46. </RadioGroup>
  47.  
  48. <Button
  49. android:id="@+id/submit_button"
  50. android:layout_width="match_parent"
  51. android:layout_height="wrap_content"
  52. android:text="Submit" />
  53.  
  54. </LinearLayout>
  55.  
Next, let's create the Java code for the activity:

Java Code
  1. import androidx.appcompat.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.Button;
  5. import android.widget.RadioButton;
  6. import android.widget.RadioGroup;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9.  
  10. public class OnlineExamActivity extends AppCompatActivity {
  11.  
  12. private TextView questionTextView;
  13. private RadioGroup answersRadioGroup;
  14. private Button submitButton;
  15.  
  16. private String[] questions;
  17. private String[] correctAnswers;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_online_exam);
  23.  
  24. // Initialize views
  25. questionTextView = findViewById(R.id.question_text_view);
  26. answersRadioGroup = findViewById(R.id.answers_radio_group);
  27. submitButton = findViewById(R.id.submit_button);
  28.  
  29. // Initialize exam data
  30. questions = getResources().getStringArray(R.array.questions);
  31. correctAnswers = getResources().getStringArray(R.array.correct_answers);
  32.  
  33. // Display first question
  34. displayQuestion(0);
  35.  
  36. // Set submit button click listener
  37. submitButton.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View v) {
  40. int selectedRadioButtonId = answersRadioGroup.getCheckedRadioButtonId();
  41.  
  42. if (selectedRadioButtonId != -1) {
  43. RadioButton selectedRadioButton = findViewById(selectedRadioButtonId);
  44. String selectedAnswer = selectedRadioButton.getText().toString();
  45.  
  46. checkAnswer(selectedAnswer);
  47. } else {
  48. Toast.makeText(OnlineExamActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show();
  49. }
  50. }
  51. });
  52. }
  53.  
  54. private void displayQuestion(int questionIndex) {
  55. String question = questions[questionIndex];
  56. questionTextView.setText(question);
  57.  
  58. RadioButton option1RadioButton = findViewById(R.id.option1_radio_button);
  59. RadioButton option2RadioButton = findViewById(R.id.option2_radio_button);
  60. RadioButton option3RadioButton = findViewById(R.id.option3_radio_button);
  61. RadioButton option4RadioButton = findViewById(R.id.option4_radio_button);
  62.  
  63. option1RadioButton.setText("Option 1");
  64. option2RadioButton.setText("Option 2");
  65. option3RadioButton.setText("Option 3");
  66. option4RadioButton.setText("Option 4");
  67. }
  68.  
  69. private void checkAnswer(String selectedAnswer) {
  70. int questionIndex = answersRadioGroup.indexOfChild(findViewById(answersRadioGroup.getCheckedRadioButtonId()));
  71. String correctAnswer = correctAnswers[questionIndex];
  72.  
  73. if (selectedAnswer.equals(correctAnswer)) {
  74. Toast.makeText(this, "Correct Answer!", Toast.LENGTH_SHORT).show();
  75. } else {
  76. Toast.makeText(this, "Wrong Answer!", Toast.LENGTH_SHORT).show();
  77. }
  78. }
  79. }
  80.  

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:

 
XML Code
  1. <!-- strings.xml -->
  2. <resources>
  3. ...
  4. <string-array name="questions">
  5. <item>Question 1</item>
  6. <item>Question 2</item>
  7. <item>Question 3</item>
  8. <item>Question 4</item>
  9. </string-array>
  10.  
  11. <string-array name="correct_answers">
  12. <item>Option 2</item>
  13. <item>Option 3</item>
  14. <item>Option 1</item>
  15. <item>Option 4</item>
  16. </string-array>
  17. ...
  18. </resources>
  19.  

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.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.