Cara Buat Aplikasi Android Quiz (Dengan Perhitungan Score)


Ini Adalah Source Code Untuk Main1.java
import com.example.tugas7.R;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;

public class Main1 extends Activity implements OnClickListener {

     // Attribute
     ImageView soal_image, bg_soal;
     TextView text_soal, text_jawab, salah_benar, nomor;
     Button button_jawab, button_next1, button_next2;
     ImageButton button_a, button_b, button_c, button_d;

     int indeks = 0, index = 0, point = 0, benar = 0, salah = 0, no = 1;

     // Array Untuk Gambar
     int[] soalGAMBAR = {
                   R.drawable.image_sillenthill,
                   R.drawable.image_peta2 };

     int[] jawab_a = {
                   R.drawable.image_facebook,
                   R.drawable.image_kelelawar };
     int[] jawab_b = {
                   R.drawable.image_myspace,
                   R.drawable.image_kucing };
     int[] jawab_c = {
                   R.drawable.image_twittter,
                   R.drawable.image_labalaba };
     int[] jawab_d = {
                   R.drawable.image_youtube,
                   R.drawable.image_panci };

     // Array Untuk Text
     String[] soalTEXT = {
                   "Manakah yang merupakan Icon Twitter?",
                   "Manakah yang bukan merupakan Binatang ?" };

     String[] textHINT = {
                   "Apa Nama Game pada Gambar Tersebut ?",
                   "Dimnakah Lokasi Terjadinya Kebakaran ?" };

     // Array Untuk Jawaban
     String[] jawabSATU = {
                   "Silent Hill",
                   "Braga" };
    
     String[] jawabDUA = { "C", "D" };

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

            soal_image = (ImageView) findViewById(R.id.Soal_Gambar);
            soal_image.setImageResource(soalGAMBAR[0]);

            text_jawab = (TextView) findViewById(R.id.Text_Jawab);
            text_jawab.setHint(textHINT[0]);

            button_jawab = (Button) findViewById(R.id.Button_Jawab);
            button_jawab.setOnClickListener(this);

            bg_soal = (ImageView) findViewById(R.id.Bg_Soal);
            nomor = (TextView) findViewById(R.id.No);
            nomor.setText("Soal " + no++);

            text_soal = (TextView) findViewById(R.id.Soal);
            text_soal.setText(soalTEXT[0]);

            button_a = (ImageButton) findViewById(R.id.BI_Jawab1);
            button_a.setImageResource(jawab_a[0]);
            button_a.setOnClickListener(this);

            button_b = (ImageButton) findViewById(R.id.BI_Jawab2);
            button_b.setImageResource(jawab_b[0]);
            button_b.setOnClickListener(this);

            button_c = (ImageButton) findViewById(R.id.BI_Jawab3);
            button_c.setImageResource(jawab_c[0]);
            button_c.setOnClickListener(this);

            button_d = (ImageButton) findViewById(R.id.BI_Jawab4);
            button_d.setImageResource(jawab_d[0]);
            button_d.setOnClickListener(this);

            salah_benar = (TextView) findViewById(R.id.Benar_Salah);

            button_next1 = (Button) findViewById(R.id.Button_Next1);
            button_next1.setOnClickListener(this);

