OnBackPressed dialog in Android Studio Code

Arah Lab
0



  OnBackPressed dialog - XML CODE
Android Studio Java Code Box with Copy Button

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    >


        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="230dp"
            android:clickable="true"
            android:foreground="?attr/selectableItemBackground"
            app:cardBackgroundColor="#FFFFFF"
            app:cardCornerRadius="12dp"
            app:cardElevation="10dp"
            android:layout_centerInParent="true"
            android:layout_margin="10dp"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >


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

                    <com.airbnb.lottie.LottieAnimationView
                        android:id="@+id/LottiPrime1"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        app:lottie_autoPlay="true"
                        android:layout_gravity="center"
                        app:lottie_loop="true"
                        app:lottie_rawRes="@raw/notification"
                        android:visibility="visible"
                        android:scaleType="centerCrop"
                        android:layout_marginTop="5dp"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:gravity="center"
                        android:textSize="20sp"
                        android:text="আপনি কি নিশ্চিত রোমান্টিক ছন্দ অ্যাপ থেকে বেরিয়ে যেতে চান?"
                        android:layout_marginTop="10dp"
                        android:fontFamily="@font/fond2"
                        />


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="2"
                    android:layout_alignParentBottom="true"
                    >

                    <androidx.cardview.widget.CardView
                        android:id="@+id/no"
                        android:layout_width="match_parent"
                        android:layout_height="45dp"
                        app:cardBackgroundColor="#FF0000"
                        app:cardCornerRadius="30dp"
                        app:cardElevation="8dp"
                        android:layout_margin="7dp"
                        android:layout_weight="1"
                        android:clickable="true"
                        android:foreground="?attr/selectableItemBackground"
                        >

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:textColor="@color/white"
                            android:textSize="25sp"
                            android:text="না"
                            android:gravity="center"
                            android:layout_marginLeft="20dp"
                            android:layout_marginRight="45dp"
                            />

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="20dp"
                            android:src="@drawable/cancel"
                            android:layout_gravity="center"
                            android:layout_marginLeft="20dp"
                            />

                    </androidx.cardview.widget.CardView>

                    <androidx.cardview.widget.CardView
                        android:id="@+id/yes"
                        android:layout_width="match_parent"
                        android:layout_height="45dp"
                        app:cardBackgroundColor="#002BFF"
                        app:cardCornerRadius="30dp"
                        app:cardElevation="8dp"
                        android:layout_margin="7dp"
                        android:layout_weight="1"
                        android:clickable="true"
                        android:foreground="?attr/selectableItemBackground"
                        >

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:textColor="@color/white"
                                android:textSize="25sp"
                                android:text="হ্যাঁ"
                                android:gravity="center"
                                android:layout_marginLeft="20dp"
                                android:layout_marginRight="45dp"
                                />

                            <ImageView
                                android:layout_width="match_parent"
                                android:layout_height="30dp"
                                android:src="@drawable/exit"
                                android:layout_gravity="center"
                                android:layout_marginLeft="20dp"
                                />



                    </androidx.cardview.widget.CardView>


                </LinearLayout>

            </RelativeLayout>



        </androidx.cardview.widget.CardView>



</RelativeLayout>

        




  OnBackPressed dialog - JAVA CODE
Android Studio Java Code Box with Copy Button

@Override
    public void onBackPressed() {


        final AlertDialog.Builder alert = new AlertDialog.Builder(this);

        View myView = getLayoutInflater().inflate(R.layout.alertbox, null);
        alert.setView(myView);

        final AlertDialog dialog = alert.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setCancelable(false);

        myView.findViewById(R.id.yes).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finishAndRemoveTask();
            }
        });


        myView.findViewById(R.id.no).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

    }

        



Tags

Post a Comment

0Comments

Post a Comment (0)