0

I start to use AndroidStudio and I want to use toast in my external class of main activity:

In External class i had this method:

 private void call_toast(){
     Toast.makeText(MainActivity, "Task Finalize!", Toast.LENGTH_SHORT).show();
 }

This is my idea, but give me error in "MainActivity". How can I show toast in my actual activity with a external class? Thanks in advance and sorry for my english!

PD1: If you need more info or code advice me!

  • External class? Do you mean internal class? – Simas Nov 20 '14 at 17:18
  • If you need to update the UI (such as with a `Toast`) from an `AsyncTask`, as indicated in comments, you can do that in `onPostExecute()` or any method besides `doInBackground()` if it's an inner class of the Activity. If your `AsyncTask` is a separate file, [see this answer about using an interface](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648) – codeMagic Nov 20 '14 at 19:45

3 Answers3

2

You have a couple of options.

  1. Pass in a Context to call_toast i.e:

    public void call_toast(Context context){}
    

and call from within an activity:

call_toast(SomeActivity.this);
  1. Call getApplicationContext() where you need an activity.

Hope this helps to get you started.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
laminatefish
  • 5,197
  • 5
  • 38
  • 70
2

Displaying toast requires it be done in the main UI thread. The following code is an example of a static method that can be executed from any thread (including a background Service when your application isn't even in the foreground).

public class ServiceUtils {
  //-------------------------------------------------------------------------
  // Constructor

  // All methods in this class are static, no need for a public constructor
  private ServiceUtils() {} 

  private static final Handler s_toastHandler = new Handler(Looper.getMainLooper());


  public static void notifyUI(final Context context, final String toastMsg) {   
      s_toastHandler.post(new Runnable() {
          public void run() {
              try {
                  Toast.makeText(context, 
                                 toastMsg, 
                                 Toast.LENGTH_SHORT).show();
              }
              catch(Exception ex) {
                Log.e(ServiceUtils.class.getSimpleName(), ex.getMessage());
              }
          }
      });
   }
 }

Now you can call from anywhere:

ServiceUtils.notifyUI(getApplicationContext(), "some toast message")
GNewc
  • 445
  • 4
  • 4
  • 1
    Yikes...block of code! Mind explaining what your code is doing for the people? – codeMagic Nov 20 '14 at 17:25
  • 1
    Sure. If you are going to display Toast, you need to make sure that it is done in the main UI thread. The above code explicitly executes the Toast.makeText(...) method in the main looper (i.e. main UI thread). Thus this static method allows the user to display toast from _anywhere_ so long as they have access to a Context. – GNewc Nov 20 '14 at 19:29
  • I simplified the solution above for easier consumption. – GNewc Nov 20 '14 at 19:42
1

Try

Toast.makeText(getApplicationContext(), "Task Finalize!", Toast.LENGTH_SHORT).show();
Machado
  • 14,105
  • 13
  • 56
  • 97