Thursday, July 27, 2017

Multiple quiz application android




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.edupointbd.amirul.quizeappsdemo.MainActivity">
<TextView
android:id="@+id/tvQuestion"
android:textSize="22sp"
android:layout_gravity="center_horizontal"
android:text="question- who are you?"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/ch1"
android:textColor="#fff"
android:layout_marginTop="22dp"
android:background="@color/colorPrimary"
android:text="Question 1 "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/ch2"
android:textColor="#fff"
android:layout_marginTop="22dp"
android:background="@color/colorPrimary"
android:text="Question 2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/ch3"
android:textColor="#fff"
android:layout_marginTop="22dp"
android:background="@color/colorPrimary"
android:text="Question 3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/ch4"
android:textColor="#fff"
android:layout_marginTop="22dp"
android:background="@color/colorPrimary"
android:text="Question 4 "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/score"
android:textSize="22sp"
android:layout_marginTop="22dp"
android:layout_gravity="center_horizontal"
android:text="Score 3/5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>



package com.edupointbd.amirul.quizeappsdemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private QuestionBank myQuestionBank = new QuestionBank();
private TextView mScoreView;
private TextView mQuestionView;
private TextView mChoice1,mChoice2,mChoice3,mChoice4;
private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScoreView = (TextView)findViewById(R.id.score);
mQuestionView = (TextView)findViewById(R.id.tvQuestion);
mChoice1 = (TextView)findViewById(R.id.ch1);
mChoice2 = (TextView)findViewById(R.id.ch2);
mChoice3 = (TextView)findViewById(R.id.ch3);
mChoice4 = (TextView)findViewById(R.id.ch4);
updateQuestion();
updateScore(mQuestionNumber);
}
private void updateQuestion() {
if(mQuestionNumber < myQuestionBank.getLength()){
mQuestionView.setText(myQuestionBank.getQuestion(mQuestionNumber));
mChoice1.setText(myQuestionBank.getChoice(mQuestionNumber,1));
mChoice2.setText(myQuestionBank.getChoice(mQuestionNumber,2));
mChoice3.setText(myQuestionBank.getChoice(mQuestionNumber,3));
mChoice4.setText(myQuestionBank.getChoice(mQuestionNumber,4));
mChoice1.setOnClickListener(this);
mChoice2.setOnClickListener(this);
mChoice3.setOnClickListener(this);
mChoice4.setOnClickListener(this);
mAnswer = myQuestionBank.getCorrectAnswer(mQuestionNumber);
mQuestionNumber++;
}
else {
Toast.makeText(getApplicationContext(),"the last was indeed",Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,HighScoreActitvity.class);
intent.putExtra("highscore",mScore);
startActivity(intent);
}
}
private void updateScore(int inPoint) {
mScoreView.setText("question no: "+mQuestionNumber+" Score "+mScore+"/"+myQuestionBank.getLength());
}
public void onClick(View view){
Button answer = (Button)view;
if(answer.getText() == mAnswer){
mScore =mScore+1;
Toast.makeText(getApplicationContext(),"Correct !!",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(),"wrong answer..",Toast.LENGTH_LONG).show();
}
updateScore(mQuestionNumber);
updateQuestion();
}
}


package com.edupointbd.amirul.quizeappsdemo;
/**
* Created by amirul on 26-Jul-17.
* www.edupointbd.com
*/
public class QuestionBank {
//array of question
private String textQuestion[] = {
"The data type created by the data abstraction process is called",
"Encapsulation is ",
"Which of the following language support garbage collection ?",
"Which is not the feature of structured programming? ",
"Procedural programming language is "
};
// multiple choise for each question
private String multipleChoice[][]= {
{"class", "structure", "abstract data type", "User-defined data type"},
{"dynamic binding", "a mechanism to associate the code and data", "data abstraction", "none of these"},
{"Java", "C++", "C", "Small Talk"},
{"Support for modular programming", "User-defined data types", "Emphasis on algorithm", "Data abstraction"},
{"COBOL", "BASICS", "C++", "PASCAL"},
};
// arraqy of correct question answer
private String mCorrectAnswer[] = {"abstract data type", "a mechanism to associate the code and data", "Java", "Data abstraction","COBOL"};
//method for get number of question
public int getLength(){
return textQuestion.length;
}
public String getQuestion(int a){
String question = textQuestion[a];
return question;
}
//return single multiple choice item int list 1 2 3 4 as an argument
public String getChoice(int index, int num){
String choice = multipleChoice[index][num-1];
return choice;
}
public String getCorrectAnswer(int a){
String answer = mCorrectAnswer[a];
return answer;
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_high_score_actitvity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="50dp"
android:background="@color/colorAccent"
android:gravity="center"
tools:context="com.edupointbd.amirul.quizeappsdemo.HighScoreActitvity">
<TextView
android:id="@+id/scoreid"
android:textSize="35sp"
android:textColor="@color/colorPrimary"
android:text="score"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/highScore"
android:text="hight score"
android:textSize="35sp"
android:textColor="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


package com.edupointbd.amirul.quizeappsdemo;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class HighScoreActitvity extends AppCompatActivity {
int score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_score_actitvity);
TextView Tvscore = (TextView)findViewById(R.id.scoreid);
TextView high = (TextView)findViewById(R.id.highScore);
Bundle b = getIntent().getExtras();
if(b !=null){
score =b.getInt("highscore");
}
Tvscore.setText("your score: "+score);
SharedPreferences mySpre = getPreferences(MODE_PRIVATE);
int hightScore = mySpre.getInt("HightScore",score);
Log.d("hightScore", String.valueOf(hightScore));
if(hightScore >= score){
high.setText("High Score: "+hightScore);
}else {
high.setText("Score"+score);
SharedPreferences.Editor editor = mySpre.edit();
editor.putInt("HightScore",score);
Log.d("hightScore 3", String.valueOf(hightScore));
editor.commit();
}
}
}