Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Configuring Alert Dialog Part 2
- //----------------------------------------------------------------------------------------------------------------------------
- AlertDialogFragment.java
- package com.teamtreehouse.stormy;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.content.Context;
- import android.os.Bundle;
- public class AlertDialogFragment extends DialogFragment {
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- Context context = getActivity();
- AlertDialog.Builder builder = new AlertDialog.Builder(context)
- .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.
- .setMessage(context.getString(R.string.error_message))
- .setPositiveButton(context.getString(R.string.error_ok_button_text), null);
- AlertDialog dialog = builder.create();
- return dialog;
- }
- }
- //----------------------------------------------------------------------------------------------------------------------------
- MainActivity.java
- package com.teamtreehouse.stormy;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import com.squareup.okhttp.Call;
- import com.squareup.okhttp.Callback;
- import com.squareup.okhttp.OkHttpClient;
- import com.squareup.okhttp.Request;
- import com.squareup.okhttp.Response;
- import java.io.IOException;
- public class MainActivity extends ActionBarActivity {
- public static final String TAG= MainActivity.class.getSimpleName();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- String apiKey = "d5faf0cb201f103df4dde0b8b0a4f490";
- double latitude = 37.8267;
- double longitude = -122.423;
- String forecastUrl = "https://api.forecast.io/forecast/d5faf0cb201f103df4dde0b8b0a4f490/" + apiKey +
- "/" + latitude +"/" + "," + longitude;
- OkHttpClient client = new OkHttpClient();
- Request request = new Request.Builder().url(forecastUrl).build();
- Call call = client.newCall(request);
- call.enqueue(new Callback() {
- @Override
- public void onFailure(Request request, IOException e) {
- }
- @Override
- public void onResponse(Response response) throws IOException {
- try { Log.v(TAG, response.body().string()); //
- if (response.isSuccessful()){
- }
- else {
- alertUserAboutError();
- }
- } catch (IOException e) {
- Log.e(TAG, "Exception caught: ", e);
- }
- }
- });
- Log.d(TAG, "Main UI code is running!");
- }
- // the new stuff:
- 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
- AlertDialogFragment dialog = new AlertDialogFragment(); //from AlertDialogFragment.java
- // To show the dialog in the activity
- // (we need a few parameters in here), the first is 'fragment manager',
- // fortunately, we can just call 'getFragmentManager()', we don't have to set up one, its already theire for us.
- // and the second parameter is a tag for the dialog.
- // 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'
- dialog.show(getFragmentManager(), "error_dialog");
- }
- }
- //----------------------------------------------------------------------------------------------------------------------------
- // 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