Skip to content
Fix Code Error

How to display Toast in Android?

March 13, 2021 by Code Error
Posted By: Anonymous

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.

Solution

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();
Answered By: Anonymous

Related Articles

  • ListView inside ScrollView is not scrolling on Android
  • Android Layout Animations from bottom to top and top…
  • Android : Fragment doesn't fit into Fragment Container
  • How to implement HorizontalScrollView like Gallery?
  • How to set dropdown arrow in spinner?
  • Constraint Layout Vertical Align Center
  • Error inflating class android.support.v7.widget.Toolbar?
  • android.view.InflateException: Binary XML file line…
  • SecurityException: Permission denied (missing…
  • Fragment Not Showing in the Activity
  • Vue js vuecli3 application does not work in ie11…
  • Android adding simple animations while…
  • Android ListView headers
  • MaterialCardView is in front of NavigationView. How…
  • Failed to load AppCompat ActionBar with unknown…
  • onClick for buttons on custom CardView
  • Getting duplicated cardviews using SwipeRefresh Layout
  • How to use ScrollView in Android?
  • Remove Text from the edit text when edit text is focused
  • Button is null object reference (Android Studio)
  • Google in-app billing, a toast breaks everything
  • Percentage width in a RelativeLayout
  • Unable to run Robolectric and Espresso with a…
  • java.lang.IllegalStateException: Can not perform…
  • GridLayout and Row/Column Span Woe
  • How to center the content inside a linear layout?
  • Integrate ZXing in Android Studio
  • Programmatically Hide/Show Android Soft Keyboard
  • Google Maps Android API v2 - Interactive InfoWindow…
  • Error inflating class…
  • Aurelia - help understanding some binding behavior…
  • AppCompat v7 r21 returning error in values.xml?
  • How to get the result of OnPostExecute() to main…
  • Android App crashes on animated gradient background
  • Unable to move from one fragment to another
  • Playing HTML5 video on fullscreen in android webview
  • Couldn't load memtrack module Logcat Error
  • Android app unable to start activity componentinfo
  • java.lang.NullPointerException: Attempt to invoke…
  • Android: How to Programmatically set the size of a Layout
  • Navigation View and Constraint Layout
  • how to show progress bar(circle) in an activity…
  • firebase storage java.lang.IllegalStateException:…
  • How to Intialize the Binding Properties in the…
  • Fit Image in ImageButton in Android
  • How to hide toolbar in collapsed mode and show in…
  • java.lang.RuntimeException: Unable to start activity…
  • Failed linking file resources
  • Android - How to download a file from a webserver
  • CSS Variable trough Polymer (dynamically loaded) elements
  • Move to another EditText when Soft Keyboard Next is…
  • ADB - Android - Getting the name of the current activity
  • Android - styling seek bar
  • Handler vs AsyncTask vs Thread
  • How to get the design view to look the same on your device
  • Android Layout Right Align
  • Android "gps requires ACCESS_FINE_LOCATION" error,…
  • Gridview with two columns and auto resized images
  • READ_EXTERNAL_STORAGE permission for Android
  • Android TextView Text not getting wrapped
  • Moving from one activity to another Activity in Android
  • CardView not showing Shadow in Android L
  • For loops print only last value
  • Fragment not showing properly
  • Text size and different android screen sizes
  • Auto-fit TextView for Android
  • Android getting value from selected radiobutton
  • How to make custom dialog with rounded corners in android
  • GridLayout (not GridView) how to stretch all children evenly
  • how to access downloads folder in android?
  • How to disable keypad popup when on edittext?
  • Defining a percentage width for a LinearLayout?
  • jQuery Mobile: document ready vs. page events
  • How to download and save an image in Android
  • Aurelia issue with setting element class based on…
  • This app won't run unless you update Google Play…
  • Duplicate ID, tag null, or parent id with another…
  • How to implement a ViewPager with different…
  • DatePicker onDateSet method not running
  • How do I align views at the bottom of the screen?
  • Put buttons at bottom of screen with LinearLayout?
  • Download a file with Android, and showing the…
  • AsyncTask Android example
  • View not attached to window manager crash
  • Android Transparent TextView?
  • Android "elevation" not showing a shadow
  • Creating a SearchView that looks like the material…
  • Assign category or integer to row if value within an…
  • Android + Pair devices via bluetooth programmatically
  • Adding custom radio buttons in android
  • Active tab issue on page load HTML
  • Unable to get Notification pop-up
  • How to create a Custom Dialog box in android?
  • NullPointerException: Attempt to invoke virtual…
  • Android:java.lang.OutOfMemoryError: Failed to…
  • How do I make a LinearLayout scrollable?
  • Add Items to ListView - Android
  • Item position in RecyclerView only changing when…
  • vuejs Data property undefined
  • How to center the elements in ConstraintLayout

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Git push results in “Authentication Failed”

Next Post:

How to extract numbers from a string in Python?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error