Challenge 1: Customizing the Toast (Solution)


#1

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);
toast.setGravity(Gravity.TOP,0,0);
toast.show();
}

2- false button

public void onClick(View v){
Toast toast= Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP,0,0);
toast.show();
}


#2

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?


#3

Try to use getXOffset() and getYOffset()


#4

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:


#5

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


#6

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


#7

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?


#8

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.


#9

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


#10

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
#11

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.