How to create a Custom Dialog box in android?
Answers (1)
Add Answer<?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" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_25sdp" android:layout_marginBottom="@dimen/_2sdp" android:paddingBottom="@dimen/_2sdp" app:cardBackgroundColor="@color/white" app:cardCornerRadius="@dimen/_10sdp" app:cardElevation="@dimen/_3sdp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_20sdp" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/doneTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_15sdp" android:fontFamily="@font/medium" android:text="@string/done" android:textColor="@color/black_violet" android:textSize="@dimen/_16sdp" /> <TextView android:id="@+id/savedSuccessfully" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_15sdp" android:layout_marginBottom="@dimen/_15sdp" android:fontFamily="@font/medium" android:text="@string/saved_successfully" android:textColor="#50000000" android:textSize="@dimen/_12sdp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#F1F5F8" android:orientation="horizontal" android:padding="@dimen/_10sdp"> <TextView android:id="@+id/fiveStar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="@dimen/_10sdp" android:layout_weight="1" android:background="@drawable/five_star" android:fontFamily="@font/medium" android:gravity="center" android:padding="@dimen/_7sdp" android:text="@string/five_star" android:textColor="#9BA9B9" android:textSize="@dimen/_14sdp" /> <TextView android:id="@+id/okay" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="@dimen/_10sdp" android:layout_weight="1" android:background="@drawable/ok_btn" android:fontFamily="@font/medium" android:gravity="center" android:padding="@dimen/_7sdp" android:text="@string/ok" android:textColor="#FFFFFF" android:textSize="@dimen/_14sdp" /> </LinearLayout> </LinearLayout> </androidx.cardview.widget.CardView> <ImageView android:layout_width="@dimen/_50sdp" android:layout_height="@dimen/_50sdp" android:layout_centerHorizontal="true" android:elevation="@dimen/_3sdp" android:src="@drawable/done_icon" /> </RelativeLayout> </FrameLayout>
In Java code as below:
Dialog dialog = new Dialog(this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); dialog.setCancelable(false); dialog.setContentView(R.layout.done_dialog); TextView doneTv = dialog.findViewById(R.id.doneTv); doneTv.setText(getLocaleStringResource(R.string.done)); TextView savedSuccessfully = dialog.findViewById(R.id.savedSuccessfully); savedSuccessfully.setText(getLocaleStringResource(R.string.saved_successfully)); TextView fiveStar = dialog.findViewById(R.id.fiveStar); fiveStar.setText(getLocaleStringResource(R.string.five_star)); TextView okay = dialog.findViewById(R.id.okay); okay.setText(getLocaleStringResource(R.string.ok)); fiveStar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyUtils.rateUs(OpenStatusActivity.this); dialog.dismiss(); } }); okay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { RewardedAds.showRewardedAds(OpenStatusActivity.this,null,false,false); } }); dialog.show();