Minggu, 18 Mei 2014

PENGGUNAAN SCROLLVIEW PADA ANDROID ECLIPSE


Scroll view adalah salah satu composit dalam android eclipse,
Untuk mengembangkan aplikasi, kadang terkendala dengan ukuran layar tampilan. Ukuran layar bisa lebih kecil dabanding ukuran tampilan aplikasi kita. Untuk mengatasi hal itu, android memiliki composit ScrollView sehingga kita bisa menampilkan sebagian informasi dalam satu waktu sedangkan bagian informasi lainnya dapat ditampilkan dengan menaikkan layar ke atas atau ke bawah.
       Tanpa ScrollView, informasi yang kita sampaikan akan terpotong sebatas layar device, sisanya tidak terlihat.
       Berikut adalah contoh aplikasi sederhana yang menggunakan ScrollView, sebenarnya ada 2 orientasi scrollview yaitu vertical dan horizontal, namun saya akan menunjukan contoh yang vertical, cukup pada layout saja tambahkan ScrollView vertical,
  • Source code Layout 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableLayout
        android:id="@+id/tablelayout1"
        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_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Anjing pemburu dan sang Kelinci"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imagedongeng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:src="@drawable/dongengkelinci" />

    <TextView
        android:id="@+id/textdongeng"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imagedongeng"
        android:layout_marginTop="19dp"
        android:text="
        Seekor anjing pemburu yang mengejar-ngejar seekor kelinci, dan setelah beberapa lama, sang Anjing menyerah dan menghentikan pengejarannya.
        Kebetulan kejadian itu disaksikan oleh seorang gembala yang langsung mengejek sang Anjing dan berkata:
        &apos;Hewan yang lebih kecil ternyata larinya lebih kencang dibandingkan dengan kamu,&apos;
        Sang Anjing menjawab, &apos;Tidakkah kamu melihat perbedaan di antara kami? Saya berlari untuk mendapatkan makanan, sedangkan
sang Kelinci berlari untuk mempertahankan hidupnya.&apos;
Keinginan menentukan kerasnya suatu usaha." />

</TableLayout>
</ScrollView>

  • Screen Shoot

Senin, 12 Mei 2014

Membuat Quiz sederhana menggunakan Android Eclipse






Hello World !

Saya akan berbagi bagaimana cara membuat quiz dalam Android eclipse, ada 2 macam quiz yang akan di tunjukan yaitu :
1. Pertanyaan menggunakan gambar dan jawaban memakai teks isian.
2. Pertanyaan menggunakan teks dan jawaban dengan tombol gambar pilihan ganda.
    Berikut adalah cara membuat quis yang pertanyaan menggunakan gambar dan jawaban memakai teks isian, pertama Copy kan gambar-gambar yang di perlukan (untuk imageview/imagebutton) pada drawable nya , dengan ukuran yang cukup.
  • Layout source code
<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=".Main" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Do you know ?" />

    <ImageView
        android:id="@+id/imagekatniss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:src="@drawable/katniss" />

    <EditText
        android:id="@+id/editTextisi"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imagekatniss"
        android:layout_marginTop="16dp"
        android:ems="10" />

    <TextView
        android:id="@+id/salahbenar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imagekatniss"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp"
        android:text="sb" />

    <Button
        android:id="@+id/btn_jwb"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editTextisi"
        android:text="Answer" />

    <Button
        android:id="@+id/btn_next"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btn_jwb"
        android:visibility="invisible"
        android:text="Next" />

</RelativeLayout>
  • Activity source code


