how to code for custom dialog in android   

1 382 Question In | Android App
Tue Feb 5, 2019 Bittu

how can i show custom dialog in my android project 
						

firstly create a layout resource file 

custom_dialog_layout.xml

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

    
        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:text="I need a Nurse"
            android:padding="10dp"
            android:foreground="?attr/selectableItemBackground"
            android:layout_margin="5dp"/>

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:gravity="center"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:text="I am staff"
            android:padding="10dp"
            android:foreground="?attr/selectableItemBackground"
            android:layout_margin="5dp"/>

</LinearLayout>

then, write this method in your java code and use it.


public void showCustomDialog( Context context) {
        Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom_dialog_layout);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);


        btn= dialog.findViewById(R.id.btn);
        btn= dialog.findViewById(R.id.btn);

}

thank you.

Abhishek

33  Mon Feb 4,2019  Solved      


Subscribe & Get Regular Updates On Your E-mail