We will learn how to create a simple Alert Dialog, which will show you a text box and a 1 button. For this we use the SetPositiveButton method, which rejects the dialog by clicking.....
How to create a simple AlertDialog?
Step 1. Create new empty android project in Android Studio and fill all require fields.
Step 2. Open activity_main.xml file into res->layout->activity_main.xml and copy & paste these below codes.
<?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.example.application.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="open dialog" />
</RelativeLayout>
Step 3. Open java folder and create new java class. Name it
ExampleDialog.java
package com.example.application;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
public class ExampleDialog extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Information")
.setMessage("This is a Dialog")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return builder.create();
}
}
Step 4. Now go to MainActivity.java class and copy & paste these below codes.
0 Comments