What is a Dialog in Android?
Dialog-
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for model events that require users to take an action before they can proceed.
When are we creating our application by using Data Binding then how we use Data Binding in a custom dialog?
We just follow three steps to implement databinding in Dialog and attach our model to dialog Layout.
- Create a custom dialog in Activity or Fragment via new keyword.
12Dialog dialog = new Dialog(mContext);dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
here I am creating a dialog and set its window feature without the title. - Add the binding layout to this Dialog by using DataBindingUtil.
12reviewBinding = (CustomReviewDialogBinding)DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.custom_review_dialog, null, false);dialog.setContentView(reviewBinding.getRoot());
- and After that, We can add our Data Models in Dialog
12345productReview = new ProductReviewDialog();reviewBinding.setType(type);reviewBinding.setHandler(this);reviewBinding.setProductReview(productReview);dialog.show();
- Now here is the full code to create a custom Dialog-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748public class AllReviewHandler {private final Context mContext;private final int productId;private String currentRating, type;private CustomReviewDialogBinding reviewBinding;private ProductReviewDialog productReview;// modelDialog dialog;private SweetAlertDialogUtil progressDialog;public AllReviewHandler(Context context, int productId){mContext = context;this.productId = productId;}public void onClickAddReview(View view, String type){dialog = new Dialog(mContext);dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);reviewBinding = (CustomReviewDialogBinding)DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.custom_review_dialog, null, false);dialog.setContentView(reviewBinding.getRoot());productReview = new ProductReviewDialog();reviewBinding.setType(type);reviewBinding.setHandler(this);reviewBinding.setProductReview(productReview);dialog.show();}public void onClickCancle(View v){dialog.dismiss();}public void onClickSubmit(View v, String type){boolean isFormValidate = true;if(productReview.getCustomerName() == null || productReview.getCustomerName().isEmpty()) {productReview.setCustomerNameError(true);isFormValidate = false;}if(!type.equalsIgnoreCase("R") && (productReview.getComment()== null || productReview.getComment().isEmpty())) {productReview.setCommentErro(true);isFormValidate = false;}if(isFormValidate){progressDialog = new SweetAlertDialogUtil(mContext);progressDialog.loading(mContext.getString(R.string.please_wait));submitYourReview();}}}
Dismissing Custom Dialog-
The system also dismisses the dialog when the user touches an item in a dialog list, except when the list uses radio buttons or checkboxes. Otherwise, We can manually dismiss our dialog by calling dismiss();
We can also dismiss our Custom Dialog as normally we dismiss without DataBinding.
1 |
dialog.dismiss(); |