Saturday, April 21, 2018

Custom Toast In Android Kotlin

create custom toast class android

A toast is used to display simple feedback about an operation in popup and automatically fade out after timeout. In this tutorial I am create a custom toast by using custom layout. Download source code from here:

activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">


    <Button
        android:id="@+id/btn_refresh"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Refresh Toast" />

    <Button
        android:id="@+id/btn_error2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:text="Info" />

    <Button
        android:id="@+id/btn_network"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:text="Network Toast" />

    <Button
        android:id="@+id/btn_error"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:text="Error Toast" />

</LinearLayout>

custom_toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp"
    android:background="#DAAA">
    <ImageView android:src="@drawable/ic_launcher_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_image"
        android:layout_marginRight="8dp"
        />
    <TextView android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        />
</LinearLayout>


MainActivity.kt:
package com.deepshikha.customtoast

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.custom_toast.view.*

class MainActivity : AppCompatActivity(), View.OnClickListener {

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

        listener()
    }

    private fun listener() {
        btn_refresh.setOnClickListener(this)
        btn_error2.setOnClickListener(this)
        btn_network.setOnClickListener(this)
        btn_error.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_error2 -> {
                var toast: Toast = Toast(this)
                toast.createToast(this, "Dummy data is benign information", Gravity.BOTTOM, Toast.LENGTH_SHORT, "#eab04c", R.drawable.ic_info_black_24dp)
            }

            R.id.btn_refresh -> {

                var toast: Toast = Toast(this)
                toast.createToast(this, "Data has been Refreshed", Gravity.BOTTOM, Toast.LENGTH_SHORT, "#2ba622", R.drawable.ic_autorenew_black_24dp)

            }

            R.id.btn_network -> {

                var toast: Toast = Toast(this)
                toast.createToast(this, "Check your Internet connection", Gravity.BOTTOM, Toast.LENGTH_SHORT, "#7c5ffa", R.drawable.ic_network_check_black_24dp)

            }

            R.id.btn_error -> {
                var toast: Toast = Toast(this)
                toast.createToast(this, "Error: Server not found", Gravity.BOTTOM, Toast.LENGTH_SHORT, "#ec1e1e", R.drawable.ic_error_outline_black_24dp)
            }
        }
    }

    fun Toast.createToast(context: Context, message: String, gravity: Int, duration: Int, backgroucolor: String, imagebackgroud: Int) {
        val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        /*first parameter is the layout you made
        second parameter is the root view in that xml
         */
        val layout = inflater.inflate(R.layout.custom_toast, (context as Activity).findViewById(R.id.custom_toast_container))

        layout.findViewById(R.id.tv_message).text = message
        layout.setBackgroundColor(Color.parseColor(backgroucolor))
        layout.findViewById(R.id.iv_image).iv_image.setImageResource(imagebackgroud)
        setGravity(gravity, 0, 100)
        setDuration(Toast.LENGTH_LONG);

        setView(layout);
        show()
    }

}

No comments:

Post a Comment