Challenge 1: Customizing the Toast (Solution)


Here is my solution to the first challenge:

1- true button:

public void onClick(View v){
Toast toast= Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT);

2- false button

public void onClick(View v){
Toast toast= Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT);


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?


Try to use getXOffset() and getYOffset()


Thanks that may be the best solution. It’s still not as elegant as dp but still better than using a precise pixel location. :slight_smile:


I used the xOffset and yOffset parameters of the setGravity method. Had to fiddle around with the yOffset value but finally settled on 400.


I get an error for “Gravity” in “Gravity.TOP”


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:

            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);

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.


I could think that maybe android.view.Gravity was not imported?


According to Android documentation, we do not use the public constructor (the “new” part) unless we are doing a custom toast view.

Challenge 1: Toast Position Issue with setGravity

Hobbieman said it well. The Toast.makeText(Context, int, int) returns a value, not modifies the current instantiated object.

Instead, assign the method .makeText() returned value to the “Toast toast” newly created object, then invoke the .setGravity method.

.makeText() is a static method, you need not an object.
.setGravity is not a static method, you must instantiate and object first.