Create Singleton Class for Toast Message
suggest changeIntroduction
Toast messages are the simplest way of providing feedback to the user. By default, Android provides a gray toast where you can set the message and the duration. If you need a more customizable and reusable toast, implement it with a custom layout. The Singleton design pattern makes that custom toast class easier to maintain.
Syntax
- Toast(Context context)
- void setDuration(int duration)
- void setGravity(int gravity, int xOffset, int yOffset)
- void setView(View view)
- void show()
Parameters
| Parameter | details |
|---|---|
| context | Relevant context which needs to display your toast message. If you use this in an activity, pass this. If you use it in a fragment, pass getActivity(). |
| view | Create a custom view and pass that view object to this. |
| gravity | Pass the gravity position of the toast. Positions are static fields on Gravity. The most common are Gravity.TOP, Gravity.BOTTOM, Gravity.LEFT, and Gravity.RIGHT. |
| xOffset | Horizontal offset of the toast message. |
| yOffset | Vertical offset of the toast message. |
| duration | Duration of the toast. Set either Toast.LENGTH_SHORT or Toast.LENGTH_LONG. |
Remarks
A toast message is a simple way of providing feedback to the user that something is happening. If you need a more advanced way to give feedback, you can use dialogs or a snackbar.
To get more details about the toast message please check this documentation. https://developer.android.com/reference/android/widget/Toast.html
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents