Best Practice for Dialog for Selecting a Bookmark - android

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

Related

Build dialog one after one in a loop

I have this function where I need to return a list depending of what user pressed in Alert Dialog (cancel or save).
But I have an issue, let's imagine we have a list with a size of 10. Then on the iteration of that list it will build 10 alert dialogs at the same time plus a dark black shadow at the background caused by these.
So I'd like to "pause" until user pressed or find a way to don't pop up all these alert dialog at the same time and just appear one by one once pressed a button.
A quick reminder: I need to return a list after all dialogs have been pressed.
Question: How could I do that?
It would be better if you provided some code with this. Anyway, even though this is not something I would do and create 10 dialogs in the for loop, this can be done.
Just create a Boolean inside your for loop which will be used to check if the dialog is dismissed.
for(int i = 0; i < list.size(); ++i) {
Boolean isDismissed = false;
AlertDialog d = new AlertDialog(getBaseContext());
d.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
isDismissed = true;
}
});
//start your dialog
while(!isDismissed) {
//do nothing
}
}
As I said, I wouldn't do this.
Because I evaluate first a list of items, then I set on a new list of items that will require user to confirm about what to do with that, so I loop that with alert dialogs waiting for user to tell me what to do with those items
There is a much better way to do this. Why not starting one CustomDialog which will ask the user what to do with those items. He could choose options for each item with a spinner or if options are KEEP or DELETE just use checkbox or something.
So as people said, creating Alert Dialogs in a loop is a bad practice so my solution into this is just setting a view on Fragment that acts like a Dialog but I just turn it visible and gone whenever I need. This seems a proper solution for my case.
When user accept or cancel the view (clicking on button) just send it to the viewmodel and the viewmodel will evaluate if there are still items on the list. If there are items then show again this "view" on Fragment asking to user what to do :)
I don't have code to show because I haven't done it yet but I have thought for a while and this is the best I can think about. Hope it helps for someone who is in the same situation!

Android dialog setNegativeButton without listener

Curiosity question here.
I use a lot of dialogs builders and most of the time my negative cancel button do nothing except dismiss the dialog. The code I found everywhere on the web is this :
builder.setNegativeButton(
"cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
I happened to find out that this code do exactly the same :
builder.setNegativeButton("cancel", null);
So my question is then : is that a bad habit not to manually dismiss the dialog, and if yes why ?
It is somewhat documented behaviour, see:
http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
When the user touches any of the action buttons created with an AlertDialog.Builder, the system dismisses the dialog for you.
So with null listener you exercise this implicit documented behaviour of Dialog.
What can go possibly wrong? (yeah, I think there's at least 50% chance that some custom ROM out there is not behaving properly... then again, who cares about custom ROMs failing to follow documented behavior, I don't any more, too much of that BS).
I learned this myself in an Android course in school. Basically, you only need to implement the button listener if you need additional functionality.
So it is not "habit" to include the click listener, it is just clear intent.

What is the best way to implement two custom dialogs, one after the other?

through the help of various tutorials, I've managed to write a custom dialog that displays a listview populated by records from a local database. I have set click listeners and figured out how to retrieve the record at the listview item clicked by setting the cursor at the position returned and so on...Now what I want to do is, dismiss this dialog when an item is clicked, and automatically open a new dialog with this cursor's content as the name of the table from which to re-populate the new listview. I'd like to know if anyone knows the best way of doing this in terms of application structure.
Currently, I am calling my dialog to show in my Activity like this:
public void onClick(View view) {
switch(view.getId()) {
case R.id.pickerbutton:
showDialog(DIALOG_PICK_CATEGORY);
break;
}
}
protected Dialog onCreateDialog(int id) {
dialog = null;
switch(id) {
case DIALOG_PICK_CATEGORY:
CustomDialogList.Builder customBuilder = new
CustomDialogList.Builder(SendCookieActivity.this);
customBuilder.setTitle(R.string.category);
dialog = customBuilder.create();
break;
}
return dialog;
}
After this dialog is shown, the user picks a category from the CustomDialogList dialog. I am having a hard time thinking of how to make it so that after the category is picked, this dialog is dismissed (or looks like it's dismissed) and the same one with newly populated items appears (or can be a completely new dialog too). and when someone presses the back button, the previous dialog is shown. Think of it as a file explorer but with only two levels of depth. I'd like to keep using my CustomDialogList because I have customized its look to match everything else in my app. Perhaps what would help me with this problem besides or instead of code, would be some diagrams of how this type of UI flow has been implemented before along with some pseudo code.
Thanks.
Assuming your custom dialog extends Dialog (or one its sub-classes). Have your Activity implement DialogInterface.onDismissListener. Then after you create the dialog with...
dialog = customBuilder.create();
...use dialog.setOnDismissListener(this); before you show it.
Your Activity will have to implement...
#Override
public void onDismiss(DialogInterface dialog) {
// Identify which dialog was dismissed and do something
}
When I have had to do this in the past, I have the onCancel for the dialog open the new dialog.

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.

Android Class which extends Dialog, how to clear TextViews before it is displayed

I have a class that extends android.app.Dialog, the layout is done in an xml file, and the setup (button listeners, etc) is done on the onCreate method. My problem is that whenever the dialog is displayed, then dismissed, and displayed again, the Editable TextViews are still populated with the information that was displayed previously. What is the common way to clear these text fields? Remember - this is a separate class that extends Dialog - so there is no 'onDialogCreate' like Activity has.
Or, perhaps I am extending the wrong class? There is just a lot of processing being done, and do not want to have all the code in the main Activity. I would like it to be in a separate Class. I tried to extend AlertDialog, but it does not create the border like Dialog does. Any help would be great.
The dialog is shown via the Activity:
protected Dialog onCreateDialog(int id) {
switch(id){
case DIALOG_NEW_SAFE:
return(new NewSafeDialog(this));
default:
return(null);
}
}
onCreateDialog(..) caches the dialog which means the same instance is reused.
3 ways to fix the undesired behavior off my head:
Override onPrepareDialog(..), use findViewById(..) to get whatever you want to clear, clear it.
Don't rely on managed dialogs at all, do new NewSafeDialog(this).show() each time you want to show the dialog.
Add onCancelListener(..), onDismissListener(..) inside your custom dialog that would call a method to clear itself.
The good way to create a dialog is by using showDialog() as you did so don't change it.
The good and easy way to force deletion of a dialog in order to make your creation code recalled again is:
void removeDialog (int id)
So if you simply do the following, it's gonna work ;)
removeDialog(DIALOG_NEW_SAFE);
showDialog(DIALOG_NEW_SAFE);
Try clearing the text in the constructor of the NewSafeDialog i.e. your dialog class.

Categories

Resources