Saturday 12 September 2015

Source code cara membuat Alert dialog di Android



/*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;
      @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);
           
      }
      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();
                        }
      }
      @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) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // 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="~Keluar Sekarang"
      android:id="@+id/exit"></Button>
 </LinearLayout>

0 comments:

Post a Comment