'Running something in background inside a dialog - java

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?

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 run AlertDialog or DialogFragment quickly?

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?

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();

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()

Need to show a popup/Dialog on different activities

I have an app that uses three Activities. I've created in the first one a Thread that checks the connection with the server and when the app cant reach the server it shows a Popup.
The thing is that when I go from the Activity1 to the Activity2 and I lose the connection, I'm getting a WindowManager$BadTokenException.
I've tried with PopupWindow and with AlertDialog but I have the same problem, I can't give them the current Activity.
Alert Dialog:
AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());
builder1.setMessage("Se ha hecho el cierre diario, es necesario reiniciar la aplicaciĆ³n.");
builder1.setCancelable(true);
builder1.setPositiveButton("Ok",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
java.lang.System.exit(0);
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
PopupWindow:
final Activity context = Activity_Start.this;
final boolean Reset = reset;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup_mensaje_error);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popup_error = layoutInflater.inflate(R.layout.mensaje_error, viewGroup, false);
// Creating the PopupWindow
final PopupWindow popupW_error = new PopupWindow(context);
popupW_error.setContentView(popup_error);
In both cases I have the same error and I'm almost 100% sure that is cause getApplicationContext() is no enough to get what the app needs.
Can someone help me? Thanks!!
Android documents suggests to use getApplicationContext();
but it will not work instead of that use your current activity while instantiating AlertDialog.Builder or AlertDialog or Dialog...
Try
AlertDialog.Builder builder1 = new AlertDialog.Builder(Activity_Start.this);
instead of
AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());
And also use
final Context context = Activity_Start.this;
instead of
final Activity context = Activity_Start.this;
The dialog is tied to this particular activity, and not the entire application. Generally use Activity.this when creating objects whose scope is just the activity. Use Application context when you are creating objects whose scope is beyond the current activity.
Hope this helps!

Categories

Resources