Toast example 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 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>
MainActivity.kt
Add the below code in your MainActivity.kt file, this file contains code on how to display toast on button click
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) //on button click displaying toastbutton.setOnClickListener {Toast.makeText(this,"Toast Example... !!!",Toast.LENGTH_LONG).show(); } } }Happy Reading 😊Thank YouSushmitha NHi 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
Post a Comment