The solution is pretty straight forward. However I do have a problem with its positioning. It overlaps with the title bar and I was wondering, if there is a more elegant solution than the offset in true pixels to move it into the activity screen?
I have a question that it might be more about Java than about the challenge itself. I don’t fully understand anonymous inner classes. alaa173’s answer works just fine, but before using their answer I tried lots of stuff and nothing did the trick. I wanted to know why this code stops the app from working:
@Override
public void onClick(View v) {
Toast toast = new Toast(getApplicationContext());
toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
}
});
The difference I see is the abscence of the “new” part. Why does assigning a reference to a toast variable of a Toast instance, and calling the methods from there break everything? What’s the difference with alaa173’s version?
I believe this issue has nothing to do with inner classes.
I do not yet know how to look at the source of the android classes, but I believe the Toast::makeText methods do not modify the instance of toast that you used in your code to make the call on them. The call is being made at the “class” level, if you will. So, those methods create (and return) a new instance of Toast which in the code you posted, does not get used. As a result, the instance of toast you created has not been modified.
If you merged lines 1 & 2 in the onClick method, it would work. Basically, assign the object received from makeText to your local toast reference variable.