A simple library to add custom toast to android applications..

Toast Library Tutorial

Toast Library Tutorial

Toast Library Tutorial

Toast Library Tutorial

Toast Library Tutorial

Step 1: Declare Jitpack

Add the JitPack repository to your build.gradle(project).

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

Step 2: Add Dependency

Add the dependency to your build.gradle(Module: app).

dependencies {
    implementation 'tech.developingdeveloper.toaster-android:toaster:2.3.0'
    implementation 'tech.developingdeveloper.toaster-android:toaster-ktx:2.3.0' //for ktx support
}

Please Note: toaster-ktx includes toaster module so, if you are using toaster-ktx version then you don’t have to add taoster

Step 3: Use

Video:

Toast Library Tutorial

A simple use case will look like this

Toaster.pop(
             this,
             "A simple toast message"
         ).show()

With a custom drawable

Toaster.pop(
              this,
              "A simple toast message with image",
              R.drawable.ic_baseline_cloud_done_24 /* image */
          ).show()

Toast Library Tutorial

Code Snippets

Using templates

  • Success
Toaster.popSuccess(
                  this,
                  "This is a success message",
                  Toaster.LENGTH_SHORT
              ).show()

Toast Library Tutorial

  • Warning
Toaster.popWarning(
              this,
              "This is a warning message",
              Toaster.LENGTH_SHORT
          ).show()

Toast Library Tutorial

  • Error
Toaster.popError(
              this,
              "This is an error message",
              Toaster.LENGTH_SHORT
          ).show()

Toast Library Tutorial

Custom Toast

val toastBuilder = Toaster.Builder(this)
              .setMessage("File uploaded successfully")
              .setLeftDrawable(R.drawable.ic_baseline_cloud_done_24)
              .setLeftDrawableTint(R.color.blue)
              .setStripTint(R.color.blue)
              .setDuration(Toaster.LENGTH_SHORT)
Toaster.pop(toastBuilder.make()).show()

Toast Library Tutorial

Custom Toast (toaster-ktx)

With the toaster-ktx, you can either make Taoster or directly create Toast with the provided functions.

  • Create Taoster usign ktx
val toaster = ToasterBuilderKtx.prepareToaster(this) {
    message = "File uploaded successfully"
    leftDrawableRes = R.drawable.ic_baseline_cloud_done_24
    leftDrawableTint = R.color.blue
    stripTint = R.color.blue
    duration = Toaster.LENGTH_SHORT
}
Toaster.pop(toaster).show()
  • Pop the Toast
Toaster.pop(toaster).show()
ToasterBuilderKtx.prepareToast(this) { 
        message = "File uploaded successfully" 
        leftDrawableRes = R.drawable.ic_baseline_cloud_done_24
        leftDrawableTint = R.color.blue
        stripTint = R.color.blue
        duration = Toaster.LENGTH_SHORT
}.show()

Toast Library Tutorial

Full Example

For a full Toaster Toast Library example project follow the following steps.

Step 1. Design Layouts

In Android we design our UI interfaces using XML. So let’s create the following layouts:

(a). activity_main.xml

Our activity_main layout.

Design your XML layout using the following 2 UI widgets and ViewGroups:

  1. androidx.constraintlayout.widget.ConstraintLayout
  2. Button
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="@dimen/margin_default"
        android:text="@string/show_toast_label"
        app:layout_constraintBottom_toTopOf="@id/with_image_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <Button
        android:id="@+id/with_image_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/margin_default"
        android:text="@string/show_toast_with_image_label"
        app:layout_constraintBottom_toTopOf="@id/builder_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button" />

    <Button
        android:id="@+id/builder_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/margin_default"
        android:text="@string/show_toast_using_builder_label"
        app:layout_constraintBottom_toTopOf="@id/success_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/with_image_button" />

    <Button
        android:id="@+id/success_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/margin_default"
        android:text="@string/show_success_toast_label"
        app:layout_constraintBottom_toTopOf="@id/warning_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/builder_button" />

    <Button
        android:id="@+id/warning_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/margin_default"
        android:text="@string/show_warning_toast_label"
        app:layout_constraintBottom_toTopOf="@id/error_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/success_button" />

    <Button
        android:id="@+id/error_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_error_toast_label"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/warning_button" />

</androidx.constraintlayout.widget.ConstraintLayout>

Step 2. Write Code

Finally we need to write our code as follows:

(a). KtxExampleActivity.kt

Our KtxExampleActivity class.

Here is the full code:

package replace_with_your_package_name

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import tech.developingdeveloper.toaster.Toaster
import tech.developingdeveloper.toasterexample.databinding.ActivityMainBinding
import tech.developingdeveloper.toasterktx.ToasterBuilderKtx

// This activity won't be visible in the app and it is only for reference.
// The output of this custom toast is same as custom toast using builder in MainActivity
class KtxExampleActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.builderButton.setOnClickListener {
            val toaster = ToasterBuilderKtx.prepareToaster(this) {
                message = "File uploaded successfully"
                leftDrawableRes = R.drawable.ic_baseline_cloud_done_24
                leftDrawableTint = R.color.blue
                stripTint = R.color.blue
                duration = Toaster.LENGTH_SHORT
            }

            Toaster.pop(toaster).show()

            // or

            ToasterBuilderKtx.prepareToast(this) {
                message = "File uploaded successfully"
                leftDrawableRes = R.drawable.ic_baseline_cloud_done_24
                leftDrawableTint = R.color.blue
                stripTint = R.color.blue
                duration = Toaster.LENGTH_SHORT
            }.show()
        }
    }
}

(b). MainActivity.kt

Our MainActivity class.

Here is the code:

package replace_with_your_package_name

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import tech.developingdeveloper.toaster.Toaster
import tech.developingdeveloper.toasterexample.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.button.setOnClickListener {
            Toaster.pop(
                this,
                "A simple toast message",
                Toaster.LENGTH_SHORT
            ).show()
        }

        binding.withImageButton.setOnClickListener {
            Toaster.pop(
                this,
                "A simple toast message with image",
                R.drawable.ic_baseline_cloud_done_24,
                Toaster.LENGTH_SHORT
            ).show()
        }

        binding.builderButton.setOnClickListener {
            val toastBuilder = Toaster.Builder(this)
                .setMessage("File uploaded successfully")
                .setLeftDrawable(R.drawable.ic_baseline_cloud_done_24)
                .setLeftDrawableTint(R.color.blue)
                .setStripTint(R.color.blue)
                .setDuration(Toaster.LENGTH_SHORT)
            Toaster.pop(toastBuilder.make()).show()
        }

        binding.errorButton.setOnClickListener {
            Toaster.popError(
                this,
                "This is an error message",
                Toaster.LENGTH_SHORT
            ).show()
        }

        binding.warningButton.setOnClickListener {
            Toaster.popWarning(
                this,
                "This is a warning message",
                Toaster.LENGTH_SHORT
            ).show()
        }

        binding.successButton.setOnClickListener {
            Toaster.popSuccess(
                this,
                "This is a success message",
                Toaster.LENGTH_SHORT
            ).show()
        }
    }
}

Reference

Download the code below:

No. Link
1. Download Full Code
2. Read more here.
3. Follow code author here.