            button_next2 = (Button) findViewById(R.id.Button_Next2);
            button_next2.setOnClickListener(this);

     }

     public void onClick(View v) {

            // Untuk Soal 1 - 2
            if (v == button_jawab) {
                   String jawab_user = text_jawab.getText().toString();

                   if (jawab_user.equalsIgnoreCase(jawabSATU[indeks])) {
                         salah_benar.setText("BENAR");
                         salah_benar.setTextColor(Color.GREEN);
                         benar++;
                         point = point + 25;
                   } else {
                         salah_benar.setText("SALAH");
                         salah_benar.setTextColor(Color.RED);
                         salah++;
                   }

                   salah_benar.setVisibility(View.VISIBLE);
                   button_next1.setVisibility(View.VISIBLE);
                   button_jawab.setEnabled(false);
                   button_jawab.setTextColor(Color.WHITE);

            }

            else if (v == button_next1) {

                   text_jawab.setText("");
                   salah_benar.setText("");
                   nomor.setText("Soal " + no++);
                   button_next1.setVisibility(View.INVISIBLE);

                   button_jawab.setEnabled(true);
                   button_jawab.setTextColor(Color.RED);

                   if (indeks < jawabSATU.length - 1) {

                         indeks++;
                         soal_image.setImageResource(soalGAMBAR[indeks]);
                         text_jawab.setHint(textHINT[indeks]);

                   } else {

                         soal_image.setVisibility(View.INVISIBLE);
                         text_jawab.setVisibility(View.INVISIBLE);
                         button_jawab.setVisibility(View.INVISIBLE);

                         bg_soal.setVisibility(View.VISIBLE);
                         text_soal.setVisibility(View.VISIBLE);
                         button_a.setVisibility(View.VISIBLE);
                         button_b.setVisibility(View.VISIBLE);
                         button_c.setVisibility(View.VISIBLE);
                         button_d.setVisibility(View.VISIBLE);

                   }
            }

            // Untuk Soal 3 - 4
            else if (v == button_a) {
                   if (jawabDUA[index].equals("A")) {
                         salah_benar.setText("BENAR");
                         salah_benar.setTextColor(Color.GREEN);
                         benar++;
                         point = point + 25;
                   } else {
                         salah_benar.setText("SALAH");
                         salah_benar.setTextColor(Color.RED);
                         salah++;
                   }
                   button_next2.setVisibility(View.VISIBLE);
            }

            else if (v == button_b) {
                   if (jawabDUA[index].equals("B")) {
                         salah_benar.setText("BENAR");
                         salah_benar.setTextColor(Color.GREEN);
                         benar++;
                         point = point + 25;
                   } else {
                         salah_benar.setText("SALAH");
                         salah_benar.setTextColor(Color.RED);
                         salah++;
                   }
                   button_next2.setVisibility(View.VISIBLE);
            }

            else if (v == button_c) {
                   if (jawabDUA[index].equals("C")) {
                         salah_benar.setText("BENAR");
                         salah_benar.setTextColor(Color.GREEN);
                         benar++;
                         point = point + 25;
                   } else {
                         salah_benar.setText("SALAH");
                         salah_benar.setTextColor(Color.RED);
                         salah++;
                   }
                   button_next2.setVisibility(View.VISIBLE);
            }

            else if (v == button_d) {
                   if (jawabDUA[index].equals("D")) {
                         salah_benar.setText("BENAR");
                         salah_benar.setTextColor(Color.GREEN);
                         benar++;
                         point = point + 25;
                   } else {
                         salah_benar.setText("SALAH");
                         salah_benar.setTextColor(Color.RED);
                         salah++;
                   }
                   button_next2.setVisibility(View.VISIBLE);
            }

            else if (v == button_next2) {

                   if (index < jawabDUA.length - 1) {

                         salah_benar.setText("");
                         nomor.setText("Soal " + no);
                         button_next2.setVisibility(View.INVISIBLE);

                         index++;
                         text_soal.setText(soalTEXT[index]);
                         button_a.setImageResource(jawab_a[index]);
                         button_b.setImageResource(jawab_b[index]);
                         button_c.setImageResource(jawab_c[index]);
                         button_d.setImageResource(jawab_d[index]);

                   } else {
                        
                         // Pindah ke MainActivity2
                         Intent m2 = new Intent(Main1.this, Main2.class);
                         m2.putExtra("BenaR", benar);
                         m2.putExtra("SalaH", salah);
                         m2.putExtra("PoinT", point);
                         startActivity(m2);
                         finish();

                   }
            }
     }
}

Ini Adalah Souce Code Untuk Main2.java
import com.example.tugas7.R;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;

public class Main2 extends Activity implements OnClickListener {

     int point, benar, salah;

     TextView nilai_point, judul, hasil, text_jawab, nomor;
     Button exit, button_jawab;
     ImageView bg_soal;
     RelativeLayout background;

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

            // Mengambil Point dan Jawaban Benar | Salah
            benar = getIntent().getIntExtra("BenaR", 0);
            salah = getIntent().getIntExtra("SalaH", 0);
            point = getIntent().getIntExtra("PoinT", 0);

            text_jawab = (TextView) findViewById(R.id.Text_Jawab);
            text_jawab.setVisibility(View.INVISIBLE);

            button_jawab = (Button) findViewById(R.id.Button_Jawab);
            button_jawab.setVisibility(View.INVISIBLE);

            bg_soal = (ImageView) findViewById(R.id.Bg_Soal);
            bg_soal.setVisibility(View.VISIBLE);

            background = (RelativeLayout) findViewById(R.id.Bg_Layout);
            background.setBackgroundResource(R.drawable.bg_umbrella);

            judul = (TextView) findViewById(R.id.Judul_Hasil);
            judul.setVisibility(View.VISIBLE);

            nomor = (TextView) findViewById(R.id.No);
            nomor.setText("Hasil");

