STEPS : FIRST SET ALL XML FILES FOR ALL
COLOUR.XML , DIMESION .XML ,STRING.XML , STYLE.XML
MAIN ACTIVITY .JAVA
package com.example.viraj.quiz;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import static android.widget.Toast.makeText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//ToDo Declear constant Here 100/13 =8 approx or u can declear below mquestion bank also
//final int PROGRESS_BAR_INCREMENT=8;
//ToDo Declear membar variable Here
// Member variables accessible in all the methods of the MainActivity:
Button mTrueButton, mFalseButton;
TextView mQuestionTextView;
TextView mScoreTextView;
ProgressBar mProgressBar;
int mIndex;
int mScore;
int mQuestion;
Toast mToastMessage;
//ToDo Uncommnnet to create question bank
// Create question bank using the TrueFalse class for each item in the array
@NonNull
private TrueFalse[] mQuestionBank = new TrueFalse[]{
new TrueFalse(R.string.question_1_1, true),
new TrueFalse(R.string.question_2, true),
new TrueFalse(R.string.question_3, true),
new TrueFalse(R.string.question_4, true),
new TrueFalse(R.string.question_5, true),
new TrueFalse(R.string.question_6, false),
new TrueFalse(R.string.question_7, true),
new TrueFalse(R.string.question_8, false),
new TrueFalse(R.string.question_9, true),
new TrueFalse(R.string.question_10, true),
new TrueFalse(R.string.question_11, false),
new TrueFalse(R.string.question_12, false),
new TrueFalse(R.string.question_13, true)
};
//int[] myIndArray=new int[] {2,4,6};
// math ceil ek function he jo conversion ka kamm kar raha hee double ko singlie me
// Declaring constants here. Rather than a fixed number, choosing to make it a function
// of the length of the question bank (the number of questions)
final int PROGRESS_BAR_INCREMENT = (int) Math.ceil(100.0 / mQuestionBank.length);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTrueButton = findViewById(R.id.true_button);
mFalseButton = findViewById(R.id.false_button);
mQuestionTextView = findViewById(R.id.question_text_view);
mScoreTextView = findViewById(R.id.score);
mProgressBar = findViewById(R.id.progress_bar);
//int element= myIndArray[1];
//type is truefalse ,,, name is first question
//TrueFalse firstQuestion=mQuestionBank[mIndex];
//mQuestion =firstQuestion.getQuestionID();
mQuestion = mQuestionBank[mIndex].getQuestionID();
mQuestionTextView.setText(mQuestion);
mTrueButton.setOnClickListener(this);
mFalseButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.true_button:
// Toast.makeText(getApplicationContext(), "TRUE", Toast.LENGTH_SHORT).show();
updateQuestion();
checkAnswer(true);
break;
case R.id.false_button:
// Toast.makeText(getApplicationContext(), "FALSE", Toast.LENGTH_SHORT).show();
updateQuestion();
checkAnswer(false);
break;
}
}
private void updateQuestion() {
// This takes the modulus. Not a division. ye bhi use kar sakte he
mIndex=(mIndex+1)%mQuestionBank.length;
mQuestion=mQuestionBank[mIndex].getQuestionID();
mQuestionTextView.setText(mQuestion);
//Present an alert dialog if we reach the end.
if(mIndex == 0) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Game Over");
alert.setCancelable(false);
alert.setMessage("You scored " + mScore + " points!");
alert.setPositiveButton("Close Application", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
}
mProgressBar.incrementProgressBy(PROGRESS_BAR_INCREMENT);
mScoreTextView.setText("Score " + mScore + "/" + mQuestionBank.length);
}
private void checkAnswer(boolean userSelection) {
boolean correctAnswer = mQuestionBank[mIndex].isAnswer();
//
// Can cancel the Toast message if there is one on screen and a new answer
// has been submitted.
if (mToastMessage != null) {
mToastMessage.cancel();
}
if(userSelection == correctAnswer) {
mToastMessage = makeText(getApplicationContext(), R.string.correct_toast, Toast.LENGTH_SHORT);
mScore = mScore + 1;
} else {
mToastMessage = Toast.makeText(getApplicationContext(), R.string.incorrect_toast, Toast.LENGTH_LONG);
}
mToastMessage.show();
}
// This callback is received when the screen is rotated so we can save the 'state'
// of the app in a 'bundle'.
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt("ScoreKey", mScore);
outState.putInt("IndexKey", mIndex);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TRUEFALSE.JAVA
package com.example.viraj.quiz;
/**
* Created by viraj on 1/11/2018.
*/
// This is the model class that represents a single quiz question.
public class TrueFalse {
private int mQuestionID;
private boolean mAnswer;
//getter and setter method auto generate
// This is the constructor that will be called when a new quiz question is created.
public TrueFalse(int questionResourceID, boolean trueOrFalse) {
mQuestionID = questionResourceID;
mAnswer = trueOrFalse;
}
//getter and setter auto generate
// This method gives us access to info stored in the (private) question id.
public int getQuestionID() {
return mQuestionID;
}
//getter and setter auto generate
// This method gives us access to info stored in the (private) mAnswer.
public boolean isAnswer() {
return mAnswer;
}
// Not actually using the setters at the moment. Users are not creating questions.
// public void setQuestionID(int questionID) {
// mQuestionID = questionID;
// }
// public void setAnswer(boolean answer) {
// mAnswer = answer;
// }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
XML FILE
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.viraj.quiz.MainActivity"
android:background="@color/colorPrimaryDark"
android:orientation="vertical"
>
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:gravity="center"
android:padding="20dp"
android:text="@string/question_1_1"
android:textColor="@color/colourText"
android:textStyle="bold"
android:textSize="30sp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@id/score"
android:gravity="center"
android:padding="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
style="@style/buttonStyle"
android:text="@string/true_button"
android:background="@color/colourTrueButton"/>
<Button
android:id="@+id/false_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
style="@style/buttonStyle"
android:text="@string/false_button"
android:background="@color/colourFalseButton"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
style="@android:style/Widget.ProgressBar.Horizontal"
android:indeterminate="false" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/progress_bar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="@color/colourText"
android:textStyle="bold"
android:textSize="25sp"
android:text="@string/initial_score"
android:padding="10dp"/>
</RelativeLayout>
COLOUR.XML , DIMESION .XML ,STRING.XML , STYLE.XML
package com.example.viraj.quiz;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import static android.widget.Toast.makeText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//ToDo Declear constant Here 100/13 =8 approx or u can declear below mquestion bank also
//final int PROGRESS_BAR_INCREMENT=8;
//ToDo Declear membar variable Here
// Member variables accessible in all the methods of the MainActivity:
Button mTrueButton, mFalseButton;
TextView mQuestionTextView;
TextView mScoreTextView;
ProgressBar mProgressBar;
int mIndex;
int mScore;
int mQuestion;
Toast mToastMessage;
//ToDo Uncommnnet to create question bank
// Create question bank using the TrueFalse class for each item in the array
@NonNull
private TrueFalse[] mQuestionBank = new TrueFalse[]{
new TrueFalse(R.string.question_1_1, true),
new TrueFalse(R.string.question_2, true),
new TrueFalse(R.string.question_3, true),
new TrueFalse(R.string.question_4, true),
new TrueFalse(R.string.question_5, true),
new TrueFalse(R.string.question_6, false),
new TrueFalse(R.string.question_7, true),
new TrueFalse(R.string.question_8, false),
new TrueFalse(R.string.question_9, true),
new TrueFalse(R.string.question_10, true),
new TrueFalse(R.string.question_11, false),
new TrueFalse(R.string.question_12, false),
new TrueFalse(R.string.question_13, true)
};
//int[] myIndArray=new int[] {2,4,6};
// math ceil ek function he jo conversion ka kamm kar raha hee double ko singlie me
// Declaring constants here. Rather than a fixed number, choosing to make it a function
// of the length of the question bank (the number of questions)
final int PROGRESS_BAR_INCREMENT = (int) Math.ceil(100.0 / mQuestionBank.length);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTrueButton = findViewById(R.id.true_button);
mFalseButton = findViewById(R.id.false_button);
mQuestionTextView = findViewById(R.id.question_text_view);
mScoreTextView = findViewById(R.id.score);
mProgressBar = findViewById(R.id.progress_bar);
//int element= myIndArray[1];
//type is truefalse ,,, name is first question
//TrueFalse firstQuestion=mQuestionBank[mIndex];
//mQuestion =firstQuestion.getQuestionID();
mQuestion = mQuestionBank[mIndex].getQuestionID();
mQuestionTextView.setText(mQuestion);
mTrueButton.setOnClickListener(this);
mFalseButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.true_button:
// Toast.makeText(getApplicationContext(), "TRUE", Toast.LENGTH_SHORT).show();
updateQuestion();
checkAnswer(true);
break;
case R.id.false_button:
// Toast.makeText(getApplicationContext(), "FALSE", Toast.LENGTH_SHORT).show();
updateQuestion();
checkAnswer(false);
break;
}
}
private void updateQuestion() {
// This takes the modulus. Not a division. ye bhi use kar sakte he
mIndex=(mIndex+1)%mQuestionBank.length;
mQuestion=mQuestionBank[mIndex].getQuestionID();
mQuestionTextView.setText(mQuestion);
//Present an alert dialog if we reach the end.
if(mIndex == 0) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Game Over");
alert.setCancelable(false);
alert.setMessage("You scored " + mScore + " points!");
alert.setPositiveButton("Close Application", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
}
mProgressBar.incrementProgressBy(PROGRESS_BAR_INCREMENT);
mScoreTextView.setText("Score " + mScore + "/" + mQuestionBank.length);
}
private void checkAnswer(boolean userSelection) {
boolean correctAnswer = mQuestionBank[mIndex].isAnswer();
//
// Can cancel the Toast message if there is one on screen and a new answer
// has been submitted.
if (mToastMessage != null) {
mToastMessage.cancel();
}
if(userSelection == correctAnswer) {
mToastMessage = makeText(getApplicationContext(), R.string.correct_toast, Toast.LENGTH_SHORT);
mScore = mScore + 1;
} else {
mToastMessage = Toast.makeText(getApplicationContext(), R.string.incorrect_toast, Toast.LENGTH_LONG);
}
mToastMessage.show();
}
// This callback is received when the screen is rotated so we can save the 'state'
// of the app in a 'bundle'.
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt("ScoreKey", mScore);
outState.putInt("IndexKey", mIndex);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TRUEFALSE.JAVA
package com.example.viraj.quiz;
/**
* Created by viraj on 1/11/2018.
*/
// This is the model class that represents a single quiz question.
public class TrueFalse {
private int mQuestionID;
private boolean mAnswer;
//getter and setter method auto generate
// This is the constructor that will be called when a new quiz question is created.
public TrueFalse(int questionResourceID, boolean trueOrFalse) {
mQuestionID = questionResourceID;
mAnswer = trueOrFalse;
}
//getter and setter auto generate
// This method gives us access to info stored in the (private) question id.
public int getQuestionID() {
return mQuestionID;
}
//getter and setter auto generate
// This method gives us access to info stored in the (private) mAnswer.
public boolean isAnswer() {
return mAnswer;
}
// Not actually using the setters at the moment. Users are not creating questions.
// public void setQuestionID(int questionID) {
// mQuestionID = questionID;
// }
// public void setAnswer(boolean answer) {
// mAnswer = answer;
// }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
XML FILE
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.viraj.quiz.MainActivity"
android:background="@color/colorPrimaryDark"
android:orientation="vertical"
>
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:gravity="center"
android:padding="20dp"
android:text="@string/question_1_1"
android:textColor="@color/colourText"
android:textStyle="bold"
android:textSize="30sp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@id/score"
android:gravity="center"
android:padding="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
style="@style/buttonStyle"
android:text="@string/true_button"
android:background="@color/colourTrueButton"/>
<Button
android:id="@+id/false_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
style="@style/buttonStyle"
android:text="@string/false_button"
android:background="@color/colourFalseButton"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
style="@android:style/Widget.ProgressBar.Horizontal"
android:indeterminate="false" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/progress_bar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="@color/colourText"
android:textStyle="bold"
android:textSize="25sp"
android:text="@string/initial_score"
android:padding="10dp"/>
</RelativeLayout>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
COLOUR.XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colourText">#F5F5F5</color>
<color name="colourTrueButton">#CDDC39</color>
<color name="colourFalseButton">#2196F3</color>
</resources>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
STYLE.XML
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Both buttons share a similar style attibutes so we can put them here -->
<style name="buttonStyle" parent="Widget.AppCompat.Button.Colored">
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/colourText</item>
<item name="android:layout_margin">@dimen/button_spacing_margin</item>
</style>
</resources>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DIMESSION
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="button_spacing_margin">4dp</dimen>
</resources>
0 comments:
Post a Comment