According this answer , I wanna to show dilaog until the next 10 posts is loaded so I created the static alertDialog method to use it in different places in my app, but the problem is the dialog doesn't cancel or dismiss
setProgressDialog in Utils class
public static AlertDialog setProgressDialog(Context context) {
int llPadding = 30;
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setPadding(llPadding, llPadding, llPadding, llPadding);
ll.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
ll.setLayoutParams(llParam);
ProgressBar progressBar = new ProgressBar(context);
progressBar.setIndeterminate(true);
progressBar.setPadding(0, 0, llPadding, 0);
progressBar.setLayoutParams(llParam);
llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
TextView tvText = new TextView(context);
tvText.setText("Loading ...");
tvText.setTextColor(context.getResources().getColor(R.color.black));
tvText.setTextSize(20);
tvText.setLayoutParams(llParam);
ll.addView(progressBar);
ll.addView(tvText);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setView(ll);
AlertDialog dialog = builder.create();
dialog.show();
Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(layoutParams);
}
return dialog;
}
then I used it in HomeFragment on click load more button
binding.loadMoreBtn.setOnClickListener(view -> {
Utils.setProgressDialog(requireContext());
if (Utils.hasNetworkAccess(requireContext())) {
postViewModel.getPosts();
// Log.w(TAG, "loadMoreBtn: "+dialog.isShowing() );
} else {
postViewModel.getAllItemsFromDataBase.getValue();
}
Utils.setProgressDialog(requireContext()).cancel();
Utils.setProgressDialog(requireContext()).dismiss();
});
PS: I tried also to create dialog AlertDialog dialog = Utils.setProgressDialog(requireContext());
instead to call the method directly then dialod.show()
, and after the getPosts
cancel or dismiss it but it dosen't appear