Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Configuring Alert Dialog Part 1
- AlertDialogFragment.java
- // IO Exception = Input/Output Exception
- // Try catch:
- // We wrap our code in something known as a Try catch block.
- // This code is essentially saying, try to check if the response is successful.
- // If so, log it.
- // If it fails with an IOException, then run this extra code to handle the exception.
- // Otherwise, proceed as normal.
- package com.teamtreehouse.stormy;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.content.Context;
- import android.os.Bundle;
- //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)
- //
- 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.
- // We need to override one key method from DialogFragment //Fragments are similar to Activities 9we will cover fragments in more depth in the future)
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) { // this method is similar to the onCreate method in the Activity
- // Code to configure the Dialog:
- Context context = getActivity(); //create variable for getActivity method*
- 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.
- // Fortunately, the dialogue frame of class that we're extending has a handy
- // method to get the activity that this dialog fragment is created in.
- // So we can call it with getActivity which will give us the activity and
- // hence the context that we need.
- // 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
- /*builder*/.setTitle("Oops! Sorry!") //delete builder to chain all the methods together!
- .setMessage("There was an error. Please try again.")
- //add button (possible three buttons: postive, negative, neutral)
- .setPositiveButton("OK", null); // a 'null' onClickListener will just close the dialogue and not do anything specific
- // Now we need to actually create it with a new method and then return it.
- AlertDialog dialog = builder.create();
- return dialog;
- }
- }
- // https://teamtreehouse.com/library/build-a-weather-app/concurrency-and-error-handling/handling-errors
- // 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