Advertisement
yo2man

Configuring Alert Dialog Part 2

Jul 6th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. //Configuring Alert Dialog Part 2
  2.  
  3. //----------------------------------------------------------------------------------------------------------------------------
  4. AlertDialogFragment.java
  5.  
  6. package com.teamtreehouse.stormy;
  7.  
  8. import android.app.AlertDialog;
  9. import android.app.Dialog;
  10. import android.app.DialogFragment;
  11. import android.content.Context;
  12. import android.os.Bundle;
  13.  
  14.  
  15. public class AlertDialogFragment extends DialogFragment {
  16.  
  17.     @Override
  18.     public Dialog onCreateDialog(Bundle savedInstanceState) {
  19.         Context context = getActivity();
  20.         AlertDialog.Builder builder = new AlertDialog.Builder(context)
  21.                 .setTitle(context.getString(R.string.error_title)) //change all the text messages to String resources. Steps: 1. Alt+Enter. 2. give the resource a name 3. then hit ESC key when CONTEXT is highlighted in red.
  22.                 .setMessage(context.getString(R.string.error_message))
  23.                 .setPositiveButton(context.getString(R.string.error_ok_button_text), null);
  24.     AlertDialog dialog  = builder.create();
  25.         return dialog;
  26.     }
  27. }
  28.  
  29. //----------------------------------------------------------------------------------------------------------------------------
  30.  
  31. MainActivity.java
  32.  
  33. package com.teamtreehouse.stormy;
  34.  
  35. import android.support.v7.app.ActionBarActivity;
  36. import android.os.Bundle;
  37. import android.util.Log;
  38. import android.view.Menu;
  39. import android.view.MenuItem;
  40.  
  41. import com.squareup.okhttp.Call;
  42. import com.squareup.okhttp.Callback;
  43. import com.squareup.okhttp.OkHttpClient;
  44. import com.squareup.okhttp.Request;
  45. import com.squareup.okhttp.Response;
  46.  
  47. import java.io.IOException;
  48.  
  49.  
  50. public class MainActivity extends ActionBarActivity {
  51.  
  52.     public static final String TAG= MainActivity.class.getSimpleName();
  53.  
  54.     @Override
  55.     protected void onCreate(Bundle savedInstanceState) {
  56.         super.onCreate(savedInstanceState);
  57.         setContentView(R.layout.activity_main);
  58.  
  59.         String apiKey = "d5faf0cb201f103df4dde0b8b0a4f490";
  60.         double latitude = 37.8267;
  61.         double longitude = -122.423;
  62.         String forecastUrl = "https://api.forecast.io/forecast/d5faf0cb201f103df4dde0b8b0a4f490/" + apiKey +
  63.                 "/" + latitude +"/" + "," + longitude;
  64.  
  65.         OkHttpClient client = new OkHttpClient();
  66.         Request request = new Request.Builder().url(forecastUrl).build();
  67.  
  68.  
  69.         Call call = client.newCall(request);
  70.         call.enqueue(new Callback() {
  71.             @Override
  72.             public void onFailure(Request request, IOException e) {
  73.  
  74.             }
  75.  
  76.             @Override
  77.             public void onResponse(Response response) throws IOException {
  78.                 try { Log.v(TAG, response.body().string()); //
  79.  
  80.                     if (response.isSuccessful()){
  81.                     }
  82.                     else {
  83.                         alertUserAboutError();
  84.                     }
  85.                 } catch (IOException e) {
  86.                     Log.e(TAG, "Exception caught: ", e);
  87.                 }
  88.  
  89.             }
  90.         });
  91.  
  92.         Log.d(TAG, "Main UI code is running!");
  93.     }
  94.  
  95.  
  96. // the new stuff:
  97.     private void alertUserAboutError() { //visually alert the user that an error has occurred. //we could use toast message but the user may miss it as it disappears quickly // instead use dialogs that requires users to confirm to dismiss
  98.         AlertDialogFragment dialog = new AlertDialogFragment(); //from AlertDialogFragment.java
  99.  
  100.         // To show the dialog in the activity
  101.         // (we need a few parameters in here), the first is 'fragment manager',
  102.         // fortunately, we can just call 'getFragmentManager()', we don't have to set up one, its already theire for us.
  103.         // and the second parameter is a tag for the dialog.
  104.         // this is just a string tag we can use to refer to it. We can name it anything. but in this case we'll name it 'error_dialog'
  105.         dialog.show(getFragmentManager(), "error_dialog");
  106.     }
  107. }
  108.  
  109.  
  110. //----------------------------------------------------------------------------------------------------------------------------
  111. // 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