            hasil = (TextView) findViewById(R.id.Hasil_Jawaban);
            hasil.setVisibility(View.VISIBLE);
            hasil.setText("Jawaban Benar : " + benar + "\nJawaban Salah : "
+ salah);

            nilai_point = (TextView) findViewById(R.id.Point);
            nilai_point.setText("Nilai Kamu : " + point);

            exit = (Button) findViewById(R.id.Button_Exit);
            exit.setOnClickListener(this);
            exit.setVisibility(View.VISIBLE);

     }

     public void onClick(View v) {
            if (v == exit) {
                   finish();
            }
     }
}

Ini Adalah Source Code Untuk activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/Bg_Layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/bg_umbrella"
   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=".Main1" >

   <TextView
       android:id="@+id/Judul"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:text="@string/judul"
       android:textColor="#FF0000"
       android:textSize="32sp"
       android:textStyle="bold" />

   <TextView
       android:id="@+id/Copyright"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/Judul"
       android:layout_centerHorizontal="true"
       android:text="@string/copyright"
       android:textColor="#FFFF00"
       android:textSize="14sp"
       android:textStyle="bold" />

   <TextView
       android:id="@+id/No"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/Copyright"
       android:layout_marginLeft="30dp"
       android:layout_marginTop="70dp"
       android:text="Soal"
       android:textColor="#FF9900"
       android:textSize="18sp"
       android:textStyle="bold"
       android:typeface="sans" />

   <DigitalClock
       android:id="@+id/Time"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/Copyright"
       android:layout_marginRight="30dp"
       android:layout_marginTop="70dp"
       android:layout_toRightOf="@id/No"
       android:gravity="right"
       android:textColor="#00FFFF"
       android:textSize="16sp"
       android:textStyle="bold"
       android:typeface="normal" />

   <!-- Soal Gambar -->

   <ImageView
       android:id="@+id/Soal_Gambar"
       android:layout_width="400dp"
       android:layout_height="300dp"
       android:layout_below="@id/No"
       android:layout_centerHorizontal="true"
       android:alpha="100" />

   <!-- Soal Text -->

   <ImageView
       android:id="@+id/Bg_Soal"
       android:layout_width="400dp"
       android:layout_height="wrap_content"
       android:layout_below="@id/Copyright"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="50dp"
       android:alpha="100"
       android:src="@drawable/bg_umbrella"
       android:visibility="invisible" />

   <TextView
       android:id="@+id/Soal"
       android:layout_width="300dp"
       android:layout_height="wrap_content"
       android:layout_below="@id/No"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="60dp"
       android:gravity="center"
       android:text=""
       android:textSize="17sp"
       android:textStyle="bold"
       android:visibility="invisible" />
  
   <!-- Point Text -->

   <TextView
       android:id="@+id/Judul_Hasil"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/No"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="30dp"
       android:text="Hasil Nilai Kamu"
       android:textColor="#FF00FF"
       android:textSize="23sp"
       android:textStyle="bold"
       android:visibility="invisible" />

   <TextView
       android:id="@+id/Hasil_Jawaban"
       android:layout_width="300dp"
       android:layout_height="wrap_content"
       android:layout_below="@id/No"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="90dp"
       android:gravity="left"
       android:text=""
       android:textSize="17sp"
       android:textStyle="bold"
       android:visibility="invisible" />

   <TextView
       android:id="@+id/Point"
       android:layout_width="300dp"
       android:layout_height="wrap_content"
       android:layout_below="@id/Hasil_Jawaban"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="15dp"
       android:gravity="left"
       android:text=""
       android:textColor="#0000FF"
       android:textSize="21sp"
       android:textStyle="bold" />

   <!-- Input Text & Button JAWAB -->

   <EditText
       android:id="@+id/Text_Jawab"
       android:layout_width="300dp"
       android:layout_height="wrap_content"
       android:layout_below="@id/Soal_Gambar"
       android:layout_marginLeft="30dp"
       android:gravity="center_horizontal"
       android:hint=""
       android:textColor="#9900FF"
       android:textSize="14sp"
       android:textStyle="italic"
       android:typeface="serif" />

   <Button
       android:id="@+id/Button_Jawab"
       android:layout_width="wrap_content"
       android:layout_height="40dp"
       android:layout_below="@id/Soal_Gambar"
       android:layout_marginRight="20dp"
       android:layout_toRightOf="@id/Text_Jawab"
       android:text="» Jawab! «"
       android:textColor="#FF0000"
       android:textSize="14sp"
       android:textStyle="bold" />

   <!-- Image Button JAWAB -->

   <TextView
       android:id="@+id/Tengah"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/Soal"
       android:layout_centerHorizontal="true"
       android:text="" />

   <ImageButton
       android:id="@+id/BI_Jawab1"
       android:layout_width="70dp"
       android:layout_height="70dp"
       android:layout_below="@id/No"
       android:layout_marginRight="20dp"
       android:layout_marginTop="130dp"
       android:layout_toLeftOf="@id/Tengah"
       android:visibility="invisible" />

   <ImageButton
       android:id="@+id/BI_Jawab2"
       android:layout_width="70dp"
       android:layout_height="70dp"
       android:layout_below="@id/BI_Jawab1"
       android:layout_marginRight="20dp"
       android:layout_toLeftOf="@id/Tengah"
       android:visibility="invisible" />

   <ImageButton
       android:id="@+id/BI_Jawab3"
       android:layout_width="70dp"
       android:layout_height="70dp"
       android:layout_below="@id/No"
       android:layout_marginLeft="20dp"
       android:layout_marginTop="130dp"
       android:layout_toRightOf="@id/Tengah"
       android:visibility="invisible" />

   <ImageButton
       android:id="@+id/BI_Jawab4"
       android:layout_width="70dp"
       android:layout_height="70dp"
       android:layout_below="@id/BI_Jawab3"
       android:layout_marginLeft="20dp"
       android:layout_toRightOf="@id/Tengah"
       android:visibility="invisible" />

   <!-- Benar atau Salah (Keterangan) -->

   <TextView
       android:id="@+id/Benar_Salah"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/Text_Jawab"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="30dp"
       android:text=""
       android:textColor="#00FF00"
       android:textSize="23sp"
       android:textStyle="bold"
       android:typeface="serif"
       android:visibility="invisible" />

   <!-- Button NEXT 1 -->

   <Button
       android:id="@+id/Button_Next1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/Benar_Salah"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="10dp"
       android:drawableRight="@drawable/icon_buttonnext"
       android:text="NEXT"
       android:textColor="#FFFFFF"
       android:textSize="16sp"
       android:textStyle="bold"
       android:visibility="invisible" />

   <!-- Button NEXT 2 -->

   <Button
       android:id="@+id/Button_Next2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/Benar_Salah"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="10dp"
       android:drawableRight="@drawable/icon_buttonnext"
       android:text="NEXT"
       android:textColor="#FFFFFF"
       android:textSize="16sp"
       android:textStyle="bold"
       android:visibility="invisible" />

   <!-- Button EXIT -->

   <Button
       android:id="@+id/Button_Exit"
       android:layout_width="90dp"
       android:layout_height="wrap_content"
       android:layout_below="@id/Point"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="35dp"
       android:text="EXIT"
       android:textColor="#FF0000"
       android:textSize="15sp"
       android:textStyle="bold"
       android:visibility="invisible" />

   <TextView
       android:id="@+id/Mafia"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_centerHorizontal="true"
       android:text="@string/mafia"
       android:textColor="#FFFF00"
       android:textSize="14sp"
       android:textStyle="bold" />

