Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjenyufu
3,311 PointsIn the Alert Dialog my [OK] button is set in the middle of the dialog. But in the lesson its in the bottom right?
How do I fix it so it would be set in the bottom right like in the tutorial?
Here is my source code:
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 = 9999; //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");
}
}
1 Answer
jenyufu
3,311 PointsFound the error:
double latitude = 9999; //37.8267;
I forgot to delete the '9999' we purposely made in the tutorial to get the error and uncomment out 37.8267
should be like this:
double latitude = 37.8267;