Membuat Alert Dialog dengan Android Studio
Aug 22, 2017
Add Comment
<?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="giviews.id.customalertdialog.MainActivity">
<Button
android:id="@+id/btnShowDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_dialog"
tools:layout_centerVertical="true"
tools:layout_centerHorizontal="true" />
</RelativeLayout>
<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="giviews.id.customalertdialog.MainActivity">
<Button
android:id="@+id/btnShowDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_dialog"
tools:layout_centerVertical="true"
tools:layout_centerHorizontal="true" />
</RelativeLayout>
Selanjutnya buat layout untuk isi dialognya, misalnya dialog untuk login. disini saya menamainya dengan dialog_login.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:layout_gravity="center"
android:text="@string/login"
android:id="@+id/lblLogin"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:textStyle="bold"/>
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="10dp"
android:inputType="textEmailAddress"
android:hint="@string/email" />
<EditText
android:id="@+id/etPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="5dp"
android:hint="@string/password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/login_label"
android:background="@color/colorAccent"
android:textColor="@android:color/white"
android:layout_marginTop="10dp"/>
</LinearLayout>
untuk skrip java nya silakan masukan kode berikut
package giviews.id.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 MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mShowDialog = (Button) findViewById(R.id.btnShowDialog);
mShowDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_login, null);
final EditText mEmail = (EditText) mView.findViewById(R.id.etEmail);
final EditText mPass = (EditText) mView.findViewById(R.id.etPass);
Button mLogin = (Button) mView.findViewById(R.id.btnLogin);
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mEmail.getText().toString().isEmpty() && !mPass.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this,
R.string.login_success,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,
R.string.login_error_msg,
Toast.LENGTH_SHORT).show();
}
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
}
}
Selanjutnya jalankan aplikasi anda, jika alert dialognya muncul maka berhasil, jika ada error silakan tulis pada kolom komentar dibawah. terimakasih sudah berkunjung ke blog ini.
0 Response to "Membuat Alert Dialog dengan Android Studio"
Post a Comment