Android to create Custom Toasts with TOASTY

Full source code to create using an awesome library called Toasty to create different custom Toast messages like Error, Success, Info and Warning Toasts and Toasts with an image

Add this in your root build.gradle file (not your module build.gradle file)

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

Add this to your module’s build.gradle file 

dependencies {
	...
	implementation 'com.github.GrenderG:Toasty:1.5.2'
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.application.toastyproject.MainActivity">

    <Button
        android:id="@+id/button_error"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showToast"
        android:text="show error toast" />

    <Button
        android:id="@+id/button_success"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showToast"
        android:text="show success toast" />

    <Button
        android:id="@+id/button_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showToast"
        android:text="show info toast" />

    <Button
        android:id="@+id/button_warning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showToast"
        android:text="show warning toast" />

    <Button
        android:id="@+id/button_normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showToast"
        android:text="show normal toast with an icon" />

</LinearLayout>

MainActivity.java

package com.example.application.toastyproject;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import es.dmoral.toasty.Toasty;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showToast(View v) {
        switch (v.getId()) {
            case R.id.button_error:
                Toasty.error(this, "This is an error Toast", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_success:
                Toasty.success(this, "This is a success Toast", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_info:
                Toasty.info(this, "This is an info Toast", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_warning:
                Toasty.warning(this, "This is a warning Toast", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_normal:
                Toasty.normal(this, "This is a normal Toast", Toast.LENGTH_SHORT, ContextCompat.getDrawable(this, R.drawable.ic_android_black_24dp)).show();
                break;
        }
    }
}

Leave a Comment