Android Simple Quiz Application Example.

First Create New Android Project For Quiz Sample Demo :


Step-1 : Main Activity :

package com.jaydarshanproductions.quiz;

import java.util.ArrayList;

import com.example.option.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity implements OnCheckedChangeListener {

ArrayList<String> que = new ArrayList<String>();
ArrayList<String> opt1 = new ArrayList<String>();
ArrayList<String> opt2 = new ArrayList<String>();
ArrayList<String> opt3 = new ArrayList<String>();
ArrayList<String> opt4 = new ArrayList<String>();
ArrayList<String> ans = new ArrayList<String>();
ArrayList<String> uans = new ArrayList<String>();

int START = 0;
Button next;
TextView tv;
RadioButton op1, op2, op3, op4;
RadioGroup rg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

inilize();
/****************** SET YOU QUESTION BELOW ************************************/

que.add("Q1. Who is the god of Cricket");
opt1.add("SEHWAG");
opt2.add("DHONI");
opt3.add("SACHIN");
opt4.add("DRAVID");
ans.add("SACHIN");

que.add("Q2. Capital of Gujarat");
opt1.add("AHMADABAD");
opt2.add("SURAT");
opt3.add("VADODARA");
opt4.add("GANDHINAGAR");
ans.add("GANDHINAGAR");

que.add("Q3. ANDROID Is a Language ?");
opt1.add("FREEWARE");
opt2.add("OPENSOURCE");
opt3.add("PAID");
opt4.add("COMMORCIAL");
ans.add("OPENSOURCE");

que.add("Q4. Who is the CM of Gujarat");
opt1.add("NARENDRA MODI");
opt2.add("VAJUBHAI VALA");
opt3.add("ANANDIBEN PATEL");
opt4.add("RAJNATH SHINH");
ans.add("NARENDRA MODI");

que.add("Q5. 20 Devide By 4 ?");
opt1.add("2");
opt2.add("12");
opt3.add("8");
opt4.add("5");
ans.add("5");

/****************** SET YOU QUESTION ABOVE/

tv.setText(que.get(START));
op1.setText(opt1.get(START));
op2.setText(opt2.get(START));
op3.setText(opt3.get(START));
op4.setText(opt4.get(START));

next.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
START++;

if (START >= que.size()) {
Intent intent = new Intent(MainActivity.this, Res.class);
intent.putStringArrayListExtra("ans", ans);
intent.putStringArrayListExtra("uans", uans);
startActivity(intent);
} else {
tv.setText(que.get(START));
op1.setText(opt1.get(START));
op2.setText(opt2.get(START));
op3.setText(opt3.get(START));
op4.setText(opt4.get(START));

op1.setChecked(false);
op2.setChecked(false);
op3.setChecked(false);
op4.setChecked(false);
}
}
});

op1.setOnCheckedChangeListener(this);
op2.setOnCheckedChangeListener(this);
op3.setOnCheckedChangeListener(this);
op4.setOnCheckedChangeListener(this);

}

private void inilize() {
// TODO Auto-generated method stub
next = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textQue);
op1 = (RadioButton) findViewById(R.id.radio0);
op2 = (RadioButton) findViewById(R.id.radio1);
op3 = (RadioButton) findViewById(R.id.radio2);
op4 = (RadioButton) findViewById(R.id.radio3);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
}

@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
// TODO Auto-generated method stub

if (checked) {
switch (button.getId()) {

case R.id.radio0:
uans.add(START, op1.getText().toString());
break;
case R.id.radio1:
uans.add(START, op2.getText().toString());
break;
case R.id.radio2:
uans.add(START, op3.getText().toString());
break;
case R.id.radio3:
uans.add(START, op4.getText().toString());
break;
}
}
}

}

Step-2 : Resource Activity :

package com.jaydarshanproductions.quiz;

import java.util.ArrayList;

import com.example.option.R;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Resource extends Activity {
ArrayList<String> ans = new ArrayList<String>();
ArrayList<String> uans = new ArrayList<String>();
ListView lv;
TextView Total, Right, Wrong, Percantage, Grade;
int TOTAL = 0, RIGHT_ANSWER = 0, WRONG_ANSWER = 0;
String GRADE = null;
float PERCENTAGE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.res);
lv = (ListView) findViewById(R.id.listans);
Total = (TextView) findViewById(R.id.textTQU);
Right = (TextView) findViewById(R.id.textRA);
Wrong = (TextView) findViewById(R.id.textWA);
Percantage = (TextView) findViewById(R.id.textPER);
Grade = (TextView) findViewById(R.id.textGRADE);

Bundle data = getIntent().getExtras();
ans = data.getStringArrayList("ans");
uans = data.getStringArrayList("uans");

Myadapter adapter = new Myadapter(this);

adapter.setAns(ans);
adapter.setUans(uans);

lv.setAdapter(adapter);

this.TOTAL = this.ans.size();
for(int i=0;i<ans.size();i++){
if(uans.get(i).equalsIgnoreCase(ans.get(i))){
RIGHT_ANSWER++;
}else {
WRONG_ANSWER++;
}
}

this.PERCENTAGE = (RIGHT_ANSWER*100)/TOTAL;

if (PERCENTAGE >= 70) {
GRADE = "FIRST CLASS";
} else if (PERCENTAGE >= 60) {
GRADE = "SECOND CLASS";
} else if (PERCENTAGE >= 50) {
GRADE = "THIRD CLASS";
} else if (PERCENTAGE >= 35) {
GRADE = "PASS";
} else {
GRADE = "FAIL";
}
Total.setText(""+TOTAL);
Right.setText(""+RIGHT_ANSWER);
Wrong.setText(""+WRONG_ANSWER);
Percantage.setText(""+PERCENTAGE);
Grade.setText(""+GRADE);
}
}

class Myadapter extends BaseAdapter {

Activity con;
ArrayList<String> uans = new ArrayList<String>();
ArrayList<String> ans = new ArrayList<String>();

public Myadapter(Activity res) {
// TODO Auto-generated constructor stub
con = res;
}

void setUans(ArrayList<String> ans) {
this.ans = ans;
}

void setAns(ArrayList<String> unas) {
this.uans = unas;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return uans.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return uans.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

LayoutInflater inflater = con.getLayoutInflater();
convertView = inflater.inflate(R.layout.anssupport, null);

TextView res = (TextView) convertView.findViewById(R.id.textView1);
TextView ans = (TextView) convertView.findViewById(R.id.textView2);
ImageView img = (ImageView) convertView.findViewById(R.id.imageView1);

res.setText(this.uans.get(position));
ans.setText(this.ans.get(position));

if (this.uans.get(position).equalsIgnoreCase(this.ans.get(position))) {
img.setImageResource(R.drawable.apply_f2);
ans.setTextColor(Color.GREEN);
} else {
img.setImageResource(R.drawable.cancel_f2);
ans.setTextColor(Color.RED);
}

return convertView;
}

}


Step-3 : XML Structure 



/This You Can Create Your Own Design/

activity_main.xml  :

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    
     <TextView
        android:id="@+id/textQue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
    
    
     <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup1"
        android:layout_centerHorizontal="true"
        android:paddingTop="20dp"
        android:background="@color/fuchsia"
        android:text="NEXT QUE" />

     <RadioGroup
         android:id="@+id/radioGroup1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/textQue"
         android:paddingTop="10dp"
         android:layout_alignBottom="@+id/button1"
         android:layout_below="@+id/textQue" >

         <RadioButton
             android:id="@+id/radio0"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignRight="@+id/textQue"
             android:layout_below="@+id/textQue"
             android:layout_marginTop="28dp"
             android:checked="false"
             android:text="RadioButton" />

         <RadioButton
             android:id="@+id/radio1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignLeft="@+id/radio0"
             android:layout_below="@+id/radio0"
             android:checked="false"
             android:text="RadioButton" />

         <RadioButton
             android:id="@+id/radio2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignLeft="@+id/radio1"
             android:layout_below="@+id/radio1"
             android:checked="false"
             android:text="RadioButton" />

         <RadioButton
             android:id="@+id/radio3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignLeft="@+id/radio2"
             android:layout_below="@+id/radio2"
             android:checked="false"
             android:text="RadioButton" />
     </RadioGroup>

</RelativeLayout>

Res.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="20dp"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listans"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp" >

    </ListView>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/listans"
        android:layout_centerHorizontal="true"
        android:text="Your Answer"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/textTQ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:text="TOTAL QUESION"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_marginLeft="22dp"
        android:layout_toRightOf="@+id/textTQ"
        android:text="RIGHT ANSWER"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView6"
        android:layout_alignBottom="@+id/textView6"
        android:layout_marginLeft="21dp"
        android:layout_toRightOf="@+id/textView3"
        android:text="WRONG ANSWER"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listans"
        android:layout_marginLeft="26dp"
        android:layout_toRightOf="@+id/textView4"
        android:text="PERCANTAGE"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_toRightOf="@+id/textView5"
        android:layout_marginLeft="15dp"
        android:text="GRADE"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textTQU"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textTQ"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="47dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textRA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textTQU"
        android:layout_alignBottom="@+id/textTQU"
        android:layout_alignLeft="@+id/textView3"
        android:layout_marginLeft="23dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textWA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textRA"
        android:layout_alignBottom="@+id/textRA"
        android:layout_alignLeft="@+id/textView4"
        android:layout_marginLeft="29dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textPER"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textWA"
        android:layout_alignBottom="@+id/textWA"
        android:layout_alignLeft="@+id/textView5"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textGRADE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textPER"
        android:layout_alignBottom="@+id/textPER"
        android:layout_alignLeft="@+id/textView6"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp"
        android:text="Right Answer"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/black" />

</RelativeLayout>

anssupport.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentRelative"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="4sp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="4sp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="16dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

result.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="39dp"
        android:layout_marginTop="27dp"
        android:text="TextView" />

</RelativeLayout>

No comments:

Post a Comment