Hello guys I need to make a simple android quiz. Well what I’ve done so for is these with the help of other examples online.
MyOSIActivity.java
package com.mns.mp;
import tp.mns.mp.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyOSIActivity extends Activity {
private RadioGroup radioAnswerGroup;
private RadioButton radioAnswerButton;
private Button btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioAnswerGroup = (RadioGroup) findViewById(R.id.radioAnswer);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioAnswerGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioAnswerButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyOSIActivity.this,
radioAnswerButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/Qn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Qn_1"
/>
<RadioGroup
android:id="@+id/radioAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_1"
android:checked="true" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_2" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_3" />
</RadioGroup>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_submit" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MyAndroidAppActivity!</string>
<string name="app_name">MyAndroidApp</string>
<string name="Qn_1"> Question 1: Acknowledgement, Sequencing, and Flow control are characteristics of which OSI layer?</string>
<string name="radio_1">Layer 2</string>
<string name="radio_2"> Layer 3</string>
<string name="radio_3"> Layer 4</string>
<string name="btn_submit">Submit</string>
</resources>
Ok now I’m trying to make the system compare the user answer with the correct answer after the user press submit button which in this case its Layer 4. Then if the user answer is wrong, the toast will reply to the user it is incorrect and show the correct answer. Where as if the user is correct then it will reply correct to the user. So I need to use if else but the thing is I am still not familiar with it. Ill appreciate if you guys give me guidances because I really need it. Thank you soo much 😀
you can do this in so many ways…. if you know the id of the correct answer, you can do: