android kotlin toast example

In this Article today learn android kotlin toast example. toast is using the display message. from the android app. The message displayed using Toast class displays quickly android kotlin toast example. follow the source code kotlin toast example. for more information visit google developer.

follow the source code for android kotlin toast example.



1.MainActivity.kt:

package example.akash.kotlintoast  
  
import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import android.view.Gravity  
import android.widget.Toast  
import kotlinx.android.synthetic.main.activity_main.*  
  
class MainActivity : AppCompatActivity() {  
  
    override fun onCreate(savedInstanceState: Bundle?) {  
        super.onCreate(savedInstanceState)  
        setContentView(R.layout.activity_main)  
        button.setOnClickListener(){  
            Toast.makeText(applicationContext,"this is toast message",Toast.LENGTH_SHORT).show()  
  
            val toast = Toast.makeText(applicationContext, "Hello Developer", Toast.LENGTH_SHORT)  
            toast.show()  
  
            val myToast = Toast.makeText(applicationContext,"toast message with gravity",Toast.LENGTH_SHORT)  
            myToast.setGravity(Gravity.LEFT,200,200)  
            myToast.show()  
        }  
    }  
}  



2.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.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:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity">  
  
  
    <Button  
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="8dp"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="8dp"  
        android:text="Click to display toast"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  
</android.support.constraint.ConstraintLayout>  

Comments

Popular posts from this blog

Kotlin camera intent example

android kotlin current date example

android kotlin listview example