Configuring the AlertDialog5:58 with Ken Alger
We can configure an Alert Dialog by chaining a few key methods together and then building it.
We're off to a great start with our alert dialog, so 0:00 that we can notify users if an error occurs. 0:02 We need to continue on with it and configure the dialog and then return it. 0:06 The title that appears at the top of the dialog is set, 0:10 using the builder.setTitle method. 0:13 So under here, builder Set title. 0:16 Just do Oops! 0:23 Sorry. 0:26 We can next add a detailed message. 0:29 We could add another line calling the builder.setMessage method. 0:32 Or we can chain methods together. 0:37 We've seen method chaining before with toast messages. 0:40 I find it allows for code to be easier to read and understand. 0:44 So let's do that here. 0:48 Before the semi colon then, hit Enter, And 0:50 type, .setMessage. 0:56 And our message will be, there was an error. 1:03 Please try again. 1:07 Finally, we need to add buttons to the bottom of the dialogue. 1:11 We can add one, two, or three of them. 1:15 The alert dialogue has three different buttons we can set, positive, 1:17 negative, and neutral. 1:22 For each button, we set the text and 1:24 the onClick listener which runs code when the button is tapped on. 1:26 For this dialogue, we just want one positive button labeled OK. 1:31 So let's chain that to our builder. 1:36 SetPositiveButton. 1:39 And we'll pass in null for OnClickListener. 1:45 So we can use null like this if we don't want to do anything specific when 1:49 the button is tapped. 1:53 A null, OnClickListener, will just close the dialog, which is all we need here. 1:54 However, we could add an OnClickListener just like a regular button. 1:59 With our dialogue defined using the builder, we need to create and return it. 2:05 We create an actual alert dialogue object by using our builder's create method. 2:09 And we can return it directly. 2:14 Return builder.create. 2:18 While we're in our AlertDialogFragment file, 2:23 there's one more thing we should do in here. 2:26 To follow Android best practices, 2:28 we should use string resources wherever possible in our apps. 2:30 We've seen them a little in previous courses and 2:34 it's great to use them wherever we can. 2:36 Fortunately, Android Studio makes this easy with a convenient shortcut. 2:39 Place your cursor in the title string and hit Alt+Enter for the quick fix. 2:43 To extract string resource, and we'll do error_title. 2:50 We can do the same for message. 2:59 Alt+Enter, extract string resource, 3:04 [INAUDIBLE] error_message. 3:08 In here, same thing, and 3:12 we'll call it error_button_ok_text. 3:16 So we're getting an error here, let's just try rebuilding the project. 3:29 And the errors go away, great. 3:36 Now we need to use this new object in our activity. 3:38 Let's head back to our new alertUserAboutError method and 3:40 put it to work. 3:44 So back here in MainActivity, and alertUserAboutError. 3:45 Let's create a new AlertDialogFragment. 3:50 We'll call it dialog and say new AlertDialogFragment. 3:55 Now we need to show it in our activity. 4:03 We can do that with dialog.show. 4:05 This takes a couple of parameters, the first being a fragment manager. 4:07 Fortunately, the activity has a method for 4:11 this called getFragmentManager, which we can pass in. 4:13 The second parameter is a tag that is just a string. 4:17 We can put in pretty much whatever we want. 4:20 So, let's just put in error dialog over there, 4:23 dialog.show. 4:28 Get fragment manager. 4:33 And our string, error dialog. 4:34 And our semicolon. 4:42 We're all set. 4:44 In order to test our dialogue though, 4:45 we need to force some kind of error in our app. 4:47 One easy way to cause our web request to fail, 4:50 is to alter the parameters we're passing into our URL. 4:52 We just need to keep in mind what we changed, and 4:56 fix it right away after the test. 4:58 Let's go up to latitude. 5:00 Here let's comment out this value. 5:05 And let's just set it to a number we know doesn't work like 9999. 5:09 Great, now we can run it and test it out. 5:17 Nice, our dialogue appears, and we tap the OK button. 5:27 We go back to the activity. 5:33 Now with that test done let's not forget to change our latitude value back. 5:35 Cool, we're off to a great start with handling errors when 5:47 something goes wrong with a network request. 5:49 What happens if the network isn't available at all though? 5:52 We'll see how to handle that next. 5:56
You need to sign up for Treehouse in order to download course files.Sign up