Skip to content

Instantly share code, notes, and snippets.

@Leonard02
Created February 6, 2018 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leonard02/999ce4be20394196a4d297f2334fc78a to your computer and use it in GitHub Desktop.
Save Leonard02/999ce4be20394196a4d297f2334fc78a to your computer and use it in GitHub Desktop.
QUIZ APP LK
package com.example.android.quizapplk;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitQuiz(View view) {
EditText addNameEditText = (EditText) findViewById(R.id.name_field);
String name =addNameEditText.getText().toString();
EditText addSurnameEditText = (EditText) findViewById(R.id.surname_field);
String surname =addSurnameEditText.getText().toString();
int score = calculateScore(view);
Toast.makeText(this, "Thank you for your answers" , Toast.LENGTH_SHORT).show();
String theMessage = "Thank you for your attending our quiz, " + name +" "+surname;
theMessage += "\nYour Total Score: " + score;
sendEmail(theMessage, score);
return;
}
public int onRadioButtonClickedModel(View view) {
String choosed = "";
int yourScore = 0;
RadioButton radiobuttonChooseModelM5 = (RadioButton) findViewById(R.id.m5_model);
RadioButton radiobuttonChooseModelR8 = (RadioButton) findViewById(R.id.r8_model);
RadioButton radiobuttonChooseModelCLS = (RadioButton) findViewById(R.id.cls_model);
Boolean ischeckedM5 = radiobuttonChooseModelM5.isChecked();
Boolean ischeckedR8 = radiobuttonChooseModelR8.isChecked();
Boolean ischeckedCls = radiobuttonChooseModelCLS.isChecked();
if (ischeckedR8) {
radiobuttonChooseModelM5.setChecked(false);
radiobuttonChooseModelCLS.setChecked(false);
choosed = radiobuttonChooseModelR8.getText().toString();
Log.v("",""+ choosed);
yourScore= yourScore + 10;
}
if (ischeckedM5) {
radiobuttonChooseModelR8.setChecked(false);
radiobuttonChooseModelCLS.setChecked(false);
choosed = radiobuttonChooseModelM5.getText().toString();
Log.v("",""+ choosed);
yourScore = yourScore + 0;
}
if (ischeckedCls) {
radiobuttonChooseModelM5.setChecked(false);
radiobuttonChooseModelR8.setChecked(false);
choosed = radiobuttonChooseModelCLS.getText().toString();
Log.v("",""+ choosed);
yourScore = yourScore + 0;
}
return yourScore;
}
public int onRadioButtonClickedLogo1(View view) {
String choosed = "";
int yourScore = 0;
RadioButton radiobuttonChooseMaclaren = (RadioButton) findViewById(R.id.maclaren_button);
RadioButton radiobuttonChooseBuggati = (RadioButton) findViewById(R.id.bugatti);
RadioButton radiobuttonChooseInfinity = (RadioButton) findViewById(R.id.infinity);
Boolean ischeckedMaclaren = radiobuttonChooseMaclaren.isChecked();
Boolean ischeckedBuggati = radiobuttonChooseBuggati.isChecked();
Boolean ischeckedInfinity = radiobuttonChooseInfinity.isChecked();
if (ischeckedMaclaren) {
radiobuttonChooseBuggati.setChecked(false);
radiobuttonChooseInfinity.setChecked(false);
choosed = radiobuttonChooseMaclaren.getText().toString();
Log.v("",""+ choosed);
yourScore= yourScore + 10;
}
if (ischeckedBuggati) {
radiobuttonChooseMaclaren.setChecked(false);
radiobuttonChooseInfinity.setChecked(false);
choosed = radiobuttonChooseBuggati.getText().toString();
Log.v("",""+ choosed);
yourScore = yourScore + 0;
}
if (ischeckedInfinity) {
radiobuttonChooseMaclaren.setChecked(false);
radiobuttonChooseBuggati.setChecked(false);
choosed = radiobuttonChooseInfinity.getText().toString();
Log.v("",""+ choosed);
yourScore = yourScore + 0;
}
return yourScore;
}
public int onCheckBoxCheckedModel(View view) {
String choosed = "";
int yourScore = 0;
CheckBox toyotaCheckBox = (CheckBox) findViewById(R.id.toyota_checkbox);
CheckBox audiCheckBox = (CheckBox) findViewById(R.id.audi_checkbox);
CheckBox porscheCheckBox = (CheckBox) findViewById(R.id.porsche_checkbox);
CheckBox alfaCheckBox = (CheckBox) findViewById(R.id.alfaromeo_checkbox);
CheckBox bmwCheckBox = (CheckBox) findViewById(R.id.bmw_checkbox);
boolean hasCheckedToyota = toyotaCheckBox.isChecked();
boolean hasCheckedAudi = audiCheckBox.isChecked();
boolean hasCheckedPorsche = porscheCheckBox.isChecked();
boolean hasCheckedAlfa = alfaCheckBox.isChecked();
boolean hasCheckedBmw =bmwCheckBox.isChecked();
if (hasCheckedToyota) {
audiCheckBox.setChecked(false);
porscheCheckBox.setChecked(false);
alfaCheckBox.setChecked(false);
bmwCheckBox.setChecked(false);
choosed = toyotaCheckBox.getText().toString();
Log.v("",""+ choosed);
yourScore= yourScore + 0;
}
if (hasCheckedAudi) {
toyotaCheckBox.setChecked(false);
porscheCheckBox.setChecked(false);
alfaCheckBox.setChecked(false);
bmwCheckBox.setChecked(false);
choosed = audiCheckBox.getText().toString();
Log.v("",""+ choosed);
yourScore= yourScore + 0;
}
if (hasCheckedPorsche) {
toyotaCheckBox.setChecked(false);
audiCheckBox.setChecked(false);
alfaCheckBox.setChecked(false);
bmwCheckBox.setChecked(false);
choosed = porscheCheckBox.getText().toString();
Log.v("",""+ choosed);
yourScore= yourScore + 10;
}
if (hasCheckedAlfa) {
toyotaCheckBox.setChecked(false);
audiCheckBox.setChecked(false);
porscheCheckBox.setChecked(false);
bmwCheckBox.setChecked(false);
choosed = alfaCheckBox.getText().toString();
Log.v("",""+ choosed);
yourScore= yourScore + 0;
}
if (hasCheckedBmw) {
toyotaCheckBox.setChecked(false);
audiCheckBox.setChecked(false);
porscheCheckBox.setChecked(false);
alfaCheckBox.setChecked(false);
choosed = bmwCheckBox.getText().toString();
Log.v("",""+ choosed);
yourScore= yourScore + 0;
}
return yourScore;
}
public int onRadioButtonClickedLogo2(View view) {
String choosed = "";
int yourScore = 0;
RadioButton radiobuttonChooseAcura = (RadioButton) findViewById(R.id.acura);
RadioButton radiobuttonChooseToyota = (RadioButton) findViewById(R.id.toyota);
RadioButton radiobuttonChooseHonda = (RadioButton) findViewById(R.id.honda);
Boolean ischeckedAcura = radiobuttonChooseAcura.isChecked();
Boolean ischeckedToyota = radiobuttonChooseToyota.isChecked();
Boolean ischeckedHonda = radiobuttonChooseHonda.isChecked();
if (ischeckedAcura) {
radiobuttonChooseToyota.setChecked(false);
radiobuttonChooseHonda.setChecked(false);
choosed = radiobuttonChooseAcura.getText().toString();
Log.v("",""+ choosed);
yourScore= yourScore + 10;
}
if (ischeckedToyota) {
radiobuttonChooseAcura.setChecked(false);
radiobuttonChooseHonda.setChecked(false);
choosed = radiobuttonChooseToyota.getText().toString();
Log.v("",""+ choosed);
yourScore = yourScore + 0;
}
if (ischeckedHonda) {
radiobuttonChooseAcura.setChecked(false);
radiobuttonChooseToyota.setChecked(false);
choosed = radiobuttonChooseAcura.getText().toString();
Log.v("",""+ choosed);
yourScore = yourScore + 0;
}
return yourScore;
}
public int calculateScore(View view) {
int totalScore = (int) onRadioButtonClickedModel(view)+ (int)onRadioButtonClickedLogo1(view)+ (int)onCheckBoxCheckedModel(view)+(int)onRadioButtonClickedLogo2(view);
return totalScore;
}
public void sendEmail(String attachment, int score) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Quiz Total Score " + score);
intent.putExtra(Intent.EXTRA_TEXT, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:orientation="vertical"
tools:context="com.example.android.quizapplk.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Start Quiz"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="@android:color/black"
android:textAlignment="center"
android:textStyle="bold" />
<EditText
android:id="@+id/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Add Name"
android:inputType="textCapWords" />
<EditText
android:id="@+id/surname_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Add Surname"
android:inputType="textCapWords" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:text="@string/question1"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/m5_model"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M5"
android:textColor="@android:color/black"
android:textSize="20dp"
android:onClick="onRadioButtonClickedModel"
android:shadowColor="@color/colorAccent"/>
<RadioButton
android:id="@+id/r8_model"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R8"
android:textColor="@android:color/black"
android:onClick="onRadioButtonClickedModel"
android:textSize="20dp" />
<RadioButton
android:id="@+id/cls_model"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLS 63 Amg"
android:textColor="@android:color/black"
android:onClick="onRadioButtonClickedModel"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:text="@string/question2"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp">
<ImageView
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/maclaren" />
<RadioButton
android:id="@+id/maclaren_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedLogo1"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp">
<ImageView
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/bugatti" />
<RadioButton
android:id="@+id/bugatti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedLogo1"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp">
<ImageView
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/infinity" />
<RadioButton
android:id="@+id/infinity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedLogo1"
android:text="" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:text="@string/question3"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/toyota_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:layout_marginLeft="16dp"
android:onClick="onCheckBoxCheckedModel"
android:text="Toyota Avensis"
android:textSize="16sp" />
<CheckBox
android:id="@+id/audi_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:layout_marginLeft="16dp"
android:onClick="onCheckBoxCheckedModel"
android:text="Audi RS 7"
android:textSize="16sp" />
<CheckBox
android:id="@+id/porsche_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:layout_marginLeft="16dp"
android:text="Porsche Panamera 4S Executive"
android:onClick="onCheckBoxCheckedModel"
android:textSize="16sp" />
<CheckBox
android:id="@+id/alfaromeo_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckBoxCheckedModel"
android:paddingLeft="24dp"
android:layout_marginLeft="16dp"
android:text="Alfa Romeo Giulia"
android:textSize="16sp" />
<CheckBox
android:id="@+id/bmw_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckBoxCheckedModel"
android:paddingLeft="24dp"
android:layout_marginLeft="16dp"
android:text="BMW 715"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:text="@string/question4"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp">
<ImageView
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/acura" />
<RadioButton
android:id="@+id/acura"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedLogo2"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp">
<ImageView
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/totyota" />
<RadioButton
android:id="@+id/toyota"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedLogo2"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp">
<ImageView
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/hondalogo" />
<RadioButton
android:id="@+id/honda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClickedLogo2"
android:text="" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:onClick="submitQuiz"
android:text="SUBMIT QUIZ" />
</LinearLayout>
</ScrollView>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment