1

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:

  1. what's "best practice" for displaying a list to the user and having him select one?
  2. is doing this in a Dialog "best practice"?

Thanks.

Community
  • 1
  • 1
Aerik
  • 2,307
  • 1
  • 27
  • 39

3 Answers3

3

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.

Zsombor Erdődy-Nagy
  • 16,864
  • 16
  • 76
  • 101
3

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

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
2

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);
codeskraps
  • 1,461
  • 2
  • 12
  • 13