</RelativeLayout>

Ini Source Code Untuk string.xml Yang Berada Di Dalam Folder Values letak Folder Values Ada Di Bawah Folder Menu
<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="app_name">Tugas 7 | Pemograman Game (Jeni4)</string>
   <string name="action_settings">Mobile Android™</string>
   <string name="judul">Jawab Pertanyaaan Berikut ini!</string>
   <string name="copyright">Design By[ Priwijaya Mamonto ||
131315034™ ]</string>
   <string name="mafia">Design by Jaya™ prodc. [ Politeknik Gorontalo ]</string>

</resources>

Dan Selanjutnya Kita Rubah Source Code Di Android Manifestnya Seperti Di Bawah Ini
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.tugas7"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
       android:minSdkVersion="8"
       android:targetSdkVersion="17" />

<application
       android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
      
       <!-- MainActivity 1 -->
       <activity
           android:name="com.example.tugas7.Main1"
           android:label="@string/app_name"
           android:screenOrientation="portrait">
           <intent-filter> <!-- Unt menampilkan Activity di Awal -->
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
      
       <!-- MainActivity 2 -->
       <activity
           android:name="com.example.tugas7.Main2"
           android:label="@string/app_name"
           android:screenOrientation="portrait">
       </activity>
      
   </application>
   </manifest>


Dan Ini Adalah Hasil Running Akhir Dari Source Code Di Atas

Komentar

Postingan populer dari blog ini

Cara Buat Form Login & Register Database MYSQL Dengan Eclipse Adt

Cara Buat Aplikasi Android Biodata

Cara Buat Aplikasi Android From Login & Register