Can we use switch statement in making a quiz app in android studio by using buttons?












0















I have come across some tutorials using switch statements to make quiz app by using radio buttons, but I intend to use buttons but not radio buttons to do this... Can I? I want the user to be able to play the quiz app by clicking buttons but not radio buttons. Please guide me through!! Thanks in advance!



The tutorials I mentioned is as below:



MainActivity,java file code



package com.example.ab.quiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
EditText ed1;
TextView tv1,tv2,tv3;
RadioButton a,b,c,d;
Button bt;
RadioGroup rg;
int q,s;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.name);
tv1=(TextView)findViewById(R.id.ques);
tv2=(TextView)findViewById(R.id.response);
tv3=(TextView)findViewById(R.id.score);
rg=(RadioGroup)findViewById(R.id.optionGroup);
a=(RadioButton)findViewById(R.id.option1);
b=(RadioButton)findViewById(R.id.option2);
c=(RadioButton)findViewById(R.id.option3);
d=(RadioButton)findViewById(R.id.option4);
bt=(Button)findViewById(R.id.next);
q=0;
s=0;

}
public void quiz(View v){
switch (q){
case 0:
{
rg.setVisibility(View.VISIBLE);
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
tv2.setText("");
tv3.setText("");
a.setEnabled(true);
b.setEnabled(true);
c.setEnabled(true);
d.setEnabled(true);
ed1.setEnabled(true);
bt.setText("Next");
s=0;

tv1.setText("1.When did India its independece?");
a.setText("1847");
b.setText("1947");
c.setText("1850");
d.setText("1950");
q=1;
break;
}
case 1:
{
ed1.setEnabled(false);
tv1.setText("2.Who is India's father of nation?");
a.setText("Mahatma Gandhi");
b.setText("Lal Bahadur Shashtri");
c.setText("Jawaharlal Nehru");
d.setText("Subhash Chandra Bose");

if (b.isChecked()){
tv2.setText("Right Answer");
s=s+10;
}
else
{
tv2.setText("Wrong Answer B was Right Answer");
}
q=2;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 2:
{
tv1.setText("3.Who was the first lady CM of Uttar Pradesh?");
a.setText("Pratibha Singh Patil");
b.setText("Indira Gandhi");
c.setText("Sucheta Kriplani");
d.setText("Mayawati");
if (a.isChecked()){
s=s+10;
tv2.setText("Right Answer");

}
else
{
tv2.setText("Wrong Answer A was Right Answer");
}
q=3;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 3:
{

tv1.setText("4.Who was first Indian lady to go in space?");
a.setText("Mayawati");
b.setText("Kalpana Chawla");
c.setText("Kiran Bedi");
d.setText("Sunita Williams");
if (c.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer C was Right Answer");
}
q=4;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 4:
{
tv1.setText("5.Who designed India's national flag?");
a.setText("Rahul Gandhi");
b.setText("Bankim Chandra Chatterjee");
c.setText("Ishwar Chandra Vidyasagar");
d.setText("Pingali Venkayya");
if (b.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer B was Right Answer");
}
q=5;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
bt.setText("Finish");
break;
}
case 5:
{
a.setEnabled(false);
b.setEnabled(false);
c.setEnabled(false);
d.setEnabled(false);
if (d.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer D was Right Answer");
}
tv3.setText(ed1.getText()+"'s final score is "+s);
bt.setText("Restart");
q=0;
break;
}
}
}
}


and...



android_main.xml file code



<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
tools:context="com.example.ab.quiz.MainActivity"
android:background="#22FF00FF">

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="Enter your name please"
android:inputType="textPersonName"
android:textSize="20sp" />

<TextView
android:id="@+id/ques"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="21dp"
android:text=""
android:textSize="20sp"
android:textStyle="bold" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_below="@+id/ques"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/optionGroup"
android:visibility="invisible">

<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ques"
android:layout_marginLeft="50dp"
android:text="" />

<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/option1"
android:layout_marginLeft="200dp"
android:text="" />

<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/option1"
android:layout_alignLeft="@+id/option1"
android:layout_marginLeft="50dp"
android:text="" />

<RadioButton
android:id="@+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/option2"
android:layout_alignLeft="@+id/option2"
android:layout_marginLeft="200dp"
android:text="" />
</RadioGroup>

<TextView
android:id="@+id/response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_below="@+id/optionGroup"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp" />

<Button
android:id="@+id/next"
android:onClick="quiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Start"
android:layout_below="@+id/response"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="33dp"
android:layout_below="@+id/next"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>









share|improve this question

























  • SO is a terrible tutorial site. Your best bet is to work on that tutorial or find another one and come back when you have a specific question.

    – jdv
    Nov 13 '18 at 15:20











  • I have updated my question... Can anyone help?

    – Alonso Tan
    Nov 23 '18 at 8:14
















0















I have come across some tutorials using switch statements to make quiz app by using radio buttons, but I intend to use buttons but not radio buttons to do this... Can I? I want the user to be able to play the quiz app by clicking buttons but not radio buttons. Please guide me through!! Thanks in advance!



The tutorials I mentioned is as below:



MainActivity,java file code



package com.example.ab.quiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
EditText ed1;
TextView tv1,tv2,tv3;
RadioButton a,b,c,d;
Button bt;
RadioGroup rg;
int q,s;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.name);
tv1=(TextView)findViewById(R.id.ques);
tv2=(TextView)findViewById(R.id.response);
tv3=(TextView)findViewById(R.id.score);
rg=(RadioGroup)findViewById(R.id.optionGroup);
a=(RadioButton)findViewById(R.id.option1);
b=(RadioButton)findViewById(R.id.option2);
c=(RadioButton)findViewById(R.id.option3);
d=(RadioButton)findViewById(R.id.option4);
bt=(Button)findViewById(R.id.next);
q=0;
s=0;

}
public void quiz(View v){
switch (q){
case 0:
{
rg.setVisibility(View.VISIBLE);
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
tv2.setText("");
tv3.setText("");
a.setEnabled(true);
b.setEnabled(true);
c.setEnabled(true);
d.setEnabled(true);
ed1.setEnabled(true);
bt.setText("Next");
s=0;

tv1.setText("1.When did India its independece?");
a.setText("1847");
b.setText("1947");
c.setText("1850");
d.setText("1950");
q=1;
break;
}
case 1:
{
ed1.setEnabled(false);
tv1.setText("2.Who is India's father of nation?");
a.setText("Mahatma Gandhi");
b.setText("Lal Bahadur Shashtri");
c.setText("Jawaharlal Nehru");
d.setText("Subhash Chandra Bose");

if (b.isChecked()){
tv2.setText("Right Answer");
s=s+10;
}
else
{
tv2.setText("Wrong Answer B was Right Answer");
}
q=2;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 2:
{
tv1.setText("3.Who was the first lady CM of Uttar Pradesh?");
a.setText("Pratibha Singh Patil");
b.setText("Indira Gandhi");
c.setText("Sucheta Kriplani");
d.setText("Mayawati");
if (a.isChecked()){
s=s+10;
tv2.setText("Right Answer");

}
else
{
tv2.setText("Wrong Answer A was Right Answer");
}
q=3;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 3:
{

tv1.setText("4.Who was first Indian lady to go in space?");
a.setText("Mayawati");
b.setText("Kalpana Chawla");
c.setText("Kiran Bedi");
d.setText("Sunita Williams");
if (c.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer C was Right Answer");
}
q=4;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 4:
{
tv1.setText("5.Who designed India's national flag?");
a.setText("Rahul Gandhi");
b.setText("Bankim Chandra Chatterjee");
c.setText("Ishwar Chandra Vidyasagar");
d.setText("Pingali Venkayya");
if (b.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer B was Right Answer");
}
q=5;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
bt.setText("Finish");
break;
}
case 5:
{
a.setEnabled(false);
b.setEnabled(false);
c.setEnabled(false);
d.setEnabled(false);
if (d.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer D was Right Answer");
}
tv3.setText(ed1.getText()+"'s final score is "+s);
bt.setText("Restart");
q=0;
break;
}
}
}
}


and...



android_main.xml file code



<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
tools:context="com.example.ab.quiz.MainActivity"
android:background="#22FF00FF">

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="Enter your name please"
android:inputType="textPersonName"
android:textSize="20sp" />

<TextView
android:id="@+id/ques"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="21dp"
android:text=""
android:textSize="20sp"
android:textStyle="bold" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_below="@+id/ques"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/optionGroup"
android:visibility="invisible">

<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ques"
android:layout_marginLeft="50dp"
android:text="" />

<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/option1"
android:layout_marginLeft="200dp"
android:text="" />

<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/option1"
android:layout_alignLeft="@+id/option1"
android:layout_marginLeft="50dp"
android:text="" />

<RadioButton
android:id="@+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/option2"
android:layout_alignLeft="@+id/option2"
android:layout_marginLeft="200dp"
android:text="" />
</RadioGroup>

<TextView
android:id="@+id/response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_below="@+id/optionGroup"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp" />

<Button
android:id="@+id/next"
android:onClick="quiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Start"
android:layout_below="@+id/response"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="33dp"
android:layout_below="@+id/next"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>









share|improve this question

























  • SO is a terrible tutorial site. Your best bet is to work on that tutorial or find another one and come back when you have a specific question.

    – jdv
    Nov 13 '18 at 15:20











  • I have updated my question... Can anyone help?

    – Alonso Tan
    Nov 23 '18 at 8:14














0












0








0








I have come across some tutorials using switch statements to make quiz app by using radio buttons, but I intend to use buttons but not radio buttons to do this... Can I? I want the user to be able to play the quiz app by clicking buttons but not radio buttons. Please guide me through!! Thanks in advance!



The tutorials I mentioned is as below:



MainActivity,java file code



package com.example.ab.quiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
EditText ed1;
TextView tv1,tv2,tv3;
RadioButton a,b,c,d;
Button bt;
RadioGroup rg;
int q,s;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.name);
tv1=(TextView)findViewById(R.id.ques);
tv2=(TextView)findViewById(R.id.response);
tv3=(TextView)findViewById(R.id.score);
rg=(RadioGroup)findViewById(R.id.optionGroup);
a=(RadioButton)findViewById(R.id.option1);
b=(RadioButton)findViewById(R.id.option2);
c=(RadioButton)findViewById(R.id.option3);
d=(RadioButton)findViewById(R.id.option4);
bt=(Button)findViewById(R.id.next);
q=0;
s=0;

}
public void quiz(View v){
switch (q){
case 0:
{
rg.setVisibility(View.VISIBLE);
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
tv2.setText("");
tv3.setText("");
a.setEnabled(true);
b.setEnabled(true);
c.setEnabled(true);
d.setEnabled(true);
ed1.setEnabled(true);
bt.setText("Next");
s=0;

tv1.setText("1.When did India its independece?");
a.setText("1847");
b.setText("1947");
c.setText("1850");
d.setText("1950");
q=1;
break;
}
case 1:
{
ed1.setEnabled(false);
tv1.setText("2.Who is India's father of nation?");
a.setText("Mahatma Gandhi");
b.setText("Lal Bahadur Shashtri");
c.setText("Jawaharlal Nehru");
d.setText("Subhash Chandra Bose");

if (b.isChecked()){
tv2.setText("Right Answer");
s=s+10;
}
else
{
tv2.setText("Wrong Answer B was Right Answer");
}
q=2;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 2:
{
tv1.setText("3.Who was the first lady CM of Uttar Pradesh?");
a.setText("Pratibha Singh Patil");
b.setText("Indira Gandhi");
c.setText("Sucheta Kriplani");
d.setText("Mayawati");
if (a.isChecked()){
s=s+10;
tv2.setText("Right Answer");

}
else
{
tv2.setText("Wrong Answer A was Right Answer");
}
q=3;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 3:
{

tv1.setText("4.Who was first Indian lady to go in space?");
a.setText("Mayawati");
b.setText("Kalpana Chawla");
c.setText("Kiran Bedi");
d.setText("Sunita Williams");
if (c.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer C was Right Answer");
}
q=4;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 4:
{
tv1.setText("5.Who designed India's national flag?");
a.setText("Rahul Gandhi");
b.setText("Bankim Chandra Chatterjee");
c.setText("Ishwar Chandra Vidyasagar");
d.setText("Pingali Venkayya");
if (b.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer B was Right Answer");
}
q=5;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
bt.setText("Finish");
break;
}
case 5:
{
a.setEnabled(false);
b.setEnabled(false);
c.setEnabled(false);
d.setEnabled(false);
if (d.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer D was Right Answer");
}
tv3.setText(ed1.getText()+"'s final score is "+s);
bt.setText("Restart");
q=0;
break;
}
}
}
}


and...



android_main.xml file code



<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
tools:context="com.example.ab.quiz.MainActivity"
android:background="#22FF00FF">

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="Enter your name please"
android:inputType="textPersonName"
android:textSize="20sp" />

<TextView
android:id="@+id/ques"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="21dp"
android:text=""
android:textSize="20sp"
android:textStyle="bold" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_below="@+id/ques"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/optionGroup"
android:visibility="invisible">

<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ques"
android:layout_marginLeft="50dp"
android:text="" />

<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/option1"
android:layout_marginLeft="200dp"
android:text="" />

<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/option1"
android:layout_alignLeft="@+id/option1"
android:layout_marginLeft="50dp"
android:text="" />

<RadioButton
android:id="@+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/option2"
android:layout_alignLeft="@+id/option2"
android:layout_marginLeft="200dp"
android:text="" />
</RadioGroup>

<TextView
android:id="@+id/response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_below="@+id/optionGroup"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp" />

<Button
android:id="@+id/next"
android:onClick="quiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Start"
android:layout_below="@+id/response"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="33dp"
android:layout_below="@+id/next"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>









share|improve this question
















I have come across some tutorials using switch statements to make quiz app by using radio buttons, but I intend to use buttons but not radio buttons to do this... Can I? I want the user to be able to play the quiz app by clicking buttons but not radio buttons. Please guide me through!! Thanks in advance!



The tutorials I mentioned is as below:



MainActivity,java file code



package com.example.ab.quiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
EditText ed1;
TextView tv1,tv2,tv3;
RadioButton a,b,c,d;
Button bt;
RadioGroup rg;
int q,s;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.name);
tv1=(TextView)findViewById(R.id.ques);
tv2=(TextView)findViewById(R.id.response);
tv3=(TextView)findViewById(R.id.score);
rg=(RadioGroup)findViewById(R.id.optionGroup);
a=(RadioButton)findViewById(R.id.option1);
b=(RadioButton)findViewById(R.id.option2);
c=(RadioButton)findViewById(R.id.option3);
d=(RadioButton)findViewById(R.id.option4);
bt=(Button)findViewById(R.id.next);
q=0;
s=0;

}
public void quiz(View v){
switch (q){
case 0:
{
rg.setVisibility(View.VISIBLE);
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
tv2.setText("");
tv3.setText("");
a.setEnabled(true);
b.setEnabled(true);
c.setEnabled(true);
d.setEnabled(true);
ed1.setEnabled(true);
bt.setText("Next");
s=0;

tv1.setText("1.When did India its independece?");
a.setText("1847");
b.setText("1947");
c.setText("1850");
d.setText("1950");
q=1;
break;
}
case 1:
{
ed1.setEnabled(false);
tv1.setText("2.Who is India's father of nation?");
a.setText("Mahatma Gandhi");
b.setText("Lal Bahadur Shashtri");
c.setText("Jawaharlal Nehru");
d.setText("Subhash Chandra Bose");

if (b.isChecked()){
tv2.setText("Right Answer");
s=s+10;
}
else
{
tv2.setText("Wrong Answer B was Right Answer");
}
q=2;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 2:
{
tv1.setText("3.Who was the first lady CM of Uttar Pradesh?");
a.setText("Pratibha Singh Patil");
b.setText("Indira Gandhi");
c.setText("Sucheta Kriplani");
d.setText("Mayawati");
if (a.isChecked()){
s=s+10;
tv2.setText("Right Answer");

}
else
{
tv2.setText("Wrong Answer A was Right Answer");
}
q=3;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 3:
{

tv1.setText("4.Who was first Indian lady to go in space?");
a.setText("Mayawati");
b.setText("Kalpana Chawla");
c.setText("Kiran Bedi");
d.setText("Sunita Williams");
if (c.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer C was Right Answer");
}
q=4;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
break;
}
case 4:
{
tv1.setText("5.Who designed India's national flag?");
a.setText("Rahul Gandhi");
b.setText("Bankim Chandra Chatterjee");
c.setText("Ishwar Chandra Vidyasagar");
d.setText("Pingali Venkayya");
if (b.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer B was Right Answer");
}
q=5;
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
bt.setText("Finish");
break;
}
case 5:
{
a.setEnabled(false);
b.setEnabled(false);
c.setEnabled(false);
d.setEnabled(false);
if (d.isChecked()){
s=s+10;
tv2.setText("Right Answer");
}
else
{
tv2.setText("Wrong Answer D was Right Answer");
}
tv3.setText(ed1.getText()+"'s final score is "+s);
bt.setText("Restart");
q=0;
break;
}
}
}
}


and...



android_main.xml file code



<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
tools:context="com.example.ab.quiz.MainActivity"
android:background="#22FF00FF">

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="Enter your name please"
android:inputType="textPersonName"
android:textSize="20sp" />

<TextView
android:id="@+id/ques"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="21dp"
android:text=""
android:textSize="20sp"
android:textStyle="bold" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_below="@+id/ques"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/optionGroup"
android:visibility="invisible">

<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ques"
android:layout_marginLeft="50dp"
android:text="" />

<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/option1"
android:layout_marginLeft="200dp"
android:text="" />

<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/option1"
android:layout_alignLeft="@+id/option1"
android:layout_marginLeft="50dp"
android:text="" />

<RadioButton
android:id="@+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/option2"
android:layout_alignLeft="@+id/option2"
android:layout_marginLeft="200dp"
android:text="" />
</RadioGroup>

<TextView
android:id="@+id/response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_below="@+id/optionGroup"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp" />

<Button
android:id="@+id/next"
android:onClick="quiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Start"
android:layout_below="@+id/response"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="33dp"
android:layout_below="@+id/next"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>






android-studio switch-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 10:11







Alonso Tan

















asked Nov 13 '18 at 15:12









Alonso TanAlonso Tan

116




116













  • SO is a terrible tutorial site. Your best bet is to work on that tutorial or find another one and come back when you have a specific question.

    – jdv
    Nov 13 '18 at 15:20











  • I have updated my question... Can anyone help?

    – Alonso Tan
    Nov 23 '18 at 8:14



















  • SO is a terrible tutorial site. Your best bet is to work on that tutorial or find another one and come back when you have a specific question.

    – jdv
    Nov 13 '18 at 15:20











  • I have updated my question... Can anyone help?

    – Alonso Tan
    Nov 23 '18 at 8:14

















SO is a terrible tutorial site. Your best bet is to work on that tutorial or find another one and come back when you have a specific question.

– jdv
Nov 13 '18 at 15:20





SO is a terrible tutorial site. Your best bet is to work on that tutorial or find another one and come back when you have a specific question.

– jdv
Nov 13 '18 at 15:20













I have updated my question... Can anyone help?

– Alonso Tan
Nov 23 '18 at 8:14





I have updated my question... Can anyone help?

– Alonso Tan
Nov 23 '18 at 8:14












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284022%2fcan-we-use-switch-statement-in-making-a-quiz-app-in-android-studio-by-using-butt%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284022%2fcan-we-use-switch-statement-in-making-a-quiz-app-in-android-studio-by-using-butt%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine