Android: Show AlertDialog inside Fragment? - android

I want to show an AlertDialog inside an AsyncTask inside a Fragment, but just so that the AlertDialog is inside that Fragment, and not "blocking" the whole application, so that I can go through my tabs and on one fragment there is a AlertDialog blocking that Fragment.
progressDialog = ProgressDialog.show(getActivity(), "Please wait...", "Fetching data", true);

ProgressDialogs are generally frowned upon by the Android Design Guidelines and in your case they are definitely problematic due to their modal nature. It would be preferable to include a ProgressBar component in each of your fragments and show / hide (from your async task).
http://www.yogeshblogspot.com/android-progress-bar-indeterminate/

Related

How to show progress dialog on home activity android?

I have a bottom navigation view, and this view has 3 items 3 different fragments.
In my one fragment I am uploading a video file to server.
My question is :
When upload start I want to show uploading progress from my main activity like instagram doing.
How can do this ? any advice please
ProgressDialog pd=new ProgressDialog(context);
pd.setMessage("Your Message");
pd.show(); //Where you want to show PD
pd.dismiss();// where you want to Dismiss PD

Make android toast not cancellable

I am showing a toast in my app. The problem is that in some devices (Samsung galaxy s6) the toast is cancelled when touching the screen. This problem doesn't happens in other devices (Nexus 5)
This is my code
LayoutInflater li = getLayoutInflater();
View layout = li.inflate(R.layout.popup_tutorial_privado, (ViewGroup)findViewById(R.id.popup));
toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Take a step back
Avoid using Toast altogether if you need more control and use a Dialog that dismisses itself after n time. You could write a method as simple as this one, that would produce something functionally equivalent to a Toast but with the added freedom of controlling when and how it's dismissed.
public void customToast(String message, int duration){
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_toast);
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.setCancelable(false);
//Customize the views, add actions, whatever
((TextView)dialog.findViewById(R.id.message)).setText(message);
dialog.show();
//Auto cancel the dialog after `duration`
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
dialog.cancel();
}
},duration);
}
Demo
Note
If you want the dialog to be shown for the exact amount of time a long toast lasts, use 3500 since private static final int LONG_DELAY = 3500;
But wait! The dialog takes focus and I need to retain it!
Ok, ok, you may be writing inside an EditText and the Dialog acting like a Toast takes control of your focus and your keyboard hides and everything is lost. To prevent this, simply set an extra flag, that will tell the Dialog that it's not focusable, and that it should not attempt to request it.
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
Keep in mind that if you're showing the Toast by watching text changes in an EditText you should keep a flag of some sort, to know whether it's being shown, or is already shown, or whatever, otherwise you'll end up with multiple dialogs.
A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive.
the toast does not interact with user, it just like "comes and go" you can not set it cancelable true or false.
you should use Dialogs to achieve what you want.
As suggested by the documentation:
A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.
For the behavior your explain, you do use snackbar with infinite time
Snackbar documentation

Best Practice for Dialog for Selecting a Bookmark

