Custom Toast in Android Using PoiziToast Library
Last Updated :
27 Mar, 2023
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
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);
}
public void show_toast(View view) {
PoiziToast.with( this )
.setBackgroundColor(getResources().getColor(R.color.teal_700))
.setIcon(R.drawable.ic_android_black_24dp)
.setGravity(Gravity.BOTTOM)
.setTextColor(R.color.black)
.setTextSize( 14 )
.makeToast( "Custom Toast" , Toast.LENGTH_SHORT)
.show();
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...