Toast Message At the heart of Activity on Android

Asked

Viewed 772 times

0

I’m creating an app and I want to post a message using Toast. But when creating Toast it only stays at the bottom of Activity. To center the message I tried to use this command line.

Toast.makeText(MainActivity.this, "Tarefa salva com sucesso!", Toast.LENGTH_SHORT);
            Toast.setGravity(Gravity.CENTER,0,0, null);
            Toast.show();

I’m using the latest version of android studio(3.2)

but it is not giving this error

error: method setGravity in class Toast cannot be applied to given types;
required: int,int,int
found: int,int,int,<null>
reason: actual and formal argument lists differ in length

1 answer

2

You are starting the Toast 3 times doing this way.

For it to work, you must do so:

Toast toast = Toast.makeText(MainActivity.this, "Tarefa salva com sucesso!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

Where he creates only one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.