<p>This article is about Android Toast and how to use it in android application with simple examples using Java and Kotlin.</p>



<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<span class="embed-youtube" style="text-align:center; display: block;"><amp-youtube data-videoid="H5xig2yTMAs" data-param-rel="1" data-param-showsearch="0" data-param-showinfo="1" data-param-iv_load_policy="1" data-param-fs="1" data-param-hl="en-US" data-param-autohide="2" data-param-wmode="transparent" width="1200" height="675" layout="responsive"><a href="https://www.youtube.com/watch?v=H5xig2yTMAs" placeholder><amp-img src="https://i.ytimg.com/vi/H5xig2yTMAs/hqdefault.jpg" alt="YouTube Poster" layout="fill" object-fit="cover"><noscript><img src="https://i.ytimg.com/vi/H5xig2yTMAs/hqdefault.jpg" loading="lazy" decoding="async" alt="YouTube Poster"></noscript></amp-img></a></amp-youtube></span>
</div></figure>



<div class="wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button is-style-outline is-style-outline--1"><a class="wp-block-button__link has-white-color has-text-color has-background" href="https://github.com/arunk7839/AndroidToastDemo" style="background-color:#560399" target="_blank" rel="noreferrer noopener"><strong>DOWNLOAD CODE</strong></a></div>
</div>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Toast</span></strong></h3>



<p>A <strong><span style="color:#04603e" class="has-inline-color">toast</span></strong> provides simple feedback about an operation in a small popup. ;</p>



<p>By default it appears near the bottom of the screen, centered horizontally.</p>



<p>Toasts are not clickable and automatically disappear after a timeout.</p>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Simple Toast</span></strong></h3>



<p>The below code snippet is an example of Simple Toast</p>



<pre class="wp-block-preformatted"><strong><em><span style="color:#04603e" class="has-inline-color">//get Toast object</span></em></strong>
Toast toast = Toast.<em>makeText</em>(getApplicationContext(), <strong>"Simple Toast"</strong>, Toast.<strong><em>LENGTH_LONG</em></strong>);

toast.show(); <strong><em><span style="color:#04603e" class="has-inline-color"> //show Toast</span></em></strong></pre>



<p>The ;<strong><span style="color:#04603e" class="has-inline-color">makeText</span></strong> ;method (makes a standard toast) takes three parameters:</p>



<p><strong><span style="color:#04603e" class="has-inline-color">Context</span></strong> ;: The context to use (Application or ;Activity ;object).</p>



<p><strong><span style="color:#04603e" class="has-inline-color">CharSequence</span></strong>: The text to show on Toast.</p>



<p><strong><span style="color:#04603e" class="has-inline-color">Duration</span></strong> ;: How long to display the message. ;It can be LENGTH_SHORT and LENGTH_LONG.</p>



<ul class="wp-block-list"><li><em><strong><span style="color:#520599" class="has-inline-color">LENGTH_SHORT</span></strong>: ;</em>Show the Toast for a short period of time.</li><li><em><strong><span style="color:#520599" class="has-inline-color">LENGTH_LONG</span></strong>: ;</em>Show the Toast for a long period of time.</li></ul>



<p><strong><span style="color:#04603e" class="has-inline-color">show()</span></strong>: Used to display the Toast on the screen.</p>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/09/Screenshot_1599812148-576x1024.png" alt="" class="wp-image-2156" width="512" height="910"/><figcaption><span class="has-inline-color has-vivid-red-color"><strong>Show standard Toast on click of SIMPLE TOAST button</strong></span></figcaption></figure></div>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Positioned Toast</span></strong></h3>



<p>Android Toast allows you to change the position of the Toast using the <strong><span style="color:#04603e" class="has-inline-color">setGravity()</span></strong> ;method. </p>



<p><strong>setGravity</strong> method accepts three parameters: a ;<a href="https://developer.android.com/reference/android/view/Gravity"><strong><span style="color:#04603e" class="has-inline-color">Gravity</span></strong></a> ;constant, an x-position offset, and a y-position offset.</p>



