How to create custom dialog box with two button in android?

Reena picture Reena · Apr 22, 2016 · Viewed 19.6k times · Source

How to generate custom dialog box in android like this, enter image description here

I want just like this. How to generate it. please give me suggestion.

i have used below code for dialog box, what is the problem in my code? I have not identify it. please share me any Idea.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <RelativeLayout
        android:id="@+id/rl_quit_learning"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@drawable/btn_white"
        android:paddingBottom="@dimen/thirty_dp"
        android:paddingLeft="@dimen/ten_dp"
        android:paddingRight="@dimen/ten_dp"
        android:paddingTop="@dimen/ten_dp">

        <TextView
            android:id="@+id/tv_quit_learning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="@dimen/ten_dp"
            android:text="Quit LEarning?"
            android:textSize="@dimen/twenty_sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_quit_learning"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="@dimen/ten_dp"
            android:layout_marginRight="@dimen/ten_dp"
            android:layout_marginTop="@dimen/twenty_dp"
            android:gravity="center"
            android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?"
            android:textSize="@dimen/sixteen_sp" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="125dp"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel" />

        <Button
            android:id="@+id/btn_video"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Video" />

    </LinearLayout>
</RelativeLayout>

Please share me any Idea. Thanks.

Answer

Agilanbu picture Agilanbu · Nov 14, 2018

Simple, 1st need to create an newcustom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="250dp"
    android:layout_gravity="center"
    android:layout_marginLeft="55dp"
    android:layout_marginRight="55dp"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="165dp"
            android:layout_gravity="center"
            android:layout_marginEnd="60dp"
            android:layout_marginStart="60dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="15dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="14dp"
                android:gravity="center"
                android:text="Quit Earning?"
                android:textColor="@android:color/black"
                android:textSize="20dp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="20dp"
                android:layout_marginStart="20dp"
                android:gravity="center"
                android:text="You are 400pts. away from \n unlocking rewards. quit Earning?"
                android:textSize="18dp"
                android:textStyle="bold" />

        </android.support.v7.widget.CardView>
    </LinearLayout>

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/frmNo"
        android:layout_marginRight="45dp"
        android:layout_marginTop="75dp">

        <android.support.design.widget.FloatingActionButton
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@android:color/transparent"
            app:backgroundTint="@color/fab2" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:elevation="6dp"
            android:text="No"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/white"
            android:textStyle="bold" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/frmOk"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="75dp">

        <android.support.design.widget.FloatingActionButton
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@android:color/transparent"
            app:backgroundTint="@color/fab1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:elevation="6dp"
            android:text="Ok"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/white"
            android:textStyle="bold" />
    </FrameLayout>
</FrameLayout>

Then, in java file (in activity) paste this code

 public class ViewDialog {

    public void showDialog(Activity activity) {
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.newcustom_layout);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        FrameLayout mDialogNo = dialog.findViewById(R.id.frmNo);
        mDialogNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Cancel" ,Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

        FrameLayout mDialogOk = dialog.findViewById(R.id.frmOk);
        mDialogOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Okay" ,Toast.LENGTH_SHORT).show();
                dialog.cancel();
            }
        });

        dialog.show();
    }
}

Finally you can call it wherever you want.

    ViewDialog alert = new ViewDialog();
    alert.showDialog(CustomDialogActivity.this);

enter image description here