Selasa, 28 Oktober 2014

Widget: Kotak Dialog

Kotak dialog merupakan sarana interaktif antar user dengan aplikasi. Skenarionya, ketika klik menu Keluar Aplikasi, akan tampil pesan "Apakah Kamu Benar-Benari Ingin Keluar Aplikasi" dengan pilihan "Iya atau Tidak". Berikut langkah pembuatannya.
Buat file layout dengan nama "activity_kotak_dialog.xml" pada folder res>layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:layout_width="fill_parent"
        android:id="@+id/toastBtn"
        android:layout_height="wrap_content"
        android:text="@string/toastTextBtn">       
    </Button>
    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listDialogBtn"
        android:text="@string/listDialogTextBtn">       
    </Button>
    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exitTextBtn"
        android:id="@+id/exitBtn">       
    </Button>
</LinearLayout>


Buat file java dengan nama "KotakDialog.java" pada folder src.
package com.example.contoh;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class KotakDialog extends Activity implements OnClickListener {
    Button pesanToast;
    Button keluar;
    Button tampilList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kotak_dialog);
        pesanToast = (Button) findViewById(R.id.toastBtn);
        pesanToast.setOnClickListener(this);
        keluar = (Button) findViewById(R.id.exitBtn);
        keluar.setOnClickListener(this);
        tampilList = (Button)
        findViewById(R.id.listDialogBtn);
        tampilList.setOnClickListener(this);
    }
    public void onClick(View clicked) {
        switch (clicked.getId()) {
            case R.id.listDialogBtn: munculListDialog(); break;       
            case R.id.toastBtn: Toast.makeText(this, "Kamu memilih Toast", Toast.LENGTH_SHORT).show(); break;
            case R.id.exitBtn: exit(); break;
        }
    }
   private void munculListDialog() {
        // TODO Auto-generated method stub
        final CharSequence[] items = { "Es Teh", "Es Jeruk", "Lemon Squash","Soft Drink" };
        AlertDialog.Builder kk = new AlertDialog.Builder(this);
        kk.setTitle("Pilih Minuman");
        kk.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                }
            }).show();
    }
    private void exit() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Apakah Kamu Benar-Benar ingin keluar?")
        .setCancelable(false)
        .setPositiveButton("Ya", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                KotakDialog.this.finish();
                }
            })
            .setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int arg1) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
        }   
        }).show();
    }   
}



Tambahkan source code berikut pada file "strings.xml" yang tersimpan di folder res>values.
 <string name="toastTextBtn">Panggil Toast</string>
    <string name="exitTextBtn">Keluar Applikasi</string>
    <string name="listDialogTextBtn">Panggil List Dialog</string>


Terakhir, aktifkan kelas java yang akan dipanggil untuk di-running pada file AndroidManifest.xml.
<activity
            android:name="com.example.contoh.KotakDialog"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


Berikut tampilannya.

1 komentar: