Last active
February 2, 2017 13:15
-
-
Save alfianyusufabdullah/a375c1361903b74f8efc3a6e089c1699 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.jonesrandom.tutorialcustomviewalertdialog; | |
| import android.content.DialogInterface; | |
| import android.support.v7.app.AlertDialog; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.widget.Button; | |
| import android.widget.EditText; | |
| import android.widget.TextView; | |
| public class MainActivity extends AppCompatActivity { | |
| /// VARIABEL 2 TEXTVIEW & 1 BUTTON | |
| TextView dataNama; | |
| TextView dataAlamat; | |
| Button inputDialog; | |
| /// VARIABEL 2 EDITTEXT UNTUK DIALOG | |
| EditText inputNama; | |
| EditText inputAlamat; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| ///INISIALISASI TEXTVIEW & BUTTON DENGAN VIEW | |
| dataNama = (TextView) findViewById(R.id.dataNama); | |
| dataAlamat = (TextView) findViewById(R.id.dataAlamat); | |
| inputDialog = (Button) findViewById(R.id.btn_Dialog); | |
| /// MENAMBAH LISTENER PADA BUTTON UNTUK MEMANGGIL ALERTDIALOG | |
| inputDialog.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| /// INFLATE VIEW UNTUK ALERTDIALOG DENGAN LAYOUTINFLATER | |
| View viewDialog = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_view, null, false); | |
| /// INISIALISASI EDITTEXT DENGAN VIEW ALERDIALOG | |
| inputNama = (EditText) viewDialog.findViewById(R.id.inputNama); | |
| inputAlamat = (EditText) viewDialog.findViewById(R.id.inputAlamat); | |
| /// MEMBUAT ALERTDIALOG BUILDER | |
| AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); | |
| builder.setTitle("Input Data"); | |
| /// MENGATUR VIEW UNTUK ALERTDIALOG | |
| builder.setView(viewDialog); | |
| /// MENAMBAH BUTTON DIAOLOG UNTUK SET DATA | |
| builder.setPositiveButton("OKE", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialogInterface, int i) { | |
| String Nama = inputNama.getText().toString(); | |
| String Alamat = inputAlamat.getText().toString(); | |
| if (Nama.isEmpty() && Alamat.isEmpty()) { | |
| ///KONDISI JIKA KEDUA EDITTEXT KOSONG | |
| dataNama.setText(" Tidak Ada Nama"); | |
| dataAlamat.setText(" Tidak Ada Alamat"); | |
| } else if (!Nama.isEmpty() && Alamat.isEmpty()) { | |
| ///KONDISI JIKA EDITTEXT ALAMAT KOSONG | |
| dataNama.setText(Nama); | |
| dataAlamat.setText(" Tidak Ada Alamat"); | |
| } else if (Nama.isEmpty() && !Alamat.isEmpty()) { | |
| ///KONDISI JIKA EDITTEXT NAMA KOSONG | |
| dataNama.setText("Tidak Ada Nama"); | |
| dataAlamat.setText(Alamat); | |
| } else { | |
| ///KONDISI JIKA KEDUA EDITTEXT TIDAK KOSONG | |
| dataNama.setText(Nama); | |
| dataAlamat.setText(Alamat); | |
| } | |
| } | |
| }); | |
| builder.setNegativeButton("BATAL", null); | |
| AlertDialog dialog = builder.create(); | |
| dialog.show(); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment