Does anybody know, if there is a possibility to do something (in my case finish activity) on toast message will be closed?
Make sure not to forget to call show() after the makeText. Check for the Context , if its the right one. The most important one , make sure your Android Notifications are on for your app, else the Toast will not be shown.
Display the created Toast Message using the show() method of the Toast class. The code to show the Toast message: Toast. makeText(getApplicationContext(), "This a toast message", Toast.
A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
You do that simply by creating a Thread
that lasts as long as the Toast
is displayed and then you can finish your Activity
.
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // your other stuff Toast.makeText(this,"This is a Toast", Toast.LENGTH_LONG).show(); thread.start(); }
Now create a thread that waits for (LENGTH_LONG = 3.5) or (LENGTH_SHORT = 2) seconds
Thread thread = new Thread(){ @Override public void run() { try { Thread.sleep(Toast.LENGTH_LONG); // As I am using LENGTH_LONG in Toast Your_Activity.this.finish(); } catch (Exception e) { e.printStackTrace(); } } };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With