76

I'm trying to show a Toast Message when user click on a Button inside a Fragment. The problem is I cannot access the activity to show the Toast on it.

Here's the source of Fragment:

    public class FrgTimes extends Fragment
    {
        ScrollView sv;
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) 
        {
            if (container == null) { return null; }

            sv = (ScrollView)inflater.inflate(R.layout.frg_times, container, false);

            btnTime1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            //******  HERE's the PROBLEM  ********
            Toast.makeText(<The Activity>, "Please long press the key", Toast.LENGTH_LONG );

            }});

            return sv;
        }

and Here's what I've been tried.

Toast.makeText( getActivity()  , ...
Toast.makeText( getView().getContext()  , ...
Toast.makeText( getActivity().getApplicationContext()  , ...
Toast.makeText( sv.getContext()  , ...
Toast.makeText( sv.getRootView().getContext()  , ...

In Debug I can see that all of these codes run without any exception but no TOAST being displayed.

mammadalius
  • 3,263
  • 6
  • 39
  • 47

15 Answers15

130

You are not calling show() on the Toast you are creating with makeText().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
68

As stated by alfo888_ibg:

@Override
public void onClick(View arg0) {
   Toast.makeText(activity,"Text!",Toast.LENGTH_SHORT).show();
}

Just do:

    Toast.makeText(getActivity(),"Text!",Toast.LENGTH_SHORT).show();

this worked for me.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
Senimii
  • 781
  • 5
  • 5
  • I am using the toast inside the onCreateView of the Fragment's java file. Toast is being called two times.Why? ```Toast.makeText(getContext(), "Entered onCreateView", Toast.LENGTH_SHORT).show();``` i have also used the getActivity() in place of getContext()... same issue – NullByte08 Nov 23 '19 at 07:27
24

To help another people with my same problem, the complete answer to Use Toast inside Fragment is:

Activity activity = getActivity();

@Override
public void onClick(View arg0) {

    Toast.makeText(activity,"Text!",Toast.LENGTH_SHORT).show();
}
Michael Unterthurner
  • 921
  • 1
  • 10
  • 25
alfo888_ibg
  • 1,847
  • 5
  • 25
  • 43
16

When making a toast in fragment do as following:

Toast.makeText(getActivity(),"Message", Toast.LENGTH_SHORT).show();

When class is extending fragment it is necessary to use getActivity() since fragment is a subclass of activity.

Cheerse

Sindri Þór
  • 2,887
  • 3
  • 26
  • 32
  • 4
    Fragment is a subclass of Object, check http://developer.android.com/reference/android/app/Fragment.html, if it was subclass of activity you could use 'this' instead – chairam Nov 28 '15 at 01:24
11

You can get the current activity with getActivity()

Toast.makeText(getActivity(),"Toast your message" ,Toast.LENGTH_SHORT).show();
mikep
  • 3,841
  • 8
  • 21
andy bit1
  • 111
  • 1
  • 4
9

Making a Toast inside Fragment

 Toast.makeText(getActivity(), "Your Text Here!", Toast.LENGTH_SHORT).show();

OR

    Activity activityObj = this.getActivity();

    Toast.makeText(activityObj, "Your Text Here!", Toast.LENGTH_SHORT).show();

OR

Toast.makeText(this, "Your Text Here!", Toast.LENGTH_SHORT).show();
Ramesh
  • 1,772
  • 4
  • 12
  • 15
3

If you are using kotlin then the context will be already defined in the fragment. So just use that context. Try the following code to show a toast message.

Toast.makeText(context , "your_text", Toast.LENGTH_SHORT).show()
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
2

When calling Toast inside android fragment:

1. Activity mActivity=this.getActivity();  

2. Toast.makeText(mActivity,"Text you want to display",Toast.LENGTH_SHORT).show();

This works for me.

Gandhi
  • 11,875
  • 4
  • 39
  • 63
1

user2564789 said it right
But you can also use this in the place of getActivity()
which will make your toast look like this


     Toast.makeText(this,"Message",Toast.LENGTH_SHORT).show();
    
vaibhav3027
  • 51
  • 1
  • 6
0

A simple [Fragment] subclass.
Kotlin!
contextA - is a parent (main) Activity. Set it on create object.

class Start(contextA: Context) : Fragment() {

var contextB: Context = contextA;

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val fl = inflater.inflate(R.layout.fragment_start, container, false)

    // only thet variant is worked on me
    fl.button.setOnClickListener { view -> openPogodaUrl(view) }

    return fl;
}

fun openPogodaUrl(view: View) {        
    try {
        pogoda.webViewClient = object : WebViewClient() { // pogoda - is a WebView
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                view?.loadUrl(url)
                return true
            }
        }
        pogoda.loadUrl("http://exemple.com/app_vidgets/pogoda.html");
    }
    catch (e: Exception)
    {
        Toast.makeText(contextB, e.toString(), Toast.LENGTH_LONG).show();
    }
}

}

Alatey
  • 371
  • 2
  • 6
0

Using Kotlin

Toast.makeText(view!!.context , "your_text", Toast.LENGTH_SHORT).show()
X-Black...
  • 1,376
  • 2
  • 20
  • 28
0

In Kotlin android Toast.makeText(activity!!, "Your Text Here!", Toast.LENGTH_SHORT).show()

RJnr
  • 670
  • 6
  • 19
0

In Kotlin :

Inside Activity: Toast.makeText(this, "Your Message", Toast.LENGTH_LONG).show()

Inside Fragment: Toast.makeText(context, "Your Message", Toast.LENGTH_LONG).show()

Inside Adapter: Toast.makeText(context.context, "Your Message", Toast.LENGTH_LONG).show()

Here first context is Activity or Fragment instance.

Manoj Kumar
  • 307
  • 2
  • 7
  • 13
-1
        public void onClick(View v) {
            Context context = v.getContext();
            CharSequence text = "Message";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
-1

Unique Approach

(Will work for Dialog, Fragment, Even Util class etc...)

ApplicationContext.getInstance().toast("I am toast");

Add below code in Application class accordingly.

public class ApplicationContext extends Application {

private static ApplicationContext instance;

@Override
public void onCreate() {
    super.onCreate();
    instance = this;
}

public static void toast(String message) {
    Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
}
}
Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212