Cara Membuat Custom Alert Dialog Di Android Studio

Android Tutorial -  Custom Alert Dialog akan dugunakan untuk form pengisian Login.

Langsung saja, untuk menampilkan Custom Alert Dialog di Android, tahap-tahapnya sebagai berikut :
Oke, pertama-tama buatlah sebuah project baru di Android Studio terlebih dahulu.
Buat project baru di Android Studio File ⇒ New Project. Kemudian pilih Blank Activity dan melanjutkannya hingga selesai.

Setelah itu buka file layout xml, home.xml, dan isikan kode berikut (layout hanya berisi satu buah tombol).
Berikut ini codingannya :

home.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
   tools:context="com.androidtutorial.com.customalertdialog.Home">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Dialog"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" 
       android:id="@+id/showdialog"/>
</RelativeLayout>

Kemudian pada Home.java, isikan kode berikut :

Home.java

package com.androidtutorial.com.customalertdialog;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Home extends AppCompatActivity {

    public EditText email,password;
    public Button showdialog,submit;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        showdialog = (Button)findViewById(R.id.showdialog);
        showdialog.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                AlertDialog.Builder dialog1 = new AlertDialog.Builder(Home.this);
                View view = getLayoutInflater().inflate(R.layout.custom_dialog,null);
                email = (EditText)view.findViewById(R.id.email);
                password = (EditText)view.findViewById(R.id.password);
                submit = (Button)view.findViewById(R.id.submit);
                dialog1.setView(view);

                final AlertDialog dialog2 = dialog1.create();
                dialog2.show();

                submit.setOnClickListener(new View.OnClickListener() {
                    @Override                    public void onClick(View v) {
                        if (!email.getText().toString().isEmpty() && !password.getText().toString().isEmpty())
                        {
                            Toast.makeText(Home.this, "Login Sukses", Toast.LENGTH_SHORT).show();
                            dialog2.dismiss();
                        }
                        else                        {
                            Toast.makeText(Home.this, "Login Error", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}

lalu buat layout baru, custom_dialog.xml.dan isikan kodingan seperti berikut ini :

custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="5dp">

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Login"        android:textSize="25sp"        android:textStyle="bold"        android:layout_gravity="center"        android:layout_marginTop="5dp"        android:id="@+id/login"/>
    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Email"        android:ems="10"        android:id="@+id/email"        android:inputType="text"/>
    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Password"        android:ems="15"        android:id="@+id/password"        android:inputType="textPassword"/>
    <Button
        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Submit"        android:background="@color/colorAccent"        android:textColor="@android:color/white"        android:id="@+id/submit"/>
</LinearLayout>

DEMO :


Terimakasih sudah berkunjung ke blog AndroidTutorial.





Sekian tutorial dari saya semoga dapat bermanfaat untuk anda..

Komentar

Postingan populer dari blog ini

Cara membuat Search View di android studio

Tutorial cara membuat Barcode di android studio

Cara membuat Spinner di Android Studio