Advertisement
yo2man

Configuring Alert Dialog Part 1

Jul 6th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. // Configuring Alert Dialog Part 1
  2.  
  3. AlertDialogFragment.java
  4. // IO Exception = Input/Output Exception
  5.  
  6. // Try catch:
  7. // We wrap our code in something known as a Try catch block.
  8. // This code is essentially saying, try to check if the response is successful.
  9. // If so, log it.
  10. // If it fails with an IOException, then run this extra code to handle the exception.
  11. // Otherwise, proceed as normal.
  12.  
  13. package com.teamtreehouse.stormy;
  14.  
  15. import android.app.AlertDialog;
  16. import android.app.Dialog;
  17. import android.app.DialogFragment;
  18. import android.content.Context;
  19. import android.os.Bundle;
  20.  
  21. //According to android documentation, we should put a dialog in a dialog Fragment (which may seem like a lot of work but you can just copy and paste it in with a bit of change later if you ever need to use dialog again)
  22.  
  23. //
  24. public class AlertDialogFragment extends DialogFragment { // [add extends DialogFragment base class // Notice, that there are two different DialogFragments here in autocomplete. We wanna make sure we get the android.app one. This android.support one is for backwards compatibility with older versions of Android. But, since our app is targeting for that one above.
  25.  
  26.     // We need to override one key method from DialogFragment //Fragments are similar to Activities 9we will cover fragments in more depth in the future)
  27.     @Override
  28.     public Dialog onCreateDialog(Bundle savedInstanceState) { // this method is similar to the onCreate method in the Activity
  29.  
  30.         //  Code to configure the Dialog:
  31.         Context context = getActivity(); //create variable for getActivity method*
  32.         AlertDialog.Builder builder = new AlertDialog.Builder(context) //*pass variable in here //normally for context we put in 'this.Activity' or 'this' however, that only works if you are in the same class. But now we're in a different class.
  33.         // Fortunately, the dialogue frame of class that we're extending has a handy
  34.         // method to get the activity that this dialog fragment is created in.
  35.         // So we can call it with getActivity which will give us the activity and
  36.         // hence the context that we need.
  37.  
  38.         // Let's take a look at what we're going to build. We are going to have a title at the top, with a message in the box and an OK button at the bottom
  39.         /*builder*/.setTitle("Oops! Sorry!") //delete builder to chain all the methods together!
  40.                 .setMessage("There was an error. Please try again.")
  41.                 //add button (possible three buttons: postive, negative, neutral)
  42.                 .setPositiveButton("OK", null);     // a 'null' onClickListener will just close the dialogue and not do anything specific
  43.     // Now we need to actually create it with a new method and then return it.
  44.     AlertDialog dialog  = builder.create();
  45.         return dialog;
  46.     }
  47. }
  48.  
  49. // https://teamtreehouse.com/library/build-a-weather-app/concurrency-and-error-handling/handling-errors
  50. // https://teamtreehouse.com/library/build-a-weather-app/concurrency-and-error-handling/configuring-the-alert-dialog
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement