How to use Toast when I cant use “this” as context

I have a location listener activity and I want to make toast notifications. But it will not let me pass this as the context. How should I make toast work?

  • Dynamically setting links to text in strings.xml
  • Can I have a single Google Maps API key for all my apps without registering their package names?
  • FragmentTabHost - No tab known for tag null
  • Load an SWF into a WebView
  • Change Background color and font color
  • OnItemClickListener and OnClickListener not working for ListView
  • Related posts:

    android listview display all available items without scroll with static header
    Play Youtube HTML5 embedded Video in Android WebView
    java.lang.Void vs void vs Null
    Difference between static and shared libraries in Android's NDK?
    Views overlayed above YouTubePlayerFragment or YouTubePlayerView in the layout hierarchy cause playb...
    “You cannot change private secure settings” - how to toggle ringtone vibration i...
  • Android Service
  • Android_Chronometer pause
  • How is the number of views limited?
  • How to solve the issue with Dalvik compiler limitation on 64K methods?
  • ADB Connection Error: Unable to create Debug Bridge: Unable to start adb server: Unable to detect adb version, adb output
  • Android/java Sqlite: How to retrieve max datetime?
  • 7 Solutions collect form web for “How to use Toast when I cant use “this” as context”

    If the toast is located inside your activity class, you could yous YourActiviy.this where YourActivity is the class name. if it’s outside your class, you’ll need to get your activity context (pass it in the constructor etc.)

    You can use NameOfYourActivity.this

    For example:

    public class MyActivity extends Activity {
         Toast.makeText(MyActivity.this, text, duration).show();

    If you are in the inner Class then try this also


    For example, if you have a listener with a method called “onComplete” inside it, this code should work.

    public void onComplete(String response, Object state) {
            final String response_complete = response;
            MyActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(MyActivity.this, text, duration).show();

    That should do it.

    instead try getApplicationContext()

     Toast tea = Toast.makeText(getApplicationContext(), "Send", Toast.LENGTH_LONG);;

    It sounds like you are in an inner class in the Activity. If thats the case, try ActivityName.this.

    Field variable: Context context;

    inside OnCreate: context = this;

    Xamarin / C# Syntax:
    Toast.MakeText(context, "your message", ToastLength.Long).Show();

    Android / Java syntax:
    Toast.makeText(context, "your message", Toast.LENGTH_LONG).show();

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.