<p>For example, if you want the toast to appear at 400 distance(y-position) from the top-center, you can show the toast with gravity like this:</p>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//get Toast object</span></strong>
</em>Toast toast = Toast.<em>makeText</em>(getApplicationContext(), <strong>"Positioned Toast "</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>);
<em><strong><span style="color:#04603e" class="has-inline-color">//set toast gravity</span></strong>
</em><strong>toast.setGravity(Gravity.<em>CENTER </em>| Gravity.<em>TOP</em>, 0, 400);</strong>
<em><strong><span style="color:#04603e" class="has-inline-color">//show toast</span></strong>
</em>toast.show();</pre>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/09/Screenshot_1599812154-576x1024.png" alt="" class="wp-image-2157" width="493" height="877"/><figcaption><strong><span class="has-inline-color has-vivid-red-color">Show Toast at specific position</span></strong> <strong><span class="has-inline-color has-vivid-red-color">on click of POSITIONED TOAST button</span></strong></figcaption></figure></div>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Custom Toast</span></strong></h3>



<p>Android Toast allows you to create a customized layout for your Toast.</p>



<p>To create a custom layout, define a View layout, in XML. The following snippet contains a customized layout for Toast (saved as ;layout/custom_toast.xml):</p>



<p><strong><span style="color:#520599" class="has-inline-color">custom_toast.xml</span></strong></p>



<pre class="wp-block-preformatted"><;<strong>androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/custom_toast_container"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/toast_background"
 android:padding="@dimen/padding_12dp"</strong>>;

 <;<strong>ImageView
 android:id="@+id/toast_img"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toStartOf="@+id/toast_text"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintHorizontal_chainStyle="packed"
 app:layout_constraintStart_toStartOf="parent"
 android:layout_marginRight="@dimen/margin_10dp"
 app:layout_constraintTop_toTopOf="parent"
 app:srcCompat="@android:drawable/stat_sys_phone_call_on_hold" </strong>/>;

 <;<strong>TextView
 android:id="@+id/toast_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/custom_toast_text"
 android:textColor="@android:color/black"
 android:textSize="@dimen/textsize_15sp"
 app:layout_constraintBottom_toBottomOf="@+id/toast_img"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toEndOf="@+id/toast_img"
 app:layout_constraintTop_toTopOf="parent" </strong>/>;
<;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>>;</pre>



