Sunday 29 November 2015

Source Code Pembuatan Alert Dialog dengan List Button di Android

//Code java :
//Author :midasoft.blogspot.com
package com.example.tampilanalertdialog;

import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;


public class AlertActivity extends Activity implements View.OnClickListener {
      Button pesanToast;
      Button keluar;
      Button tampilList;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_alert);
           
            pesanToast=(Button)findViewById(R.id.toast);
            pesanToast.setOnClickListener(this);
           
            keluar=(Button)findViewById(R.id.exit);
            keluar.setOnClickListener(this);
           
            tampilList=(Button)findViewById(R.id.listDialog);
            tampilList.setOnClickListener(this);
           
      }
      public void onClick(View view){
            if(view==pesanToast){
                  Toast.makeText(this, "Anda Memilih Tampilan Toast", Toast.LENGTH_SHORT).show();
                  }
                  else if(view==keluar){
                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setMessage("Apakah Anda Yakin..?”!“ ingin keluar?")
                  .setCancelable(false)
                  .setPositiveButton("Ya",new DialogInterface.OnClickListener() {
                       
                  public void onClick(DialogInterface dialog,int id) {
                        AlertActivity.this.finish();}
                        })
                  .setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();}}).show();
                  }
                  //menampilkan list dialog
                  else if(view== tampilList){
                  final CharSequence[] items = {"Senin", "Selasa", "Rabu", "Kamis","Jum'at","Sabtu","Minggu"};
                              AlertDialog.Builder builder = new AlertDialog.Builder(this);
                              builder.setTitle("Pilih Hari");
                              builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {
                                          public void onClick(DialogInterface dialog, int item){
                                                Toast.makeText(getApplicationContext(), items[item],
                                                Toast.LENGTH_SHORT).show();
                                                      }
                                          }).show();
                  }
      }

}


/* Code XML */
<?xml version="1.0" encoding="utf-8"?>
<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:text="Tampilkan Toast"
       android:id="@+id/toast"
       android:layout_height="wrap_content">
   </Button>
   <Button
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="Tampilkan List"
       android:id="@+id/listDialog">
    </Button>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Keluar sekarang"
        android:id="@+id/exit">
       
    </Button>
</LinearLayout>


0 comments:

Post a Comment