Saturday, 12 September 2015

Source code Alert dialog dengan Tampil List



/*Code java :
package com.example.tampilanalertdialog;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;


public class AlertActivity extends ActionBarActivity 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.setItems(items, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                              Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                              } }).show();}
                  }
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.alert, menu);
            return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                  return true;
            }
            return super.onOptionsItemSelected(item);
      }
}

/* 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