0

I have an Android app, I need to add to it an AlertDialog that shows when the app starts. Also, I need to add to the AlertDialog a button (like: visit website), when the user click that button, it will open the link and browse it in the browser or anything else.

How I can do that?!

Thanks in advance.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57

2 Answers2

2

Use an AlertDialog.Builder in your activities onCreate() method. You can use setPositiveButton() and launch your website in an intent, or webview when the user clicks it.

For example

    new AlertDialog.Builder(mContext)
        .setMessage("Launch Website")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Hide the dialog
                dialog.dismiss(); 

                // Launch the website
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));

                startActivity(intent);
            }
        })
        .show();

See http://developer.android.com/reference/android/app/AlertDialog.Builder.html

SteveEdson
  • 2,485
  • 2
  • 28
  • 46
0

First you have to add an AlertDialog to your onCreate() method in your Activity. After then You have to add a button to that AlertDialog. There are three types of button in AlertDialog.

  1. Positive

  2. Negative

  3. Neutral

Use any one of them.

Then when the button clicked you need to go to the website url, in some browser in the android device.

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.your-web-site-url.com"));
startActivity(browserIntent);

Try this:

final Context context = this;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

// set title
alertDialogBuilder.setTitle("Your Title");

// set dialog message
alertDialogBuilder
    .setMessage("Click to visit website!")
    .setCancelable(false)
    .setPositiveButton("Go to web site",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            // if this button is clicked

              Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.your-web-site-url.com"));
              startActivity(browserIntent);

        }
    }));

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
  • Thanks! your answer helped me. Can I add a check box (like: Do not show this again)?! If I can, what I need for it?! –  Aug 21 '15 at 14:20
  • 1
    Welcome. Yes you can add a check box or anything you want. just create a layout add a check box in that layout, and then add the check box to the AlertDialog using setView(your_check_box_view). for example see this : http://stackoverflow.com/questions/9763643/how-to-add-a-check-box-to-an-alert-dialog – Md. Nasir Uddin Bhuiyan Aug 21 '15 at 14:31
  • How I can save the check box to shared preferences if the user check it?! Thanks, your answer was helpful. –  Aug 21 '15 at 16:13
  • 1
    See this : http://stackoverflow.com/questions/23024831/android-shared-preferences-example – Md. Nasir Uddin Bhuiyan Aug 21 '15 at 16:27