artworkad シ artworkad シ - 1 month ago 5
Android Question

How to display Toast in Android?

I have a slider that can be pulled up and then it shows a map. I can move the slider up and down to hide or show the map. When the map is on front, I can handle touch events on that map. Everytime I touch, a AsyncTask is fired up, it downloads some data and makes a Toast that displays the data. Although I start the task on touch event no toast is displayed, not till I close the slider. When the slider is closed and the map is not displayed anymore the Toast appears.

any ideas?

well start the task


public boolean onTouchEvent(MotionEvent event, MapView mapView){
if (event.getAction() == 1) {
new TestTask(this).execute();
return true;
return false;

and in onPostExecute make a toast


In new TestTask(this), this is a reference to MapOverlay and not to MapActivity, so this was the problem.


How to display Toast in Android?

In order to "display Toast" in your application, try this:

Toast.makeText(getActivity(), (String)data.result, 

Another example...

Toast.makeText(getActivity(), "this is my Toast message!!! =)",

We can define two constants for duration:

int LENGTH_LONG Show the view or text notification for a long period of time.

int LENGTH_SHORT Show the view or text notification for a short period of time.

Read more about "Toasts"

Another option,

Customizing your toast:

LayoutInflater myInflater=LayoutInflater.from(this);
View view=myInflater.inflate(R.layout.your_custom_layout,null);
Toast mytoast=new Toast(this);