{"id":486,"date":"2016-07-13T07:37:29","date_gmt":"2016-07-13T07:37:29","guid":{"rendered":"http:\/\/abhiandroid.com\/programming\/?p=486"},"modified":"2019-06-13T06:10:38","modified_gmt":"2019-06-13T06:10:38","slug":"custom-toast-tutorial-example","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/programming\/custom-toast-tutorial-example.html","title":{"rendered":"Toast &#038; Custom Toast With Example In Android Studio"},"content":{"rendered":"<p>In Android, Toast is used to display information for a period of time. It contains a message to be displayed quickly and disappears after specified period of time. It does not block the user interaction. Toast is a subclass of Object class. In this we use two constants for setting the duration for the Toast. Toast notification in android always appears near the bottom of the screen. We can also create our custom toast by using custom layout(xml file).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-489\" src=\"\/programming\/wp-content\/uploads\/2016\/07\/Toast-custom-toast-in-Android.jpg\" alt=\"Toast custom toast in Android\" width=\"481\" height=\"135\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2016\/07\/Toast-custom-toast-in-Android.jpg 481w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2016\/07\/Toast-custom-toast-in-Android-300x84.jpg 300w\" sizes=\"auto, (max-width: 481px) 100vw, 481px\" \/><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Special Note:<\/strong><\/span> In Android, Toast is used when we required to notify user about an operation without expecting any user input. It displays a small popup for message and automatically fades out after timeout.<\/p>\n<hr \/>\n<h4><strong>Important Methods Of Toast:<\/strong><\/h4>\n<p>Let\u2019s we discuss some important methods of Toast that may be called in order to manage the Toast.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. makeText(Context context, CharSequence text, int duration):<\/strong><\/span> This method is used to initiate the Toast. This method take three parameters First is for the application Context, Second is text message and last one is duration for the Toast.<\/p>\n<p><span style=\"color: #008000;\"><strong>Constants of Toast:<\/strong><\/span> Below is the constants of Toast that are used for setting the duration for the Toast.<\/p>\n<p><strong>1. LENGTH_LONG:<\/strong> It is used to display the Toast for a long period of time. When we set this duration the Toast will be displayed for a long duration.<\/p>\n<p><strong>2. LENGTH_SHORT:<\/strong> It is used to display the Toast for short period of time. When we set this duration the Toast will be displayed for short duration.<\/p>\n<p>Below we show the use of makeText() method of Toast in which we set application context, a text message and duration for the Toast.<\/p>\n<pre>Toast toast = Toast.makeText(getApplicationContext(), \"Simple Toast\", Toast.LENGTH_LONG); \/\/ initiate the Toast with context, message and duration for the Toast\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. show():<\/strong><\/span> This method is used to display the Toast on the screen. This method is display the text which we create using makeText() method of Toast.<\/p>\n<p>Below we Firstly initiate the Toast and then display it using show() method.<\/p>\n<pre>Toast toast = Toast.makeText(getApplicationContext(), \"Simple Toast In Android\", Toast.LENGTH_LONG); \/\/ initiate the Toast with context, message and duration for the Toast\r\ntoast.show(); \/\/ display the Toast\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. setGravity(int,int,int):<\/strong><\/span> This method is used to set the gravity for the Toast. This method accepts three parameters: a\u00a0Gravity\u00a0constant, an x-position offset, and a y-position offset.<\/p>\n<p>Below we Firstly initiate the Toast, set top and left gravity and then display it using show() method.<\/p>\n<pre>Toast toast = Toast.makeText(getApplicationContext(), \"Simple Toast In Android\", Toast.LENGTH_LONG); \/\/ initiate the Toast with context, message and duration for the Toast\r\ntoast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);\u00a0\u00a0\u00a0\u00a0 \/\/ set gravity for the Toast.\r\ntoast.show(); \/\/ display the Toast\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. setText(CharSequence s):<\/strong><\/span> This method is used to set the text for the Toast. If we use makeText() method and then we want to change the text value for the Toast then we use this method.<\/p>\n<p>Below we firstly create a new Toast using makeText() method and then set the text for the Toast.<\/p>\n<pre>Toast toast = Toast.makeText(getApplicationContext(), \"Simple Toast In Android\", Toast.LENGTH_LONG); \/\/ initiate the Toast with context, message and duration for the Toast\r\ntoast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);\u00a0\u00a0\u00a0\u00a0 \/\/ set gravity for the Toast.\r\ntoast.setText(\"Changed Toast Text\"); \/\/ set the text for the Toast\r\ntoast.show(); \/\/ display the Toast\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. setDuration(int duration):<\/strong><\/span> This method is used to set the duration for the Toast. If we use makeText() method and then we want to change the duration for the Toast then we use this method.<\/p>\n<p>Below we firstly create a new Toast using makeText() method and then set the duration for the Toast.<\/p>\n<pre>Toast toast = Toast.makeText(getApplicationContext(), \"Simple Toast In Android\", Toast.LENGTH_LONG); \/\/ initiate the Toast with context, message and duration for the Toast\r\ntoast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);\u00a0\u00a0\u00a0\u00a0 \/\/ set gravity for the Toast.\r\ntoast.setDuration(Toast.LENGTH_SHORT); \/\/ set the duration for the Toast.\r\ntoast.show(); \/\/ display the Toast\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. inflate(int, ViewGroup):<\/strong> <\/span>This method is used to inflate the layout from the xml. In this method first parameter is the layout resource ID and the second is the root View.<\/p>\n<p>Below we retrieve the Layout Inflater and then inflate the layout from the xml file.<\/p>\n<pre>\/\/ Retrieve the Layout Inflater and inflate the layout from xml\r\nLayoutInflater inflater = getLayoutInflater();\r\nView layout = inflater.inflate(R.layout.custom_toast_layout,\r\n(ViewGroup) findViewById(R.id.toast_layout_root));\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. setView(View):<\/strong><\/span> This method is used to set the view for the Toast. In this method we pass the inflated layout which we inflate using inflate() method.<\/p>\n<p>Below we firstly retrieve the layout inflater and then inflate the layout and finally create a new Toast and pass the inflated layout in the setView() method.<\/p>\n<pre>\/\/ Retrieve the Layout Inflater and inflate the layout from xml\r\nLayoutInflater inflater = getLayoutInflater();\r\nView layout = inflater.inflate(R.layout.custom_toast_layout,\r\n(ViewGroup) findViewById(R.id.toast_layout_root));\r\n\r\n\/\/ create a new Toast using context\r\nToast toast = new Toast(getApplicationContext());\r\ntoast.setDuration(Toast.LENGTH_LONG); \/\/ set the duration for the Toast\r\ntoast.setView(layout); \/\/ set the inflated layout\r\ntoast.show(); \/\/ display the custom Toast<\/pre>\n<hr \/>\n<h4><strong>\u00a0Custom Toast in Android:<\/strong><\/h4>\n<p>In Android, Sometimes simple Toast may not be satisfactory, and then we can go for customizing a Toast. For creating a custom layout, define a View layout, in XML and pass the root View object to the setView(View) method.<\/p>\n<h4><strong>Steps for Implementation of Custom Toast In Android:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span>\u00a0Firstly Retrieve the\u00a0Layout Inflater \u00a0with \u00a0getLayoutInflater() \u00a0(or\u00a0getSystemService()) and then inflate the layout from XML using\u00a0inflate(int, ViewGroup). In inflate method first parameter is the layout resource ID and the second is the root View.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Create a new Toast with\u00a0Toast(Context)\u00a0and set some properties of the Toast, such as the duration and gravity.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Call\u00a0setView(View)\u00a0and pass the inflated layout in this method.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Display the Toast on the screen using show() method of Toast.<\/p>\n<p>In the below example we have shown the functioning of Toast and custom Toast both.<\/p>\n<hr \/>\n<h4><strong>Toast And Custom Toast Example In Android Studio:<\/strong><\/h4>\n<p>Below is the example of Toast and Custom Toast in Android. In this example we display two Button\u2019s one for Simple Toast and other for Custom Toast and perform click event on them. Whenever a user click on simple Toast Button a Toast with message \u201cSimple Toast In Android\u201d displayed on the screen and when a user clicks on custom toast Button a message \u201cCustom Toast In Android\u201d with a image displayed on the screen. For Creating a custom toast we firstly retrieve the layout inflater and then inflate the custom toast layout from the xml file. After that we get the reference of TextView and ImageView from the inflated layout and set the text and image in the TextView and ImageView. Finally we create a new Toast and pass the inflated layout in the setView() method and then display the Toast by using show() method of Toast.<\/p>\n<p>Below is the final output, download Android Studio code and step by step explanation of the example:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ToastExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-490\" src=\"\/programming\/wp-content\/uploads\/2016\/07\/Toast-And-Custom-Toast-Example-in-Android.gif\" alt=\"Toast And Custom Toast Example in Android\" width=\"307\" height=\"476\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span>\u00a0Create a new project and name it ToastExample<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong> <\/span>\u00a0Open res -&gt; layout -&gt;activity_main.xml (or) main.xml and add following code:<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nxmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\nandroid:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\nandroid:paddingTop=\"@dimen\/activity_vertical_margin\"\r\ntools:context=\".MainActivity\"&gt;\r\n&lt;!-- Button's for simple and custom Toast --&gt;\r\n&lt;Button\r\nandroid:id=\"@+id\/simpleToast\"\r\nandroid:layout_width=\"200dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_marginTop=\"150dp\"\r\nandroid:background=\"#f00\"\r\nandroid:text=\"Simple Toast\"\r\nandroid:textColor=\"#fff\"\r\nandroid:textSize=\"20sp\" \/&gt;\r\n\r\n&lt;Button\r\nandroid:id=\"@+id\/customToast\"\r\nandroid:layout_width=\"200dp\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_below=\"@+id\/simpleToast\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_margin=\"50dp\"\r\nandroid:background=\"#0f0\"\r\nandroid:text=\"Custom Toast\"\r\nandroid:textColor=\"#fff\"\r\nandroid:textSize=\"20sp\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:\u00a0<\/strong><\/span>\u00a0Now create a xml layouts by\u00a0right clicking on res\/layout -&gt; New -&gt; Layout Resource\u00a0File\u00a0and name it custom_toast_layout.xml<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:id=\"@+id\/toast_layout_root\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"fill_parent\"\r\nandroid:background=\"#DAAA\"\r\nandroid:orientation=\"horizontal\"\r\nandroid:padding=\"8dp\"&gt;\r\n&lt;!-- ImageVView and TextView for custom Toast --&gt;\r\n&lt;ImageView\r\nandroid:id=\"@+id\/toastImageView\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_marginRight=\"8dp\" \/&gt;\r\n\r\n&lt;TextView\r\nandroid:id=\"@+id\/toastTextView\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:textColor=\"#FFF\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span>\u00a0Open\u00a0 \u00a0src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step we open\u00a0MainActivity\u00a0and\u00a0add the code for initiate the Button\u2019s and perform click event on Button\u2019s. Whenever a user click on simple Toast Button a Toast with message \u201cSimple Toast In Android\u201d displayed on the screen and when a user clicks on custom toast Button a message \u201cCustom Toast In Android\u201d with a image displayed on the screen. For Creating a custom toast we firstly retrieve the layout inflater and then inflate the custom toast layout from the xml file. After that we get the reference of TextView and ImageView from the inflated layout and set the text and image in the TextView and ImageView. Finally we create a new Toast and pass the inflated layout in the setView() method and then display the Toast by using show() method of Toast.<\/p>\n<pre>package com.abhiandroid.toastexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Gravity;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.widget.ImageView;\r\nimport android.widget.TextView;\r\nimport android.widget.Toast;\r\nimport android.widget.Button;\r\nimport android.view.ViewGroup;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    Button simpleToast, customToast;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ get the reference of Button's\r\n        simpleToast = (Button) findViewById(R.id.simpleToast);\r\n        customToast = (Button) findViewById(R.id.customToast);\r\n        \/\/ perform setOnClickListener event on simple Toast Button\r\n        simpleToast.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ initiate a Toast with message and duration\r\n                Toast toast = Toast.makeText(getApplicationContext(), \"Simple Toast In Android\", Toast.LENGTH_LONG); \/\/ initiate the Toast with context, message and duration for the Toast\r\n                toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);     \/\/ set gravity for the Toast.\r\n                toast.show(); \/\/ display the Toast\r\n\r\n            }\r\n        });\r\n        \/\/ perform setOnClickListener event on custom Toast Button\r\n        customToast.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ Retrieve the Layout Inflater and inflate the layout from xml\r\n                LayoutInflater inflater = getLayoutInflater();\r\n                View layout = inflater.inflate(R.layout.custom_toast_layout,\r\n                        (ViewGroup) findViewById(R.id.toast_layout_root));\r\n                \/\/ get the reference of TextView and ImageVIew from inflated layout\r\n                TextView toastTextView = (TextView) layout.findViewById(R.id.toastTextView);\r\n                ImageView toastImageView = (ImageView) layout.findViewById(R.id.toastImageView);\r\n                \/\/ set the text in the TextView\r\n                toastTextView.setText(\"Custom Toast In Android\");\r\n                \/\/ set the Image in the ImageView\r\n                toastImageView.setImageResource(R.drawable.ic_launcher);\r\n                \/\/ create a new Toast using context\r\n                Toast toast = new Toast(getApplicationContext());\r\n                toast.setDuration(Toast.LENGTH_LONG); \/\/ set the duration for the Toast\r\n                toast.setView(layout); \/\/ set the inflated layout\r\n                toast.show(); \/\/ display the custom Toast\r\n\r\n            }\r\n        });\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Android, Toast is used to display information for a period of time. It contains a message to be displayed quickly and disappears after specified period of time. It does not block the user interaction. Toast is a subclass of Object class. In this we use two constants for setting the duration for the Toast. &hellip; <a href=\"https:\/\/abhiandroid.com\/programming\/custom-toast-tutorial-example.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Toast &#038; Custom Toast With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,63],"tags":[],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-archieve","category-toast"],"psp_head":"<title>Toast &amp; Custom Toast With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"In Android, Toast is used to display information for a period of time. Tutorial on custom Toast with example in Android Studio.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/programming\/custom-toast-tutorial-example.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/comments?post=486"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":987,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/posts\/486\/revisions\/987"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}