1

I am working on my first Android app and I am trying to create a toast and specify the location where I want it to appear. When I was using the code that is now commented (it didn't customize the toast's location) the app ran perfectly on the emulator, but now that I am trying to customize the location and using the code below the comments, the app stops running when I click in the UI on the button meant to show the toast.

Do you have any idea of why this is happening? Because I have seen very similar codes (almost the same) presented as correct, so this should be working. I know this is a very simple code and a simple question, but still, I cannot figure out what is wrong with my code.

This is the code on my onButtonClick:

public void onClick(View V) {

            // Toast.makeText(QuizActivity.this,
            //                 R.string.correct_toast,
            //                 Toast.LENGTH_SHORT).show();

            Toast t = new Toast(QuizActivity.this);
            t.setText(R.string.correct_toast);
            t.setDuration(Toast.LENGTH_SHORT);
            t.setGravity(Gravity.TOP|Gravity.LEFT,0,0);
            t.show();

        }
  • Possible duplicate of [How to change position of Toast in Android?](https://stackoverflow.com/questions/2506876/how-to-change-position-of-toast-in-android) – Rahul Mishra Aug 09 '18 at 01:07
  • What your app target API? Why don't you use Snackbar as Toast is kinda phase out nowadays. Snackbar is used to suit the material design – Angus Aug 09 '18 at 01:08
  • @RahulMishra it is not a duplicate if he use the latest android api which result in a different issue – Angus Aug 09 '18 at 01:09
  • "_the app stops running when in the UI I click_" Please post stack trace – John Joe Aug 09 '18 at 01:10
  • @JohnJoe I just run the app, and right away I have two buttons. Then, when I click on the first of them (which is still using a code like the one on comments) it shows a toast, which is what I am expecting. But then, when I click the other button (which has the code that I modified to make the toast appear in a different place) the app stops running. – Karol Phung Aug 09 '18 at 01:26
  • See your log. When your app stops running, threre must be something display on there – John Joe Aug 09 '18 at 01:59

1 Answers1

0

From https://developer.android.com/reference/android/widget/Toast#Toast(android.content.Context) :

Construct an empty Toast object. You must call setView(View) before you can call show().

Your code crashes because now you are using the Toast() constructor. And you need to setView(View) before show(). While this is not necessary with makeText().

As I understand, makeText() creates a View for you setting the text from the second parameter. But the constructor doesn't, and the setText() method does not either. setText() only update the an existing text:

From https://developer.android.com/reference/android/widget/Toast#setText(int) :

Update the text in a Toast that was previously created using one of the makeText() methods.

Here is an example of how to do that:

Custom toast on Android: a simple example

Edit

In any case, you can create the Toast with your initial code, without calling show().

public void onClick(View V) {

        Toast t = Toast.makeText(QuizActivity.this,
                        R.string.correct_toast,
                        Toast.LENGTH_SHORT);


        t.setGravity(Gravity.TOP|Gravity.LEFT,0,0);
        t.show();

    }

Edit 2

Now you have edited your code, but you are passing the Button View.

Barrendeitor
  • 506
  • 1
  • 3
  • 14
  • 1
    Thank you so much, your answer was absolutely helpful. Although even using setView (View) it does not work yet (so I have to further explore this), right now your second suggestion workes perfectly. So, thanks for your help! – Karol Phung Aug 09 '18 at 02:34