By Alvin Alexander. Last updated: June 6, 2016
This is how you can show an Android Toast
message from a Fragment:
Toast.makeText(getActivity(), "Click!", Toast.LENGTH_SHORT).show();
As shown, don’t forget to call .show()
at the end of the makeText
method. Forgetting to call show()
is a common mistake.
If it helps to see this in more context, this is a complete onListItemClick
method from a ListFragment
subclass I’m currently working on:
@Override public void onListItemClick(ListView l, View v, int position, long id) { Crime c = (Crime)(getListAdapter()).getItem(position); Log.d(TAG, c.getTitle() + " was clicked"); Toast.makeText(getActivity(), "Click!", Toast.LENGTH_SHORT).show(); }