12/08/2018, 15:13

Create Dialog in Android

Create Dialog View Create XML file dialog_view.xml, for example: < LinearLayout xmlns: android = " http://schemas.android.com/apk/res/android " android: layout_awidth = " match_parent " android: layout_height = " match_parent " android: orientation = ...

Create Dialog View

Create XML file dialog_view.xml, for example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_awidth="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">
    <EditText
        android:id="@+id/et_table_name"
        android:layout_awidth="match_parent"
        android:layout_height="50dp" />
    <LinearLayout
        android:layout_awidth="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/bt_create"
            android:layout_awidth="100dp"
            android:layout_height="match_parent"
            android:text="Create"/>
        <Button
            android:id="@+id/bt_cancel"
            android:layout_awidth="100dp"
            android:layout_height="match_parent"
            android:text="Cancel"/>
    </LinearLayout>
</LinearLayout>

Set in file.java

Create Dialog Object

Dialog dialog = new Dialog(MainActitvity.this);

The supplied {MainActivity.this} is used to obtain the window manager and base theme used to present the dialog.

Set view for dialog

dialog.setContentView(R.layout.dialog_view);

Set title for dialog

dialog.setTitle("My dialog");

Set components of dialog

private Button creatTable;
private Button cancelDialog;
private EditText etTableName;

etTableName = (EditText) dialog.findViewById(R.id.et_table_name);
creatTable = (Button) dialog.findViewById(R.id.bt_create);
cancelDialog = (Button) dialog.findViewById(R.id.bt_cancel);

Tutoriol Video: https://youtu.be/vgdzOK6XYDc

0