How to run AlertDialog or DialogFragment quickly? - java

I have a Fragment that does the following:
showProgressBar();
controller.calc();
setTextResult();
hiddenProgressBar();
My showProgressBar:
ProgressBar loadingCalc = new ProgressBar(requireContext(), null, android.R.attr.progressBarStyle);
loadingCalc.setId(View.generateViewId());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
loadingCalc.setLayoutParams(layoutParams);
alertDialog = new AlertDialog.Builder(requireContext(), R.style.Dialog)
.setView(loadingCalc)
.setCancelable(false)
.create();
alertDialog.show();
My hiddenProgressBar:
if (alertDialog != null) {
alertDialog.dismiss();
}
First the Fragment executes the showProgressBar, leaving the AlertDialog visible. Then it calculates and in the end executes the hiddenProgressBar. Visually it doesn't. When I run the application it first performs the calculation and at the end it shows the AlertDialogand quickly withdraws it.
What the code is doing:
Calculate > showing the AlertDialog> hiding the AlertDialog
What should happen:
Show AlertDialog > Calculate > Hide AlertDialog
Observation: I've already tried using DialogFragment but it has the same behavior as AlertDialog. I just want a Dialog with a ProgressBar while Android does a calculation that can take a while.
How can I solve this?

Related

Why is my showcaseview showing behind my bottomsheetdialog in android?

I am trying to show a showcase inside my bottomsheetdialog, as shown in my code below:
MainActivity.java:
report.setOnClickListener(v -> {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.bottom_dialog_layout);
EditText report_bill_type = dialog.findViewById(R.id.bll);
dialog.show();
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable((Color.TRANSPARENT)));
dialog.getWindow().setGravity(Gravity.BOTTOM);
new GuideView.Builder(v.getContext())
.setTitle("Title here")
.setContentText("Description here")
.setGravity(smartdevelop.ir.eram.showcaseviewlib.config.Gravity.auto)
.setTargetView(report_bill_type) //showcase keeps showing behind the dialog
.setDismissType(DismissType.anywhere)
.build()
.show();
});
When I execute the code, the showcase keeps showing behind the dialog. How do I resolve this issue?
I am using this showcase view.

How to display a messgae on top of each activity?

I am checking the internet connection in my application, when the internet is not accessible I want to display a small popup message to the user that you're offline.
I tried to solve the issue by using AlertDialog and AlertDialog.Builder, and I have also searched through different solution on the internet but no solution resolve my issue. I am trying to do this by the following method.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
View alertdialog = inflater.inflate(R.layout.nointernetdialogue, null);
builder.setView(alertdialog);
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams alertTop = dialog.getWindow().getAttributes();
alertTop.gravity = Gravity.TOP | Gravity.START;
alertTop.x = 100;
alertTop.y = 100;
dialog.show();
The result I want.
Create BaseActivity which shall implement Broadcast Receiver regarding network connectivity checks. Whenever network connectivity goes off, show the SnackBar/Alert.
Let all your other activities extend this Base activity.
Alert dialog looks like a over kill just have a TextView and show and hide it on network change.
Try with this example Click here
here i am using BroadcastReceiver to find the Wifi and Mobile data status
By this you can access the Network state anywhere un the App
You should create a BaseActivity that checks internet connection and shows alert dialog if needed. All activities that you want to show alert dialog should extended from BaseActivity.
You can work with top SnackBar , there is the code
Snackbar snack = Snackbar.make(findViewById(android.R.id.content), "Online", Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams(); params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snack.show();

'Running something in background inside a dialog

I have some codes like this:
ArrayList<Teacher> teachers = subs.get(position).getTeachers();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_layout, null);
builder.setView(v);
ListView listViewDialog = (ListView) v.findViewById(R.id.dialog_lv);
TesAdapter adapter1 = new TesAdapter(MainActivity.this, teachers);
listViewDialog.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listViewDialog.setDivider(null);
builder.show();
This operation takes a lot of time. So, I am trying to show a ProgressDialog while it is loading. then to hide the progressbar and show this AlertDialog.
I have tried these:
How to show progress dialog in Android?
How can I run code on a background thread on Android?
But none of them worked :( any solution?

Star Rating in Dialogbox won't change saved value

// Custom Dialog Box
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this, R.style.Theme_AppCompat_Dialog_Alert);
final View mView = getLayoutInflater().inflate(R.layout.completed, null);
ImageButton imgForm = (ImageButton) mView.findViewById(R.id.RateButton);
mBuilder.setCancelable(false);
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show(); // Dialogbox appears
// Interest Rating
final AlertDialog.Builder nBuilder = new AlertDialog.Builder( MainActivity.this, R.style.Theme_AppCompat_Light_Dialog_Alert);
final View nView = getLayoutInflater().inflate(R.layout.intrst, null);
Save_Intrst = (Button) nView.findViewById(R.id.SaveIntrst);
nBuilder.setCancelable(false);
nBuilder.setView(nView);
final AlertDialog dilog = nBuilder.create();
// LongPress Image Button
imgForm.setOnLongClickListener(new View.OnLongClickListener(){
#Override
public boolean onLongClick(View view){
dialog.dismiss();
dilog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dilog.show(); // Dialogbox appears
return true;
}
});
Save_Intrst.setOnClickListener(new View.OnClickListener(){
String IntrstLvl;
#Override
public void onClick(View v){
RatingBar rBar = (RatingBar)nView.findViewById(R.id.ratingStar);
IntrstLvl = Integer.toString(rBar.getNumStars());
addData(IntrstLvl);
dilog.dismiss();
Log.d(TAG,"Dismissed");
}
});
Whenever I select "save" within the Save_Intrst it saves 5 stars regardless of what I choose. I'm still fairly new to Android development and have been java coding for a bit now.
This is just a snippet of code of the project and I believe it will be enough, it shows my submit button, the submit button will launch a dialog box that will have a secret button in an image (ImgForm) the image doesn't show but that's not the problem, after long pressing it will launch another dialog that has a 5 Star Rating Bar and a Save button, this is used for rating after the person completes the previous requirements. The rating will always save "5" regardless of what was inserted, even after a reinstall of app onto the device.
getNumStars() will tell you the maximum number of stars shown and will always be 5 as you have defined it. If you want the actual selected rating, you will need getRating(). See this documentation.
You need to use the rBar.getRating()
RatingBar rBar = (RatingBar)nView.findViewById(R.id.ratingStar);
IntrstLvl = Integer.toString(rBar.getRating());
https://developer.android.com/reference/android/widget/RatingBar.html
You are calling getNumStars() which according to the documentation "Returns the number of stars shown." which means the total number of stars a user can select. You should instead be checking getRating() which returns the number of stars currently selected.
https://developer.android.com/reference/android/widget/RatingBar.html#getRating()

Android : screen width Dialog with adMob

I want to create dialog with adMob , but it always gives an error , that its not enough place to put it . so is it possible to make dialog width equal to screen width ?
here is how i create it all :
public void CreateDialog(){
Dialog d = new Dialog(this);
d.setTitle("Loading...");
LinearLayout ll = new LinearLayout(d.getContext());
ll.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
ll.addView(adView);
d.setContentView(ll);
d.show();
}
// AdView is loading normally , and working without dialog;

Categories

Resources