Header Ads

Header ADS

Custom alert dialogue in Android studio Java.

                              Custom alert dialogue in Android studio Java. 


Hello friend welcome to CM coding lab website. Follow the below steps to create such dynamic level custom alert dialog.

Step_1: First of all make a project, And then go to activity_main.xml Then paste the following codes in your activity_main.xml
activity_main.xml 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/dialog_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog"
android:textAllCaps="false"
android:textSize="16sp"
/> 
</LinearLayout>

 

Step_2: Make a layout resource file, Then paste the following codes in your resource file.
Custom_Dising.XML
      <?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="280dp"
android:background="@drawable/ba_im"
android:orientation="vertical"
>

<ImageView
android:id="@+id/bt_cansel"
android:layout_width="36dp"
android:layout_height="36dp"
app:srcCompat="@drawable/baseline_dangerous_24"
android:scaleType="centerCrop"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>

<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="60dp"
app:srcCompat="@drawable/baseline_cloud_done_24"
android:scaleType="centerCrop"
/>

<TextView
android:id="@+id/Success"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Success"
android:textColor="@color/teal_700"
android:textSize="25sp"
android:padding="5dp"
android:textStyle="bold"
/>

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Congratulations! you have createed a custom dialog."
android:textColor="#009688"
android:textSize="18sp"
android:gravity="center"
android:textStyle="bold"
/>

<Button
android:id="@+id/btn_okay"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:text="Okay"
android:textSize="13dp"
android:background="@drawable/orange_button"
android:padding="8dp"
android:layout_marginTop="20dp"
/>
</LinearLayout>


</RelativeLayout>


Step_3: go to your MainActivity.java and Then paste the following codes in your MainActivity.java.
MainActivity.java
      
//===================================================================================       
//===Custom alert dialog code start here=============================================
//=========ShowDialog findViewById===========================
Button ShowDialog = findViewById(R.id.dialog_btn);

//Create the Dialog here
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.coustom_dilog);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.setCancelable(false);

Button Okay = dialog.findViewById(R.id.btn_okay);
ImageView bt_cansel = dialog.findViewById(R.id.bt_cansel);

//====bt_cansel start here==============================================
Okay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//==================================================
//code here as you want after clicking the Okay button.

Toast.makeText(MainActivity.this, "Yes Okay", Toast.LENGTH_SHORT).show();

//==================================================
dialog.dismiss();
}
});
//====bt_cansel end here==============================================

//====bt_cansel start here==============================================
bt_cansel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//==================================================
//code here as you want after clicking the Okay button.

Toast.makeText(MainActivity.this, "I am dangerous!", Toast.LENGTH_SHORT).show();

//==================================================
dialog.dismiss();
}
});
//====bt_cansel end here==============================================

//====ShowDialog start here==============================================
ShowDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Showing the dialog here
dialog.show();
}
});
//====ShowDialog end here==============================================
//===Custom alert dialog code end here=============================================
//===================================================================================
                                 


No comments

Custom alert dialogue in Android studio Java.

                               Custom alert dialogue in Android studio Java.   Hello friend welcome to CM coding l...

Theme images by Jason Morrow. Powered by Blogger.