Open In App

Custom Toast in Android Using PoiziToast Library

Last Updated : 27 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Toast provides information/feedback about an operation. It is a small popup that appears on the current activity UI and automatically disappears after a timeout. In this article, we will learn how to create customized Toast using PoiziToast Library in android. So, we will understand this by making a simple app to display customized Toast.

What is PoiziToast Library?

The PoiziToast library builds upon the Android Toast Class and is used to enhance it. This library offers many customization options that can be used on Android Toast Class.

Step-by-Step Implementation

Step 1: Create a new project in Android Studio and select Java as the language. If you are new to Android refer to How to Create/Start a New Project in Android Studio.

Step 2: Navigate to Gradle Scripts > settings.gradle(Project Setting) and the JitPack repository inside repositories in dependencyResolutionManagement {}.

allprojects {
repositories {
 …
 maven { url “https://jitpack.io” }
}
}

Step 3: Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section and Sync the project by clicking on Sync Now option appearing in the top right corner.   

implementation ‘com.github.farshidabz:poizitoast:v1.0.1’

Step 4: Navigate to the  app > res > layout > activity_main.xml and add the below code to that file

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
   <Button
      android:layout_width="140dp"
      android:layout_height="60dp"
      android:text="Custom Toast"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      android:layout_marginTop="30dp"
      android:onClick="show_toast">
   </Button>
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 5: Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. 

Java




package com.example.customtoast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
  
import com.farshidabz.poizitoast.PoiziToast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  
    // this function will be called when
    // the button is clicked
    public void show_toast(View view) {
  
          
        PoiziToast.with(this)
  
                // Set the background color for the Toast
                .setBackgroundColor(getResources().getColor(R.color.teal_700))
  
                // Set the icon for the Toast
                .setIcon(R.drawable.ic_android_black_24dp)
  
                // Set the gravity for the Toast
                // in this case Toast will appear at the bottom
                .setGravity(Gravity.BOTTOM)
  
                // Set the text color
                .setTextColor(R.color.black)
  
                // Set the size of the text
                .setTextSize(14)
  
                // Set the Toast Message and it's length
                .makeToast("Custom Toast", Toast.LENGTH_SHORT)
  
                // Display the Toast
                .show();
    }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads