Site icon C1CTech

Android Toast(Java/Kotlin)

&NewLine;<p>This article is about Android Toast and how to use it in android application with simple examples using Java and Kotlin&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"H5xig2yTMAs" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;H5xig2yTMAs" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;H5xig2yTMAs&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;H5xig2yTMAs&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button is-style-outline is-style-outline--1"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;AndroidToastDemo" style&equals;"background-color&colon;&num;560399" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Toast<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>A <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">toast<&sol;span><&sol;strong> provides simple feedback about an operation in a small popup&period;&nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>By default it appears near the bottom of the screen&comma; centered horizontally&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Toasts are not clickable and automatically disappear after a timeout&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Simple Toast<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>The below code snippet is an example of Simple Toast<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong><em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get Toast object<&sol;span><&sol;em><&sol;strong>&NewLine;Toast toast &equals; Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"Simple Toast"<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;toast&period;show&lpar;&rpar;&semi; <strong><em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"> &sol;&sol;show Toast<&sol;span><&sol;em><&sol;strong><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>The&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">makeText<&sol;span><&sol;strong>&nbsp&semi;method &lpar;makes a standard toast&rpar; takes three parameters&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">Context<&sol;span><&sol;strong>&nbsp&semi;&colon; The context to use &lpar;Application or&nbsp&semi;Activity&nbsp&semi;object&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">CharSequence<&sol;span><&sol;strong>&colon; The text to show on Toast&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">Duration<&sol;span><&sol;strong>&nbsp&semi;&colon; How long to display the message&period;&nbsp&semi;It can be LENGTH&lowbar;SHORT and LENGTH&lowbar;LONG&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><em><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">LENGTH&lowbar;SHORT<&sol;span><&sol;strong>&colon;&nbsp&semi;<&sol;em>Show the Toast for a short period of time&period;<&sol;li><li><em><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">LENGTH&lowbar;LONG<&sol;span><&sol;strong>&colon;&nbsp&semi;<&sol;em>Show the Toast for a long period of time&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">show&lpar;&rpar;<&sol;span><&sol;strong>&colon; Used to display the Toast on the screen&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screenshot&lowbar;1599812148-576x1024&period;png" alt&equals;"" class&equals;"wp-image-2156" width&equals;"512" height&equals;"910"&sol;><figcaption><span class&equals;"has-inline-color has-vivid-red-color"><strong>Show standard Toast on click of SIMPLE TOAST button<&sol;strong><&sol;span><&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Positioned Toast<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>Android Toast allows you to change the position of the Toast using the <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">setGravity&lpar;&rpar;<&sol;span><&sol;strong>&nbsp&semi;method&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>setGravity<&sol;strong> method accepts three parameters&colon; a&nbsp&semi;<a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;view&sol;Gravity"><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">Gravity<&sol;span><&sol;strong><&sol;a>&nbsp&semi;constant&comma; an x-position offset&comma; and a y-position offset&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>For example&comma; if you want the toast to appear at 400 distance&lpar;y-position&rpar; from the top-center&comma; you can show the toast with gravity like this&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get Toast object<&sol;span><&sol;strong>&NewLine;<&sol;em>Toast toast &equals; Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"Positioned Toast "<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&semi;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set toast gravity<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>toast&period;setGravity&lpar;Gravity&period;<em>CENTER <&sol;em>&vert; Gravity&period;<em>TOP<&sol;em>&comma; 0&comma; 400&rpar;&semi;<&sol;strong>&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show toast<&sol;span><&sol;strong>&NewLine;<&sol;em>toast&period;show&lpar;&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screenshot&lowbar;1599812154-576x1024&period;png" alt&equals;"" class&equals;"wp-image-2157" width&equals;"493" height&equals;"877"&sol;><figcaption><strong><span class&equals;"has-inline-color has-vivid-red-color">Show Toast at specific position<&sol;span><&sol;strong> <strong><span class&equals;"has-inline-color has-vivid-red-color">on click of POSITIONED TOAST button<&sol;span><&sol;strong><&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Custom Toast<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>Android Toast allows you to create a customized layout for your Toast&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>To create a custom layout&comma; define a View layout&comma; in XML&period; The following snippet contains a customized layout for Toast &lpar;saved as&nbsp&semi;layout&sol;custom&lowbar;toast&period;xml&rpar;&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">custom&lowbar;toast&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;custom&lowbar;toast&lowbar;container"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&commat;drawable&sol;toast&lowbar;background"&NewLine; android&colon;padding&equals;"&commat;dimen&sol;padding&lowbar;12dp"<&sol;strong>&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;toast&lowbar;img"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toStartOf&equals;"&commat;&plus;id&sol;toast&lowbar;text"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;chainStyle&equals;"packed"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; android&colon;layout&lowbar;marginRight&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&NewLine; app&colon;srcCompat&equals;"&commat;android&colon;drawable&sol;stat&lowbar;sys&lowbar;phone&lowbar;call&lowbar;on&lowbar;hold" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;toast&lowbar;text"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"&commat;string&sol;custom&lowbar;toast&lowbar;text"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;black"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textsize&lowbar;15sp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;toast&lowbar;img"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toEndOf&equals;"&commat;&plus;id&sol;toast&lowbar;img"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" <&sol;strong>&sol;&gt&semi;&NewLine;&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>You must use the ID of the ConstraintLayout element &lpar;&&num;8220&semi;<strong>custom&lowbar;toast&lowbar;container<&sol;strong>&&num;8220&semi;&rpar; and the ID of the XML layout file &OpenCurlyDoubleQuote;<strong>custom&lowbar;toast&period;xml<&sol;strong>” to inflate the layout as shown here&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get View object &lpar;using custom layout&comma; created for Toast&rpar;<&sol;span><&sol;strong>&NewLine;<&sol;em>View layout &equals; getLayoutInflater&lpar;&rpar;&period;inflate&lpar;R&period;layout&period;<strong><em>custom&lowbar;toast<&sol;em><&sol;strong>&comma; &lpar;ViewGroup&rpar; findViewById&lpar;R&period;id&period;<strong><em>custom&lowbar;toast&lowbar;container<&sol;em><&sol;strong>&rpar;&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>Now pass this&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">View<&sol;span><&sol;strong>&nbsp&semi;object to the&nbsp&semi;<span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color"><strong>setView&lpar;&rpar;<&sol;strong>&nbsp&semi;<&sol;span>method&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Toast toast &equals; <strong>new <&sol;strong>Toast&lpar;getApplicationContext&lpar;&rpar;&rpar;&semi; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get Toast object<&sol;span><&sol;strong>&NewLine;&NewLine;<&sol;em>toast&period;setDuration&lpar;Toast&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set Toast duration<&sol;span><&sol;strong>&NewLine;<&sol;em>toast&period;setGravity&lpar;Gravity&period;<strong><em>CENTER <&sol;em><&sol;strong>&vert; Gravity&period;<strong><em>BOTTOM<&sol;em><&sol;strong>&comma; 0&comma; 300&rpar;&semi; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set Toast gravity<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>toast&period;setView&lpar;layout&rpar;&semi; <em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set View object<&sol;span><&sol;em><&sol;strong><em>&NewLine;<&sol;em>toast&period;show&lpar;&rpar;&semi; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show Toast<&sol;span><&sol;strong><&sol;em><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screenshot&lowbar;1599812160-576x1024&period;png" alt&equals;"" class&equals;"wp-image-2158" width&equals;"499" height&equals;"887"&sol;><figcaption><strong><span class&equals;"has-inline-color has-vivid-red-color">Show Toast with Custom View<&sol;span><&sol;strong> <strong><span class&equals;"has-inline-color has-vivid-red-color">on click of CUSTOM TOAST button<&sol;span><&sol;strong><&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>1<&sol;strong>&period; In&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">Android Studio<&sol;span><&sol;strong>&comma; go to&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">File &Implies; New Project<&sol;span><&sol;strong>&comma; fill all the details required to create a new project and then click on&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">finish<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>2<&sol;strong>&period; Rename the layout file&nbsp&semi;<strong>activity&lowbar;main&period;xml<&sol;strong>&nbsp&semi;as&nbsp&semi;<span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>activity&lowbar;toast&period;xml<&sol;strong>&nbsp&semi;<&sol;span>and add the below code&period; This layout file consist of three buttons&comma;&nbsp&semi;<strong>SIMPLE TOAST<&sol;strong>&nbsp&semi;&lpar;To show Android standard toast&rpar;&comma;&nbsp&semi;<strong>POSITIONED <strong>TOAST<&sol;strong><&sol;strong>&nbsp&semi;&lpar;To show toast at specific position&rpar;&comma;&nbsp&semi;<strong>CUSTOM <strong>TOAST<&sol;strong><&sol;strong>&nbsp&semi;&lpar;To show toast with custom view&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">activity&lowbar;toast&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em>&lt&semi;&quest;<&sol;em><strong>xml version<&sol;strong><strong>&equals;"1&period;0" <&sol;strong><strong>encoding<&sol;strong><strong>&equals;"utf-8"<&sol;strong><em>&quest;&gt&semi;<br><&sol;em>&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>android<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br><&sol;strong><strong> <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>app<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br><&sol;strong><strong> <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>tools<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;margin<&sol;strong><strong>&equals;"&commat;dimen&sol;margin&lowbar;20dp"<br><&sol;strong><strong> <&sol;strong><strong>tools<&sol;strong><strong>&colon;context<&sol;strong><strong>&equals;"&period;ToastActivityKotlin"<&sol;strong>&gt&semi;<br><br><br> &lt&semi;<strong>Button<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;simple&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;text<&sol;strong><strong>&equals;"&commat;string&sol;btn&lowbar;simple&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;padding<&sol;strong><strong>&equals;"&commat;dimen&sol;padding&lowbar;12dp"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintBottom&lowbar;toTopOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;positioned&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toTopOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintVertical&lowbar;chainStyle<&sol;strong><strong>&equals;"packed" <&sol;strong>&sol;&gt&semi;<br><br> &lt&semi;<strong>Button<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;positioned&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;marginTop<&sol;strong><strong>&equals;"&commat;dimen&sol;margin&lowbar;10dp"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;marginBottom<&sol;strong><strong>&equals;"&commat;dimen&sol;margin&lowbar;10dp"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;text<&sol;strong><strong>&equals;"&commat;string&sol;btn&lowbar;positioned&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;padding<&sol;strong><strong>&equals;"&commat;dimen&sol;padding&lowbar;12dp"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintBottom&lowbar;toTopOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;custom&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;simple&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintHorizontal&lowbar;bias<&sol;strong><strong>&equals;"0&period;5"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;simple&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toBottomOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;simple&lowbar;toast" <&sol;strong>&sol;&gt&semi;<br><br> &lt&semi;<strong>Button<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;custom&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;text<&sol;strong><strong>&equals;"&commat;string&sol;btn&lowbar;custom&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;padding<&sol;strong><strong>&equals;"&commat;dimen&sol;padding&lowbar;12dp"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;positioned&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintHorizontal&lowbar;bias<&sol;strong><strong>&equals;"0&period;5"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;positioned&lowbar;toast"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toBottomOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;positioned&lowbar;toast" <&sol;strong>&sol;&gt&semi;<br><br>&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>3&period;&nbsp&semi;<&sol;strong>Create a new layout file&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">custom&lowbar;toast&period;xml<&sol;span><&sol;strong>&nbsp&semi;&lpar;layout-&gt&semi;New-&gt&semi;Layout Resource File&rpar; and add the below code&period; This layout file represents the custom layout of toast&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">custom&lowbar;toast&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em>&lt&semi;&quest;<&sol;em><strong>xml version&equals;"1&period;0" encoding&equals;"utf-8"<&sol;strong><em>&quest;&gt&semi;&NewLine;<&sol;em>&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;custom&lowbar;toast&lowbar;container"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&commat;drawable&sol;toast&lowbar;background"&NewLine; android&colon;padding&equals;"&commat;dimen&sol;padding&lowbar;12dp"<&sol;strong>&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;toast&lowbar;img"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toStartOf&equals;"&commat;&plus;id&sol;toast&lowbar;text"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;chainStyle&equals;"packed"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; android&colon;layout&lowbar;marginRight&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&NewLine; app&colon;srcCompat&equals;"&commat;android&colon;drawable&sol;stat&lowbar;sys&lowbar;phone&lowbar;call&lowbar;on&lowbar;hold" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;toast&lowbar;text"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"&commat;string&sol;custom&lowbar;toast&lowbar;text"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;black"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textsize&lowbar;15sp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;toast&lowbar;img"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toEndOf&equals;"&commat;&plus;id&sol;toast&lowbar;img"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" <&sol;strong>&sol;&gt&semi;&NewLine;&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Complete code in Java<&sol;span><&sol;strong><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p>Below activity contains three buttons with click listener to show the different ways of implementing the toast in&nbsp&semi;<strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Java<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">SnackbarActivityJava&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;androidtoastdemo&semi;&NewLine;&NewLine;<strong>import <&sol;strong>android&period;os&period;Bundle&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;Gravity&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;View&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;ViewGroup&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;Button&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;TextView&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;Toast&semi;&NewLine;&NewLine;<strong>import <&sol;strong>androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;&NewLine;<strong>public class <&sol;strong>ToastActivityJava <strong>extends <&sol;strong>AppCompatActivity <strong>implements <&sol;strong>View&period;OnClickListener &lbrace;&NewLine;&NewLine; <strong>private <&sol;strong>Button <strong>btnSimpleToast<&sol;strong>&comma; <strong>btnPositionedToast<&sol;strong>&comma; <strong>btnCustomToast<&sol;strong>&semi;&NewLine;&NewLine; &commat;Override&NewLine; <strong>protected void <&sol;strong>onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; <strong>super<&sol;strong>&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;<strong><em>activity&lowbar;toast<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get buttons by its id<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>btnSimpleToast <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;simple&lowbar;toast<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <strong>btnPositionedToast <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;positioned&lowbar;toast<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <strong>btnCustomToast <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;custom&lowbar;toast<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set click listener on buttons<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>btnSimpleToast<&sol;strong>&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine; <strong>btnPositionedToast<&sol;strong>&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine; <strong>btnCustomToast<&sol;strong>&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; <strong>int <&sol;strong>id &equals; view&period;getId&lpar;&rpar;&semi;&NewLine;&NewLine; <strong>switch <&sol;strong>&lpar;id&rpar; &lbrace;&NewLine;&NewLine; <strong>case <&sol;strong>R&period;id&period;<strong><em>btn&lowbar;simple&lowbar;toast<&sol;em><&sol;strong>&colon;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show simple toast<&sol;span><&sol;strong>&NewLine; <&sol;em>Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"Simple Toast"<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine; <strong>break<&sol;strong>&semi;&NewLine;&NewLine; <strong>case <&sol;strong>R&period;id&period;<strong><em>btn&lowbar;positioned&lowbar;toast<&sol;em><&sol;strong>&colon;&NewLine; <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"> <em>&sol;&sol;get Toast object<&sol;em><&sol;span><&sol;strong><em>&NewLine; <&sol;em>Toast toast &equals; Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"Positioned Toast "<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set toast gravity<&sol;span><&sol;strong>&NewLine; <&sol;em>toast&period;setGravity&lpar;Gravity&period;<strong><em>CENTER <&sol;em><&sol;strong>&vert; Gravity&period;<strong><em>TOP<&sol;em><&sol;strong>&comma; 0&comma; 400&rpar;&semi;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show toast<&sol;span><&sol;strong>&NewLine; <&sol;em>toast&period;show&lpar;&rpar;&semi;&NewLine; <strong>break<&sol;strong>&semi;&NewLine;&NewLine; <strong>case <&sol;strong>R&period;id&period;<strong><em>btn&lowbar;custom&lowbar;toast<&sol;em><&sol;strong>&colon;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get View object &lpar;using custom layout&comma; created for Toast&rpar;<&sol;span><&sol;strong>&NewLine; <&sol;em>View layout &equals; getLayoutInflater&lpar;&rpar;&period;inflate&lpar;R&period;layout&period;<strong><em>custom&lowbar;toast<&sol;em><&sol;strong>&comma; &lpar;ViewGroup&rpar; findViewById&lpar;R&period;id&period;<strong><em>custom&lowbar;toast&lowbar;container<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get TextView from View object<&sol;span><&sol;strong>&NewLine; <&sol;em>TextView text &equals; layout&period;findViewById&lpar;R&period;id&period;<strong><em>toast&lowbar;text<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set text in textview<&sol;span><&sol;strong>&NewLine; <&sol;em>text&period;setText&lpar;<strong>"Your phone call is on hold"<&sol;strong>&rpar;&semi;&NewLine;&NewLine; toast &equals; <strong>new <&sol;strong>Toast&lpar;getApplicationContext&lpar;&rpar;&rpar;&semi; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get Toast object<&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em>toast&period;setDuration&lpar;Toast&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set Toast duration<&sol;span><&sol;strong>&NewLine; <&sol;em>toast&period;setGravity&lpar;Gravity&period;<strong><em>CENTER <&sol;em><&sol;strong>&vert; Gravity&period;<strong><em>BOTTOM<&sol;em><&sol;strong>&comma; 0&comma; 300&rpar;&semi; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set Toast gravity<&sol;span><&sol;strong>&NewLine; <&sol;em>toast&period;setView&lpar;layout&rpar;&semi; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set View object<&sol;span><&sol;strong>&NewLine; <&sol;em>toast&period;show&lpar;&rpar;&semi; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show Toast<&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em><strong>break<&sol;strong>&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>When you run the application it will look like this&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screenshot&lowbar;1599812140-576x1024&period;png" alt&equals;"" class&equals;"wp-image-2159" width&equals;"507" height&equals;"901"&sol;><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Complete code in Kotlin<&sol;span><&sol;strong><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p>Create a new activity&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">SnackbarActivityKotlin&period;kt<&sol;span><&sol;strong>&nbsp&semi;&lpar;New-&gt&semi;Activity-&gt&semi;Empty Activity-&gt&semi;select language kotlin&rpar; and then click on&nbsp&semi;<strong>finish<&sol;strong>&period; This activity contains three buttons with click listener to show the different ways of implementing the toast in&nbsp&semi;<strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Kotlin<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">SnackbarActivityKotlin&period;kt<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;androidtoastdemo&NewLine;&NewLine;<strong>import <&sol;strong>androidx&period;appcompat&period;app&period;AppCompatActivity&NewLine;<strong>import <&sol;strong>android&period;os&period;Bundle&NewLine;<strong>import <&sol;strong>android&period;view&period;Gravity&NewLine;<strong>import <&sol;strong>android&period;view&period;View&NewLine;<strong>import <&sol;strong>android&period;view&period;ViewGroup&NewLine;<strong>import <&sol;strong>android&period;widget&period;Button&NewLine;<strong>import <&sol;strong>android&period;widget&period;TextView&NewLine;<strong>import <&sol;strong>android&period;widget&period;Toast&NewLine;&NewLine;<strong>class <&sol;strong>ToastActivityKotlin &colon; AppCompatActivity&lpar;&rpar;&comma; View&period;OnClickListener &lbrace;&NewLine; <strong>override fun <&sol;strong>onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;&NewLine; <strong>super<&sol;strong>&period;onCreate&lpar;savedInstanceState&rpar;&NewLine; setContentView&lpar;R&period;layout&period;<em>activity&lowbar;toast<&sol;em>&rpar;&NewLine;&NewLine; <em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;get buttons by its id<&sol;strong><&sol;span>&NewLine; <&sol;em><strong>val <&sol;strong>btnSimpleToast &equals; findViewById&lt&semi;Button&gt&semi;&lpar;R&period;id&period;<em>btn&lowbar;simple&lowbar;toast<&sol;em>&rpar;&NewLine; <strong>val <&sol;strong>btnPositionedToast &equals; findViewById&lt&semi;Button&gt&semi;&lpar;R&period;id&period;<em>btn&lowbar;positioned&lowbar;toast<&sol;em>&rpar;&NewLine; <strong>val <&sol;strong>btnCustomToast &equals; findViewById&lt&semi;Button&gt&semi;&lpar;R&period;id&period;<em>btn&lowbar;custom&lowbar;toast<&sol;em>&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set click listener on buttons<&sol;span><&sol;strong>&NewLine; <&sol;em>btnSimpleToast&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&NewLine; btnPositionedToast&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&NewLine; btnCustomToast&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <strong>override fun <&sol;strong>onClick&lpar;view&colon; View&rpar; &lbrace;&NewLine;&NewLine; <strong>val <&sol;strong>id &equals; view&period;<em>id&NewLine;&NewLine; <&sol;em><strong>when <&sol;strong>&lpar;id&rpar; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show simple toast<&sol;span><&sol;strong>&NewLine; <&sol;em>R&period;id&period;<em>btn&lowbar;simple&lowbar;toast <&sol;em>-&gt&semi; Toast&period;makeText&lpar;<em>applicationContext<&sol;em>&comma; <strong>"Simple Toast"<&sol;strong>&comma; Toast&period;<em>LENGTH&lowbar;LONG<&sol;em>&rpar;&period;show&lpar;&rpar;&NewLine;&NewLine; R&period;id&period;<em>btn&lowbar;positioned&lowbar;toast <&sol;em>-&gt&semi; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get Toast object<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>val <&sol;strong>toast &equals; Toast&period;makeText&lpar;<em>applicationContext<&sol;em>&comma; <strong>"Positioned Toast "<&sol;strong>&comma; Toast&period;<em>LENGTH&lowbar;SHORT<&sol;em>&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set Toast gravity<&sol;span><&sol;strong>&NewLine; <&sol;em>toast&period;setGravity&lpar;Gravity&period;<em>CENTER <&sol;em>or Gravity&period;<em>TOP<&sol;em>&comma; 0&comma; 400&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show Toast<&sol;span><&sol;strong>&NewLine; <&sol;em>toast&period;show&lpar;&rpar;&NewLine; &rcub;&NewLine; R&period;id&period;<em>btn&lowbar;custom&lowbar;toast <&sol;em>-&gt&semi; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get View object &lpar;using custom layout&comma; created for Toast&rpar;<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>val <&sol;strong>layout &equals; <em>layoutInflater<&sol;em>&period;inflate&lpar;R&period;layout&period;<em>custom&lowbar;toast<&sol;em>&comma; findViewById&lpar;R&period;id&period;<em>custom&lowbar;toast&lowbar;container<&sol;em>&rpar; <strong>as&quest; <&sol;strong>ViewGroup&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get TextView from View object<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>val <&sol;strong>text &equals; layout&period;findViewById&lt&semi;TextView&gt&semi;&lpar;R&period;id&period;<em>toast&lowbar;text<&sol;em>&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set text in textview<&sol;span><&sol;strong>&NewLine; <&sol;em>text&period;<em>text <&sol;em>&equals; getText&lpar;R&period;string&period;<em>custom&lowbar;toast&lowbar;text<&sol;em>&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;text&period;setText&lpar;"Your phone call is on hold"&rpar;<&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em><strong>val <&sol;strong>toast &equals; Toast&lpar;<em>applicationContext<&sol;em>&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get Toast object<&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em>toast&period;<em>duration <&sol;em>&equals; Toast&period;<em>LENGTH&lowbar;LONG <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set Toast duration&NewLine;<&sol;span><&sol;strong>&NewLine; <&sol;em>toast&period;setGravity&lpar;Gravity&period;<em>CENTER <&sol;em>or Gravity&period;<em>BOTTOM<&sol;em>&comma; 0&comma; 300&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set Toast gravity&NewLine;&NewLine; &sol;&sol;set View object<&sol;span><&sol;strong>&NewLine; <&sol;em>toast&period;<em>view <&sol;em>&equals; layout <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;toast&period;setView&lpar;layout&rpar;<&sol;span><&sol;strong>&NewLine;&NewLine; <&sol;em>toast&period;show&lpar;&rpar; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show Toast<&sol;span><&sol;strong>&NewLine; <&sol;em>&rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;

Exit mobile version