<p>You must use the ID of the ConstraintLayout element (&#8220;<strong>custom_toast_container</strong>&#8220;) and the ID of the XML layout file “<strong>custom_toast.xml</strong>” to inflate the layout as shown here:</p>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//get View object (using custom layout, created for Toast)</span></strong>
</em>View layout = getLayoutInflater().inflate(R.layout.<strong><em>custom_toast</em></strong>, (ViewGroup) findViewById(R.id.<strong><em>custom_toast_container</em></strong>));</pre>



<p>Now pass this ;<strong><span style="color:#04603e" class="has-inline-color">View</span></strong> ;object to the ;<span style="color:#520599" class="has-inline-color"><strong>setView()</strong> ;</span>method.</p>



<pre class="wp-block-preformatted">Toast toast = <strong>new </strong>Toast(getApplicationContext()); <em><strong><span style="color:#04603e" class="has-inline-color">//get Toast object</span></strong>

</em>toast.setDuration(Toast.<strong><em>LENGTH_LONG</em></strong>); <em><strong><span style="color:#04603e" class="has-inline-color">//set Toast duration</span></strong>
</em>toast.setGravity(Gravity.<strong><em>CENTER </em></strong>| Gravity.<strong><em>BOTTOM</em></strong>, 0, 300); <em><strong><span style="color:#04603e" class="has-inline-color">//set Toast gravity</span></strong>
</em><strong>toast.setView(layout); <em><span style="color:#04603e" class="has-inline-color">//set View object</span></em></strong><em>
</em>toast.show(); <em><strong><span style="color:#04603e" class="has-inline-color">//show Toast</span></strong></em></pre>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/09/Screenshot_1599812160-576x1024.png" alt="" class="wp-image-2158" width="499" height="887"/><figcaption><strong><span class="has-inline-color has-vivid-red-color">Show Toast with Custom View</span></strong> <strong><span class="has-inline-color has-vivid-red-color">on click of CUSTOM TOAST button</span></strong></figcaption></figure></div>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Creating New Project</span></strong></h3>



<p><strong>1</strong>. In ;<strong><span style="color:#04603e" class="has-inline-color">Android Studio</span></strong>, go to ;<strong><span style="color:#04603e" class="has-inline-color">File ⇒ New Project</span></strong>, fill all the details required to create a new project and then click on ;<strong><span style="color:#04603e" class="has-inline-color">finish</span></strong>.</p>



<p><strong>2</strong>. Rename the layout file ;<strong>activity_main.xml</strong> ;as ;<span style="color:#04603e" class="has-inline-color"><strong>activity_toast.xml</strong> ;</span>and add the below code. This layout file consist of three buttons, ;<strong>SIMPLE TOAST</strong> ;(To show Android standard toast), ;<strong>POSITIONED <strong>TOAST</strong></strong> ;(To show toast at specific position), ;<strong>CUSTOM <strong>TOAST</strong></strong> ;(To show toast with custom view).</p>



<p><strong><span style="color:#520599" class="has-inline-color">activity_toast.xml</span></strong></p>



<pre class="wp-block-preformatted"><em><;?</em><strong>xml version</strong><strong>="1.0" </strong><strong>encoding</strong><strong>="utf-8"</strong><em>?>;<br></em><;<strong>androidx.constraintlayout.widget.ConstraintLayout </strong><strong>xmlns:</strong><strong>android</strong><strong>="http://schemas.android.com/apk/res/android"<br></strong><strong> </strong><strong>xmlns:</strong><strong>app</strong><strong>="http://schemas.android.com/apk/res-auto"<br></strong><strong> </strong><strong>xmlns:</strong><strong>tools</strong><strong>="http://schemas.android.com/tools"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="match_parent"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="match_parent"<br></strong><strong> </strong><strong>android</strong><strong>:layout_margin</strong><strong>="@dimen/margin_20dp"<br></strong><strong> </strong><strong>tools</strong><strong>:context</strong><strong>=".ToastActivityKotlin"</strong>>;<br><br><br> <;<strong>Button<br></strong><strong> </strong><strong>android</strong><strong>:id</strong><strong>="@+id/btn_simple_toast"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="match_parent"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:text</strong><strong>="@string/btn_simple_toast"<br></strong><strong> </strong><strong>android</strong><strong>:padding</strong><strong>="@dimen/padding_12dp"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintBottom_toTopOf</strong><strong>="@+id/btn_positioned_toast"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintEnd_toEndOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintStart_toStartOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintTop_toTopOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintVertical_chainStyle</strong><strong>="packed" </strong>/>;<br><br> <;<strong>Button<br></strong><strong> </strong><strong>android</strong><strong>:id</strong><strong>="@+id/btn_positioned_toast"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="match_parent"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:layout_marginTop</strong><strong>="@dimen/margin_10dp"<br></strong><strong> </strong><strong>android</strong><strong>:layout_marginBottom</strong><strong>="@dimen/margin_10dp"<br></strong><strong> </strong><strong>android</strong><strong>:text</strong><strong>="@string/btn_positioned_toast"<br></strong><strong> </strong><strong>android</strong><strong>:padding</strong><strong>="@dimen/padding_12dp"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintBottom_toTopOf</strong><strong>="@+id/btn_custom_toast"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintEnd_toEndOf</strong><strong>="@+id/btn_simple_toast"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintHorizontal_bias</strong><strong>="0.5"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintStart_toStartOf</strong><strong>="@+id/btn_simple_toast"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintTop_toBottomOf</strong><strong>="@+id/btn_simple_toast" </strong>/>;<br><br> <;<strong>Button<br></strong><strong> </strong><strong>android</strong><strong>:id</strong><strong>="@+id/btn_custom_toast"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="match_parent"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:text</strong><strong>="@string/btn_custom_toast"<br></strong><strong> </strong><strong>android</strong><strong>:padding</strong><strong>="@dimen/padding_12dp"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintBottom_toBottomOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintEnd_toEndOf</strong><strong>="@+id/btn_positioned_toast"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintHorizontal_bias</strong><strong>="0.5"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintStart_toStartOf</strong><strong>="@+id/btn_positioned_toast"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintTop_toBottomOf</strong><strong>="@+id/btn_positioned_toast" </strong>/>;<br><br><;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>>;</pre>



<p><strong>3. ;</strong>Create a new layout file ;<strong><span style="color:#04603e" class="has-inline-color">custom_toast.xml</span></strong> ;(layout->;New->;Layout Resource File) and add the below code. This layout file represents the custom layout of toast.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>




<p><strong><span style="color:#520599" class="has-inline-color">custom_toast.xml</span></strong></p>



<pre class="wp-block-preformatted"><em><;?</em><strong>xml version="1.0" encoding="utf-8"</strong><em>?>;
</em><;<strong>androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/custom_toast_container"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/toast_background"
 android:padding="@dimen/padding_12dp"</strong>>;

 <;<strong>ImageView
 android:id="@+id/toast_img"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toStartOf="@+id/toast_text"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintHorizontal_chainStyle="packed"
 app:layout_constraintStart_toStartOf="parent"
 android:layout_marginRight="@dimen/margin_10dp"
 app:layout_constraintTop_toTopOf="parent"
 app:srcCompat="@android:drawable/stat_sys_phone_call_on_hold" </strong>/>;

 <;<strong>TextView
 android:id="@+id/toast_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/custom_toast_text"
 android:textColor="@android:color/black"
 android:textSize="@dimen/textsize_15sp"
 app:layout_constraintBottom_toBottomOf="@+id/toast_img"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toEndOf="@+id/toast_img"
 app:layout_constraintTop_toTopOf="parent" </strong>/>;
<;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>>;</pre>



<h4 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Complete code in Java</span></strong></h4>



<p>Below activity contains three buttons with click listener to show the different ways of implementing the toast in ;<strong><span style="color:#520599" class="has-inline-color">Java</span></strong>.</p>



<p><strong><span style="color:#520599" class="has-inline-color">SnackbarActivityJava.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package </strong>com.c1ctech.androidtoastdemo;

<strong>import </strong>android.os.Bundle;
<strong>import </strong>android.view.Gravity;
<strong>import </strong>android.view.View;
<strong>import </strong>android.view.ViewGroup;
<strong>import </strong>android.widget.Button;
<strong>import </strong>android.widget.TextView;
<strong>import </strong>android.widget.Toast;

<strong>import </strong>androidx.appcompat.app.AppCompatActivity;

<strong>public class </strong>ToastActivityJava <strong>extends </strong>AppCompatActivity <strong>implements </strong>View.OnClickListener {

 <strong>private </strong>Button <strong>btnSimpleToast</strong>, <strong>btnPositionedToast</strong>, <strong>btnCustomToast</strong>;

 @Override
 <strong>protected void </strong>onCreate(Bundle savedInstanceState) {
 <strong>super</strong>.onCreate(savedInstanceState);
 setContentView(R.layout.<strong><em>activity_toast</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//get buttons by its id</span></strong>
 </em><strong>btnSimpleToast </strong>= findViewById(R.id.<strong><em>btn_simple_toast</em></strong>);
 <strong>btnPositionedToast </strong>= findViewById(R.id.<strong><em>btn_positioned_toast</em></strong>);
 <strong>btnCustomToast </strong>= findViewById(R.id.<strong><em>btn_custom_toast</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//set click listener on buttons</span></strong>
 </em><strong>btnSimpleToast</strong>.setOnClickListener(<strong>this</strong>);
 <strong>btnPositionedToast</strong>.setOnClickListener(<strong>this</strong>);
 <strong>btnCustomToast</strong>.setOnClickListener(<strong>this</strong>);
 }


 @Override
 <strong>public void </strong>onClick(View view) {

 <strong>int </strong>id = view.getId();

 <strong>switch </strong>(id) {

 <strong>case </strong>R.id.<strong><em>btn_simple_toast</em></strong>:
 <em><strong><span style="color:#04603e" class="has-inline-color">//show simple toast</span></strong>
 </em>Toast.<em>makeText</em>(getApplicationContext(), <strong>"Simple Toast"</strong>, Toast.<strong><em>LENGTH_LONG</em></strong>).show();
 <strong>break</strong>;

 <strong>case </strong>R.id.<strong><em>btn_positioned_toast</em></strong>:
 <strong><span style="color:#04603e" class="has-inline-color"> <em>//get Toast object</em></span></strong><em>
 </em>Toast toast = Toast.<em>makeText</em>(getApplicationContext(), <strong>"Positioned Toast "</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>);
 <em><strong><span style="color:#04603e" class="has-inline-color">//set toast gravity</span></strong>
 </em>toast.setGravity(Gravity.<strong><em>CENTER </em></strong>| Gravity.<strong><em>TOP</em></strong>, 0, 400);
 <em><strong><span style="color:#04603e" class="has-inline-color">//show toast</span></strong>
 </em>toast.show();
 <strong>break</strong>;

 <strong>case </strong>R.id.<strong><em>btn_custom_toast</em></strong>:

 <em><strong><span style="color:#04603e" class="has-inline-color">//get View object (using custom layout, created for Toast)</span></strong>
 </em>View layout = getLayoutInflater().inflate(R.layout.<strong><em>custom_toast</em></strong>, (ViewGroup) findViewById(R.id.<strong><em>custom_toast_container</em></strong>));

 <em><strong><span style="color:#04603e" class="has-inline-color">//get TextView from View object</span></strong>
 </em>TextView text = layout.findViewById(R.id.<strong><em>toast_text</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//set text in textview</span></strong>
 </em>text.setText(<strong>"Your phone call is on hold"</strong>);

 toast = <strong>new </strong>Toast(getApplicationContext()); <em><strong><span style="color:#04603e" class="has-inline-color">//get Toast object</span></strong>

 </em>toast.setDuration(Toast.<strong><em>LENGTH_LONG</em></strong>); <em><strong><span style="color:#04603e" class="has-inline-color">//set Toast duration</span></strong>
 </em>toast.setGravity(Gravity.<strong><em>CENTER </em></strong>| Gravity.<strong><em>BOTTOM</em></strong>, 0, 300); <em><strong><span style="color:#04603e" class="has-inline-color">//set Toast gravity</span></strong>
 </em>toast.setView(layout); <em><strong><span style="color:#04603e" class="has-inline-color">//set View object</span></strong>
 </em>toast.show(); <em><strong><span style="color:#04603e" class="has-inline-color">//show Toast</span></strong>

 </em><strong>break</strong>;

 }
 }
}</pre>



<p>When you run the application it will look like this:</p>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/09/Screenshot_1599812140-576x1024.png" alt="" class="wp-image-2159" width="507" height="901"/></figure></div>



<h4 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Complete code in Kotlin</span></strong></h4>



<p>Create a new activity ;<strong><span style="color:#04603e" class="has-inline-color">SnackbarActivityKotlin.kt</span></strong> ;(New->;Activity->;Empty Activity->;select language kotlin) and then click on ;<strong>finish</strong>. This activity contains three buttons with click listener to show the different ways of implementing the toast in ;<strong><span style="color:#520599" class="has-inline-color">Kotlin</span></strong>.</p>



<p><strong><span style="color:#520599" class="has-inline-color">SnackbarActivityKotlin.kt</span></strong></p>



<pre class="wp-block-preformatted"><strong>package </strong>com.c1ctech.androidtoastdemo

<strong>import </strong>androidx.appcompat.app.AppCompatActivity
<strong>import </strong>android.os.Bundle
<strong>import </strong>android.view.Gravity
<strong>import </strong>android.view.View
<strong>import </strong>android.view.ViewGroup
<strong>import </strong>android.widget.Button
<strong>import </strong>android.widget.TextView
<strong>import </strong>android.widget.Toast

<strong>class </strong>ToastActivityKotlin : AppCompatActivity(), View.OnClickListener {
 <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {
 <strong>super</strong>.onCreate(savedInstanceState)
 setContentView(R.layout.<em>activity_toast</em>)

 <em><span style="color:#04603e" class="has-inline-color"><strong>//get buttons by its id</strong></span>
 </em><strong>val </strong>btnSimpleToast = findViewById<;Button>;(R.id.<em>btn_simple_toast</em>)
 <strong>val </strong>btnPositionedToast = findViewById<;Button>;(R.id.<em>btn_positioned_toast</em>)
 <strong>val </strong>btnCustomToast = findViewById<;Button>;(R.id.<em>btn_custom_toast</em>)

 <em><strong><span style="color:#04603e" class="has-inline-color">//set click listener on buttons</span></strong>
 </em>btnSimpleToast.setOnClickListener(<strong>this</strong>)
 btnPositionedToast.setOnClickListener(<strong>this</strong>)
 btnCustomToast.setOnClickListener(<strong>this</strong>)
 }

 <strong>override fun </strong>onClick(view: View) {

 <strong>val </strong>id = view.<em>id

 </em><strong>when </strong>(id) {

 <em><strong><span style="color:#04603e" class="has-inline-color">//show simple toast</span></strong>
 </em>R.id.<em>btn_simple_toast </em>->; Toast.makeText(<em>applicationContext</em>, <strong>"Simple Toast"</strong>, Toast.<em>LENGTH_LONG</em>).show()

 R.id.<em>btn_positioned_toast </em>->; {

 <em><strong><span style="color:#04603e" class="has-inline-color">//get Toast object</span></strong>
 </em><strong>val </strong>toast = Toast.makeText(<em>applicationContext</em>, <strong>"Positioned Toast "</strong>, Toast.<em>LENGTH_SHORT</em>)

 <em><strong><span style="color:#04603e" class="has-inline-color">//set Toast gravity</span></strong>
 </em>toast.setGravity(Gravity.<em>CENTER </em>or Gravity.<em>TOP</em>, 0, 400)

 <em><strong><span style="color:#04603e" class="has-inline-color">//show Toast</span></strong>
 </em>toast.show()
 }
 R.id.<em>btn_custom_toast </em>->; {

 <em><strong><span style="color:#04603e" class="has-inline-color">//get View object (using custom layout, created for Toast)</span></strong>
 </em><strong>val </strong>layout = <em>layoutInflater</em>.inflate(R.layout.<em>custom_toast</em>, findViewById(R.id.<em>custom_toast_container</em>) <strong>as? </strong>ViewGroup)

 <em><strong><span style="color:#04603e" class="has-inline-color">//get TextView from View object</span></strong>
 </em><strong>val </strong>text = layout.findViewById<;TextView>;(R.id.<em>toast_text</em>)

 <em><strong><span style="color:#04603e" class="has-inline-color">//set text in textview</span></strong>
 </em>text.<em>text </em>= getText(R.string.<em>custom_toast_text</em>) <em><strong><span style="color:#04603e" class="has-inline-color">//text.setText("Your phone call is on hold")</span></strong>

 </em><strong>val </strong>toast = Toast(<em>applicationContext</em>) <em><strong><span style="color:#04603e" class="has-inline-color">//get Toast object</span></strong>

 </em>toast.<em>duration </em>= Toast.<em>LENGTH_LONG <strong><span style="color:#04603e" class="has-inline-color">//set Toast duration
</span></strong>
 </em>toast.setGravity(Gravity.<em>CENTER </em>or Gravity.<em>BOTTOM</em>, 0, 300) <em><strong><span style="color:#04603e" class="has-inline-color">//set Toast gravity

 //set View object</span></strong>
 </em>toast.<em>view </em>= layout <em><strong><span style="color:#04603e" class="has-inline-color">//toast.setView(layout)</span></strong>

 </em>toast.show() <em><strong><span style="color:#04603e" class="has-inline-color">//show Toast</span></strong>
 </em>}
 }
 }
}</pre>


