STEP 1: Create xml dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:padding="20sp"
android:background="@color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tv_select_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="@string/exit_text"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="@+id/card_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:foreground="@drawable/ripple2"
app:cardCornerRadius="16sp"
tools:ignore="UnusedAttribute">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="12dp"
android:text="@string/cancel"
android:textColor="@color/white"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/card_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:foreground="@drawable/ripple2"
app:cardCornerRadius="16sp"
tools:ignore="UnusedAttribute">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="12dp"
android:text="@string/exit"
android:textColor="@color/white"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
STEP 1: Create fragment (java)
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import com.developerbrothers.dailynews.Activity.MainActivity;
import com.developerbrothers.dailynews.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import java.util.Objects;
public class BottomSheetFragment extends BottomSheetDialogFragment {
public static BottomSheetFragment newInstance() {
BottomSheetFragment fragment = new BottomSheetFragment();
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void setupDialog(Dialog dialog, int style) {
View contentView = View.inflate(getContext(), R.layout.bottom_sheet_exit, null);
TextView tv_select_address = contentView.findViewById(R.id.tv_select_address);
CardView card_cancel = contentView.findViewById(R.id.card_cancel);
CardView card_exit = contentView.findViewById(R.id.card_exit);
card_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
card_exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity. fa.finish();
}
});
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(contentView);
((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.white));
}
}
STEP 3: Call
private void showExitDialog() {
BottomSheetFragment bottomSheetDialog = BottomSheetFragment.newInstance();
bottomSheetDialog.show(getSupportFragmentManager(), "Exit Dialog");
}