Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

EDIT:

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

and in onPostExecute make a toast

Toast.makeText(app.getBaseContext(),(String)data.result,                  Toast.LENGTH_SHORT).show(); 

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

like image 482
UpCat Avatar asked Aug 17 '10 07:08

UpCat


People also ask

Which method is used to display toast?

Use the makeText() method, which takes the following parameters: The application Context . The text that should appear to the user. The duration that the toast should remain on the screen.

What is toast in Android explain with example?

An Android Toast is a small message displayed on the screen, similar to a tool tip or other similar popup notification. A Toast is displayed on top of the main content of an activity, and only remains visible for a short time period.


1 Answers

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

Toast.makeText(getActivity(), (String)data.result,     Toast.LENGTH_LONG).show(); 

Another example:

Toast.makeText(getActivity(), "This is my Toast message!",    Toast.LENGTH_LONG).show(); 

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.

Customizing your toast

LayoutInflater myInflater = LayoutInflater.from(this); View view = myInflater.inflate(R.layout.your_custom_layout, null); Toast mytoast = new Toast(this); mytoast.setView(view); mytoast.setDuration(Toast.LENGTH_LONG); mytoast.show(); 
like image 145
Jorgesys Avatar answered Sep 20 '22 09:09

Jorgesys