package arcel.minigame;

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {

     Button btn_jwb, btn_next;
     TextView salahbenar;
     EditText ngisi;
     ImageView soal;
     int []pertanyaan = {R.drawable.katniss,R.drawable.angelfals,R.drawable.malela,
                                R.drawable.minion,R.drawable.venesia,R.drawable.channingtatum,R.drawable.legonpari,
                                R.drawable.minesweeper,R.drawable.mainecoon1,R.drawable.upsidedown};
     String [] jawaban = {"Katniss Everdeen","Angels falls","Air terjun Malela","Bee-do minion",
                                "Venesia","Channing Tatum","Legon Pari","Minesweeper","Maine coon","Upside down"};
     int indeks = 0;
     int salah = 0 , benar = 0 , skor = 0;
    
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.layout1);
          
           soal = (ImageView)findViewById(R.id.imagekatniss);
           soal.setImageResource(pertanyaan[0]);
          
           salahbenar=(TextView)findViewById(R.id.salahbenar);
          
           btn_jwb = (Button)findViewById(R.id.btn_jwb);
           btn_jwb.setOnClickListener(this);
          
           btn_next = (Button)findViewById(R.id.btn_next);
           btn_next.setOnClickListener(this);
          
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
           // Inflate the menu; this adds items to the action bar if it is present.
           getMenuInflater().inflate(R.menu.layout1, menu);
           return true;
     }

     @Override
     public void onClick(View v)
     {
           if (v==btn_jwb)
           {
                ngisi = (EditText)findViewById(R.id.editTextisi);
                String jawabanuser = ngisi.getText().toString();
                if (jawabanuser.equalsIgnoreCase(jawaban[indeks]))
                {
                     salahbenar.setText("Benar");
                }
                else
                {
                     salahbenar.setText("Salah");
                }
                btn_next.setVisibility(View.VISIBLE);
           }
           else if (v==btn_next)
           {
                if (indeks<pertanyaan.length-1)
                {
                     indeks ++;
                     soal.setImageResource(pertanyaan[indeks]);
                     salahbenar.setText("");
                     ngisi.setText("");
                }
                btn_next.setVisibility(View.INVISIBLE);
           }
     }
}

  • Screen Shoot



       Lalu berikut adalah cara membuat quiz yang pertanyaan menggunakan teks dan jawaban dengan tombol gambar pilihan ganda, pertama sediakan 2 Activity dan 2 Layout
  • Souce code Layout 1
<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:background="#7FFFD4"
    tools:context=".Main" >

    <TextView
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

    <ImageButton
        android:id="@+id/siberian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/golden"
        android:layout_toRightOf="@+id/hello"
        android:src="@drawable/siberianhuskyyy" />

    <ImageButton
        android:id="@+id/samoyed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/golden"
        android:layout_below="@+id/siberian"
        android:layout_marginTop="29dp"
        android:layout_toLeftOf="@+id/hello"
        android:src="@drawable/samoyeddd" />

    <ImageButton
        android:id="@+id/akita"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/samoyed"
        android:layout_toRightOf="@+id/hello"
        android:src="@drawable/akitainuu" />

    <ImageButton
        android:id="@+id/golden"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/soal"
        android:layout_marginTop="40dp"
        android:layout_toLeftOf="@+id/hello"
        android:src="@drawable/goldenretrieverrr" />

    <TextView
        android:id="@+id/soal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/hello"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Questions" />

    <Button
        android:id="@+id/btn_next"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/akita"
        android:layout_marginBottom="28dp"
        android:text="Next" />

    <TextView
        android:id="@+id/salahbenar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/samoyed"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="" />

</RelativeLayout>
  • Source Code Layout 2


<?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"
    android:background="#EE82EE" >
   

    <TextView
        android:id="@+id/Hasil1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="150dp"
        android:text="Hasil" />

    <Button
        android:id="@+id/btnexit"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="20dp"
        android:text="exit" />
   
</RelativeLayout>


  • Source code Activity 1
package arcel.choseeit;

