Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i have an example of displaying a toast using runOnUiThread.

I searched many places but could not find a complete working example of implementation of "runOnUiThread". I tried a lot , but getting lots of errors . I just want to display a toast from a thread.

like image 370
Sourav301 Avatar asked Aug 03 '12 07:08

Sourav301


People also ask

How do you display toast?

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.

What is use of toast explain with example?

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.

Can I show toast from background thread?

Steps: declare a handler in the main activity (onCreate) now create a thread from the main activity and pass the Context of that activity. now use this context in the Toast, instead of using getApplicationContext()

Which is the correct way to create and display a toast?

Creating a Toast Toast toast = Toast. makeText(getApplicationContext(), "This is a message displayed in a Toast", Toast. LENGTH_SHORT); toast. show();


1 Answers

So here is the final full code. Thanks to all who have replied.

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Toast;  public class MainActivity extends Activity {   @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      MainActivity.this.runOnUiThread(new Runnable() {          public void run() {             Toast.makeText(MainActivity.this, "This is Toast!!!", Toast.LENGTH_SHORT).show();          }     }); }  } 

And About the XML, its is the default XML file created. No change needed.

like image 142
2 revs Avatar answered Sep 28 '22 05:09

2 revs