-4

I want To know how can I Toast The Message or String after 5 seconds In Android..? Suggest Some Solutions or Code..

Toast.makeText(Getapplicationcontext,"Hello", Toast.length_Short);

I have Simple code can anybody have idea..?

Raj
  • 2,997
  • 2
  • 12
  • 30

3 Answers3

1

I am assuming you want to show Toast after 5 seconds when your activity is open, you have to use Handler class for that checkout below code -

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(getContext(), "Hello", Toast.LENGTH_SHORT).show();
    }
}, 5000);

where 5000 is time in milliseconds.

Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
0
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {

    // Add the line which you want to run after 5 sec.

    }
},5000);
Raj
  • 2,997
  • 2
  • 12
  • 30
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Aug 02 '18 at 01:10
0

You can use Handler class like this :

Handler h= new Handler();
h.postDelayed(new Runnable(){
    Toast.makeText(getApplicationContext(),"Hello", Toast.LENGTH_SHORT).show();\\your code
},5000);\\miliseconds
Raj
  • 2,997
  • 2
  • 12
  • 30
Omid.N
  • 824
  • 9
  • 19