import android.os.Bundle;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {

     Button btn_next;
     ImageButton pilih_a,pilih_b,pilih_c,pilih_d;
     TextView soal,salahbenar;
     String [] pertanyaan ={"Manakah jenis anjing yang memerankan peran Haciko?",
     "Bill gates adalah pembuat pertama kali Microsoft, manakah dia?",
     "Tumbuhan mana kah yang memiliki nama latin Ficus Benjamina?",
     "Dalam Film Finding Nemo ada tokoh yang mengidap Hilang ingatan sementara , manakah ia?",
     "Manakah kucing yang jenis nya American Curl?"};
    
     int [] pilihan_a ={R.drawable.goldenretrieverrr,R.drawable.facebook,    R.drawable.bakauuu,R.drawable.doriii,R.drawable.blacannn};
     int [] pilihan_b
={R.drawable.siberianhuskyyy,R.drawable.billgates,R.drawable.cemara,R.drawable.ikan,R.drawable.maruu};
     int [] pilihan_c ={R.drawable.samoyeddd,R.drawable.channingtatumm,R.drawable.ficusbenjaminaa,R.drawable.kurakuraaa,R.drawable.americancurl};
     int [] pilihan_d ={R.drawable.akitainuu,R.drawable.jackdorseywitter,R.drawable.sawittt,R.drawable.nemoo,R.drawable.toygerrr};
     String [] jawaban = {"D","B","C","A","C"};
     int indeks=0;
     int salah = 0, benar = 0 , skor = 0;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.layoutsatu);
          
           soal=(TextView)findViewById(R.id.soal);
           soal.setText(pertanyaan[0]);
          
           salahbenar=(TextView)findViewById(R.id.salahbenar);
          
           pilih_a=(ImageButton)findViewById(R.id.golden);
           pilih_a.setImageResource(pilihan_a[0]);
           pilih_a.setOnClickListener(this);
          
           pilih_b=(ImageButton)findViewById(R.id.siberian);
           pilih_b.setImageResource(pilihan_b[0]);
           pilih_b.setOnClickListener(this);
          
           pilih_c=(ImageButton)findViewById(R.id.samoyed);
           pilih_c.setImageResource(pilihan_c[0]);
           pilih_c.setOnClickListener(this);
          
           pilih_d=(ImageButton)findViewById(R.id.akita);
           pilih_d.setImageResource(pilihan_d[0]);
           pilih_d.setOnClickListener(this);
          
           btn_next=(Button)findViewById(R.id.btn_next);
           btn_next.setOnClickListener(this);
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
           // Inflate the menu; this adds items to the action bar if it is present.
           getMenuInflater().inflate(R.menu.layoutsatu, menu);
           return true;
     }

     @Override
     public void onClick(View v)
     {
     if (v==pilih_a)
           {
           if (jawaban[indeks].equals("A"))
                {
                salahbenar.setText("benar");benar++;
                }
           else
                {
                salahbenar.setText("salah");salah++;
                }
           }
     else if (v==pilih_b)
           {
           if (jawaban[indeks].equals("B"))
                {
                salahbenar.setText("benar");benar++;
                }
           else
                {
                salahbenar.setText("salah");salah++;
                }
           }
     else if (v==pilih_c)
           {
           if (jawaban[indeks].equals("C"))
                {
                salahbenar.setText("benar");benar++;
                }
           else
           {
                salahbenar.setText("salah");salah++;
           }
           }
     else if (v==pilih_d)
           {
           if (jawaban[indeks].equals("D"))
                {
                salahbenar.setText("benar");benar++;
                }
           else
           {
                salahbenar.setText("salah");salah++;
           }
           }
     else if (v==btn_next)
           {
                if (indeks<jawaban.length-1)
                {
                     indeks++;
                     soal.setText(pertanyaan[indeks]);
                     pilih_a.setImageResource(pilihan_a[indeks]);
                     pilih_b.setImageResource(pilihan_b[indeks]);
                     pilih_c.setImageResource(pilihan_c[indeks]);
                     pilih_d.setImageResource(pilihan_d[indeks]);
                     salahbenar.setText("");
                }
                else
                {
                     Intent i = new Intent (Main.this,hasil.class);
                     i.putExtra("Benar", benar);
                     i.putExtra("salah", salah);
                     startActivity(i);
                }
           }
     }
}
  • Source Code Activity 2
package arcel.choseeit;

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

public class hasil extends Activity implements OnClickListener {

     TextView hasil;
    
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.hasilnya);
          
           int benar = getIntent().getIntExtra("Benar", 0);
           int salah = getIntent().getIntExtra("Salah", 0);
          
           hasil= (TextView)findViewById(R.id.Hasil1);
           hasil.setText("Jawaban benar : "+benar+"Jawaban Salah : "+salah);
           }
    
     @Override
     public void onClick(View arg0) {
           // TODO Auto-generated method stub
     }
}
  • Screen Shoot