Mambuat “ALert” Dialog pada Android

Untuk membuat alert dialog  kita bisa menggunakan AlertDialogBuilder  . kodenya seperti dibawah ini :

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

Apabila dalam alert dialog ada 2 pengambilan keputusan, contohnya “Ya” atau “Tidak”, maka kodenya seperti dibawah ini :

alertDialogBuilder.setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
alertDialogBuilder.setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener)

Langkah awal membuat alert dialog, buatlah file baru dengan nama src/com.example.alertdialog/MainActivity.java

package com.example.alertdialog;
 
import com.example.alertdialog.*;
 
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
 
public class MainActivity extends Activity {
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
 
   public void open(View view){
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
      alertDialogBuilder.setMessage(R.string.decision);
      alertDialogBuilder.setPositiveButton(R.string.positive_button,
      new DialogInterface.OnClickListener() {
 
         @Override
         public void onClick(DialogInterface arg0, int arg1) {
            Intent positveActivity = new Intent(getApplicationContext(),com.example.alertdialog.PositiveActivity.class);
            startActivity(positveActivity);
 
         }
      });
      alertDialogBuilder.setNegativeButton(R.string.negative_button,
      new DialogInterface.OnClickListener() {
 
         @Override
         public void onClick(DialogInterface dialog, int which) {
            Intent negativeActivity = new Intent(getApplicationContext(),com.example.alertdialog.NegativeActivity.class);
            startActivity(negativeActivity);
         }
      });
 
      AlertDialog alertDialog = alertDialogBuilder.create();
      alertDialog.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.main, menu);
      return true;
   }
}

Berikut adalah kode standar dari src/com.example.alertdialog/PositiveActivity.java

package com.example.alertdialog;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
 
public class PositiveActivity extends Activity {
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_positive);
   }
 
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.positive, menu);
      return true;
   }
 
}

Berikut adalah kode standar dari src/com.example.alertdialog/NegativeActivity.java

package com.example.alertdialog;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
 
public class NegativeActivity extends Activity {
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_negative);
   }
 
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.negative, menu);
      return true;
   }
 
}

Berikut adalah kode dari res/layout/activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >
 
  <Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="170dp"
     android:onClick="open"
     android:text="@string/hello_world" />
 
</RelativeLayout>

Berikut adalah kode dari res/layout/activity_positive.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".PositiveActivity" >
 
   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="14dp"
      android:layout_marginTop="20dp"
      android:text="@string/positive"
      android:textAppearance="?android:attr/textAppearanceLarge" />
 
</RelativeLayout>

Berikut adalah kode dari res/layout/activity_negative.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".NegativeActivity" >
 
   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="14dp"
      android:layout_marginTop="17dp"
      android:text="@string/negative"
      android:textAppearance="?android:attr/textAppearanceLarge" />
 
</RelativeLayout>

Berikut adalah kode dari Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
   <string name="app_name">AlertDialog</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="title_activity_positive">PositiveActivity</string>
   <string name="title_activity_negative">NegativeActivity</string>
   <string name="positive">Positive Activity</string>
   <string name="negative">Negative Activity</string>
   <string name="decision">Are you sure, you wanted to make this decision</string>
   <string name="positive_button">+ive</string>
   <string name="negative_button">-ive</string>
 
</resources>

Berikut adalah kode standar dari AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.alertdialog"
   android:versionCode="1"
   android:versionName="1.0" >
 
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" />
 
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name="com.example.alertdialog.MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
 
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      <activity
         android:name="com.example.alertdialog.PositiveActivity"
         android:label="@string/title_activity_positive" >
      </activity>
      <activity
         android:name="com.example.alertdialog.NegativeActivity"
         android:label="@string/title_activity_negative" >
      </activity>
</application>
 
</manifest>

Sekarang hanya tekan tombol Halo dunia untuk melihat kotak peringatan, yang akan menjadi sesuatu seperti ini

android_alert_dialog2

Sekarang pilih salah satu dua tombol dan melihat aktivitas masing-masing loading. Apabila Anda memilih positve tombol, layar ini akan muncul

android_alert_dialog3

Sekarang tekan kembali tombol pada perangkat Anda, dan kali ini pilih negatif dari dialog peringatan Anda. Layar berikut akan muncul saat ini

android_alert_dialog4

Sumber : https://www.webhozz.com/blog/mambuat-alert-dialog-pada-android/

admin