How To Show Custom Toast In Android?
When it's comes to display light message in response to options performed by app Android platform offers Toast.Toast is a light messages display near the bottom,horizontally centered to the screen.Toast can display maximum two line of message.Toast messages are not clickable and hide automatically after sometime.Toast messages are useful when you don't want to block user interface and want to display messages.
Figure 1. shows "Sending message..." text message,indicating message has been sending.
Fegure 1. Toast Message.
Show Toast.
To show Toast you need to first initialize Toast object with one of makeText() methods.makeText() method takes three parameters : the application Context, the message to display and duration for the Toast.this method returns fully initialized Toast object.you can show Toast with show() method.
Following code snipped shows how to show simple toast message.
How To Change Toast Default Position?
Toast by default display near the bottom of the screen,horizontally centered.You can change toast default position with setGravity(int, int, int) method,which takes three parameters: a Gravity constant , a X-position offset and Y-position offset.
Following code snippet shows how to position toast to top right corner of the screen.
If you want to change position of x axis of toast then increase or decrease value of second parameter and if you want to change position of Y-axis of toast then increase of decrease value of third parameter.
You can see that ID of the LinearLayout element is "custom_toast_container". You must use this ID and ID of XML layout file to inflate the layout.
Following code snippet shows how to inflate and set custom layout for Toast.
First retrieve LayoutInflater object with getLayoutInflater() or from getSystemService() method.then inflate your custom layout with inflate(int, ViewGroup) method, which take to parameters first is the ID of layout file you want to inflate and second is root View.You can use inflated View to find more View objects in the layout.So lets find and set content for TextView.At last create the Toast object with Toast(Context) and set properties like duration,gravity Then call setView(View) and pass it the inflated custom layout.You can now display toast by calling show() method.
How To Display Custom Toast?
When simple toast layout can not fulfill your requirements you can create custom view for toast messages.To create custom toast layout define View layout,in XML file or in you application code and pass the root View object to setView(view) method of the Toast object to set this layout.
Following code snipped shows custom layout for the toast message.
Note : save this layout file as custom_toast.xml
You can see that ID of the LinearLayout element is "custom_toast_container". You must use this ID and ID of XML layout file to inflate the layout.
Following code snippet shows how to inflate and set custom layout for Toast.
First retrieve LayoutInflater object with getLayoutInflater() or from getSystemService() method.then inflate your custom layout with inflate(int, ViewGroup) method, which take to parameters first is the ID of layout file you want to inflate and second is root View.You can use inflated View to find more View objects in the layout.So lets find and set content for TextView.At last create the Toast object with Toast(Context) and set properties like duration,gravity Then call setView(View) and pass it the inflated custom layout.You can now display toast by calling show() method.
No comments:
Post a Comment