I haven't written any code for this yet, but I've been researching how to implement bookmarks in my custom web browser. From what I've read, I believe the way to go is to show the user a dialog (I saw this article on how to return the value from the dialog) containing, I think, a ListView for the bookmarks... I'm honestly stuck at something pretty simple - how to present the bookmarks to the user and select one.
So, where my questions:
what's "best practice" for displaying a list to the user and having
him select one?
is doing this in a Dialog "best practice"?
Thanks.
The classic way to do this (pre 3.0) is to use simple Dialogs, that you manage with your current Activity. The easiest way to go is to use the AlertDialogBuilder to build the dialog, see here, around the middle, the "Adding a list" section. This way you get a Dialog with a list, and the user can select exactly one entry from that list.
Nowadays however, you should be using DialogFragments, with the (not so) new Fragment framework. You can use the official compatibility lib to make fragments work on older Android builds. In a DialogFragment, you can either show any UI layout you want if you override the onCreateView(...) callback, or you can define the looks and behavior by using the "onCreateDialog(...)" callback (you can use AlertDialogBuilder here, too). See the link for examples.
The DialogFragment based solution is more self-contained and you can easily call/show it from any place in your Application.
And yes, I do think that a single-select list-based dialog can be considered the "best practice" in this kind of situation. However, the other advantage of the DialogFragment based solution is, that you're not forced to show it in Dialog-style, you can also embed it into an Activity's layout as a standard fragment if that's what you want.
Best is the to show a dialog with a list and upon user selection navigate to either browser or webview.
you can use the below code to present a dialog to user::
String[] yourarraylist = new String[]{"A","B","C","D","E","F","G"};
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setTitle("title");
builder.setSingleChoiceItems(yourarraylist, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), yourarraylist[item], Toast.LENGTH_SHORT).show();
//launch web browser or webview
alert.dismiss();
}
});
alert = builder.create();
alert.show();
you can launch web browser as below::
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
Also for opening the url in webview you can refer my blog at this LINK
I would personally use another activity to display the bookmarks rather than using a dialog. The user could have a large number of bookmarks saved and a dialog I don't think would be the best way.
I would make another activty which extends ListView and display the bookmarks on a list or even better a gridview with the thumbnail of the bookmark. Then by clicking a bookmark will return to the main activity refreshing the browser with the selected bookmark.
No need for dialogs, unless you like to create a longpressclick or context menu to show a dialog for deleting/editing/.. bookmarks.
The alertdialog.builder is the quickest and easiest way to build dialog. As another tip to build dialogs with alertdialog.builder. The builder has a setview to give it any view you want to the dialog, this view can basically be a linearLayout with lost more view already in it creating a complex dialog view.
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle(getResources().getString(R.string.dialog_title));
ad.setView(dialogLayout);

Android DialogFragment progress bar

I've searched everywhere and I can't find a solution to this problem.
Basically I have a login screen and I'm trying to get a progress spinner to show up while it's logging in to the server (via a thread), and then dismiss it after the login is successful. It has to work while changing orientations.
I am using DialogFragment with the Android compatibility package to make a progress bar (can't find any documentation on it, only for basic\alert dialog) because showDialog() is deprecated now. Right now I just show a custom message box as a login spinner.
In Summary:
How can I set up a Progress spinner with DialogFragment.
How can I dismiss it in another thread after orientation changes.
For showing a progress spinner, just override DialogFragment.onCreateDialog() in your dialog fragment like this (no need for overriding onCreateView()):
#Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final ProgressDialog dialog = new ProgressDialog(getActivity());
//
dialog.setTitle(R.string.login_title);
dialog.setMessage(getString(R.string.login_message));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
// etc...
return dialog;
}
As for dismissing that dialog fragment from somewhere else, you'll need to get a hold of FragmentManager (from inside your next FragmentActivity or Fragment) and call popBackStack() on it (if you don't do any other fragment transaction in the meantime).
If there's more steps/fragment transactions between your progress dialog fragment and the next activity, you'll probably need one of the other popBackStack(...) methods that take an ID or tag to pop everything up to your progress dialog fragment off the stack.
I know this is old question but I want to share much better solution for this
According to Android Development Protip:
"Stop using ProgressDialog,Inline indicators are your friend"
As Roman Nurik states:
This one's quick. Stop using ProgressDialog and other modal loading
indicators. They're extremely interruptive and annoying, especially
when:
You see one every time you switch tabs.
You can't Back out of them.
They say "Please wait." No thanks, I'd rather just uninstall.
Either show loading indicators inline with your content (e.g.
http://developer.android.com/training/animation/crossfade.html) or better yet, load small amounts of data in the
background so that you minimize the need to even show a loading
indicator.
More about progress & activity in the design guidelines.

ProgressDialog in TabHost application ofandroid

I want to use the Progress Dailog in my application. I am facing one issue in doing it, after some RnD I came to know that it is not quite possible to create the progress Dialog I have the Activity Group Class for the TabHost in the application.
I have exactly the same scenario, I have the TabHost in my application and an ActivityGroup Class that has the TabHost Classes. So, when I try to create the Progress Dialog for the Class that is in the Activity Group Class I cannot create it. But if I try to create the Progress Dialog for the Class that is not in the Activity Group I can create it with no issues.
Is there any solutions now?
I think the problem is with context of the progress dialog
Try giving the context of the dialog as getParent()
ProgressDialog.show(getParent(), " Loading...", "Please wait...", true, false);

Categories

Resources