Skip to main content

Custom Toast in Android using Kotlin



Custom Toast in Android using Kotlin


   In previous article we have discussed on what is Toast and its syntax check out on this link if required What is toast and  basic example of toast using kotlin  Toast Example this will be continued article of previous 

activity_main.xml

Add the below code in your activity_main.xml file , this file contains button which on click will display the toast


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout        
xmlns:android="http://schemas.android.com/apk/res/android"     
   xmlns:tools="http://schemas.android.com/tools"   
     xmlns:app="http://schemas.android.com/apk/res-auto"    
    android:layout_width="match_parent"       
 android:layout_height="match_parent"   
     tools:context=".MainActivity"      
  android:orientation="vertical">


    <Button android:layout_width="match_parent"      
      android:layout_height="wrap_content"          
  android:id="@+id/button"       
     android:text="Toast Example"/>
</LinearLayout>


custom_toast.xml

Create custom_toast.xml layout file and design the toast as per your requirement in this example we have created an imageview and textview where textview displays the message and imageview displays the image given in source

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      
        android:layout_width="match_parent"      
        android:layout_height="match_parent"     
         android:id="@+id/linear"             
        android:orientation="vertical">

    <ImageView        
    android:layout_width="match_parent"        
       android:layout_height="wrap_content"     
           android:id="@+id/toast_img"         
       android:src="@drawable/logo"/>
    <TextView android:layout_width="match_parent"       
       android:layout_height="wrap_content"   
 android:id="@+id/toast_txt"  
  android:text="Welcome to SDK Book"/>
</LinearLayout>

MainActivity.kt

Add below code in MainActivity.kt class, in this class we are inflating the custom_toast.xml layout created and on button click the toast message will be displayed as in custom_toast

package com.example.kotlinsampleproject

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.custom_toast.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Inflating toast custom layout     
  val custom_layout= layoutInflater.inflate(R.layout.custom_toast,linear);

        //on button click displaying custom toast  
      button.setOnClickListener {        
    val mytoast=Toast(this)
        mytoast.duration=Toast.LENGTH_LONG     
        mytoast.view = custom_layout
        mytoast.show();

        }
    }
}


Happy Reading 😊
Thank You
Sushmitha N
Hi there ! Im a experienced android developer with 4years of revelant exprience.My Skills include Android,Core Java ,Oops and Kotlin.
Youtube Channel : SDK Book

Comments

Popular posts from this blog

Android Interview Questions

Android Interview Questions Which was the first mobile brand with Android Application?                           Answer :   First Mobile brand with Android Application was HTC. What is the current version of android and its API ?   Answer: Current version of android  is “Pie” (as per Nov 2018) and its API level 28. Life cycle of Activity ? Answer: OnCreate() - This method is called when the activity is created . It is called only once in activity lifecycle. OnStart() - This method is called when the activity is getting started , no user interactions will be done at this stage,and this method will be called several times in activity life cycle depending upon the user navigation.(Loads UI) OnResume()- On this method call user can interact with the activity and perform actions like click. OnPause()- W...
Find the biggest difference between any two numbers in array? Approach: Assume array of integer arr = [5,9,66,8,37]  Two calculate biggest difference between two numbers logic is largest number - smallest number . By arranging the array in ascending order we get the first element which is smallest and the last element which is largest. Now the array look like integer arr =[5,8,9,37,66] Here the solution is last element (largest number)- first element(smallest number) i.e; 66-5= 61 Implementing in Java /* Read input from STDIN. Print your output to STDOUT*/ import java.io.*; import java.util.*; public class BiggestDifference{   public static void main(String args[] ) throws Exception { //Takes user input Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //create an array depending upon the number of user inputs int[] array = new int[n]; //insert user input elements into the array for(int i = 0; i < n; i++) ...
Count how many numbers are negative in an array? Approach: Assume array of integer arr = [5,-9,66,-8,37] As  we know a negative number is a real number less than zero We are going to apply the same logic here , and check the number of negative numbers in the array. Expected output is : 2 ,since there are 2 negative numbers among the set of numbers Implementing in Java /* Read input from STDIN. Print your output to STDOUT*/ import java.io.*; import java.util.*; public class CountNegativeNumbers{   public static void main(String args[] ) throws Exception { //Write code here Scanner scan = new Scanner(System.in); int n = scan.nextInt();    // takes user input int a[] = new int[n];   // creates array depending on the user input int count=0;              //count variable which is zero initially for(...