Toast Libraries
Curation of some of the best android Toast libraries this year.
Spikeysanju/MotionToast
đ A Beautiful Motion Toast Library for Kotlin Android.
A Beautiful Multipurpose Motion Toast Library in Android using Kotlin.
Preview - Motion Toast đ
Preview - Color Motion Toast đ
Preview - Dark Toast đ
Preview - Dark Color Toast đ
Types of Toast Style â¤ī¸
Step 1: Installation
Dependency Project Level
Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:
Dependency App Level:
Add dependency in your app module
Step 2: Usage
Five Toast Types
Toast Duration âī¸
Success Toast
MotionToast.createToast(this,
"Hurray success đ"
"Upload Completed successfully!",
MotionToastStyle.SUCCESS,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Error Toast
MotionToast.createToast(this,
"Failed âšī¸"
"Profile Update Failed!",
MotionToastStyle.ERROR,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Warning Toast
MotionToast.createToast(this,"Please fill all the details!",
MotionToastStyle.WARNING,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Info Toast
MotionToast.createToast(this,"This is information toast!",
MotionToastStyle.INFO,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Delete Toast
MotionToast.createToast(this,"Delete all history!",
MotionToastStyle.DELETE,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Sample Code for - Color Motion Toast đ
Success Toast
MotionToast.createColorToast(this,"Upload Completed!",
MotionToastStyle.SUCCESS,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Error Toast
MotionToast.createColorToast(this,"Profile Update Failed!",
MotionToastStyle.ERROR,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Warning Toast
MotionToast.createColorToast(this,"Please fill all the details!",
MotionToastStyle.WARNING,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Info Toast
MotionToast.createColorToast(this,"This is information toast!",
MotionToastStyle.INFO,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Delete Toast
MotionToast.createColorToast(this,"Delete all history!",
MotionToastStyle.DELETE,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Success Toast
MotionToast.darkToast(this,"Upload Completed!",
MotionToastStyle.SUCCESS,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Error Toast
MotionToast.darkToast(this,"Profile Update Failed!",
MotionToastStyle.ERROR,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Warning Toast
MotionToast.darkToast(this,"Please fill all the details!",
MotionToastStyle.WARNING,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Info Toast
MotionToast.darkToast(this,"This is information toast!",
MotionToastStyle.INFO,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Delete Toast
MotionToast.darkToast(this,"Delete all history!",
MotionToastStyle.DELETE,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Sample Code for - Dark Color Toast đđ¤đ
Success Toast
MotionToast.darkColorToast(this,"Upload Completed!",
MotionToastStyle.SUCCESS,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Error Toast
MotionToast.darkColorToast(this,"Profile Update Failed!",
MotionToastStyle.ERROR,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Warning Toast
MotionToast.darkColorToast(this,"Please fill all the details!",
MotionToastStyle.WARNING,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Info Toast
MotionToast.darkColorToast(this,"This is information toast!",
MotionToastStyle.INFO,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Delete Toast
MotionToast.darkColorToast(this,"Delete all history!",
MotionToastStyle.DELETE,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this,R.font.helvetica_regular))
Full Example
For a full MotionToast
Toast Library example project follow the following steps.
Step 1. Design Layouts
(a). activity_main.xml
Our
activity_main
layout.
First inside your /res/layout/
directory create an xml layout file named activity_main.xml
.
Then design your XML layout using the following 4 UI widgets and ViewGroups:
RelativeLayout
LinearLayout
Switch
Button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_centerInParent="true"
android:orientation="vertical">
<Switch
android:id="@+id/switch_custom_colors"
android:layout_width="200dp"
android:text="Custom toast colors"
android:layout_height="wrap_content" />
<Button
android:id="@+id/successBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Success"
android:fontFamily="@font/helveticabold"/>
<Button
android:id="@+id/errorBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Error"
android:fontFamily="@font/helveticabold"/>
<Button
android:id="@+id/warningBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Warning"
android:fontFamily="@font/helveticabold"/>
<Button
android:id="@+id/infoBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Info"
android:fontFamily="@font/helveticabold"/>
<Button
android:id="@+id/deleteBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Delete"
android:fontFamily="@font/helveticabold"/>
<Button
android:id="@+id/noInternetBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:fontFamily="@font/helveticabold"
android:text="No Internet" />
</LinearLayout>
</RelativeLayout>
(b). motion_toast.xml
Our
motion_toast
layout.
Moreover design your XML layout using the following 5 UI widgets and ViewGroups:
RelativeLayout
View
ImageView
LinearLayout
TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/motion_toast_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="@dimen/dimen_24"
android:background="@android:color/white">
<View
android:id="@+id/colorView"
android:layout_width="@dimen/dimen_12"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:background="@drawable/color_view_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/custom_toast_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="@dimen/dimen_8"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/colorView"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_info_yellow" />
<LinearLayout
android:id="@+id/custom_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/dimen_8"
android:layout_marginEnd="@dimen/dimen_8"
android:layout_toEndOf="@id/custom_toast_image"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="@+id/custom_toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dimen_12"
android:layout_marginTop="@dimen/dimen_12"
android:layout_marginEnd="@dimen/dimen_12"
android:fontFamily="@font/montserrat_bold"
android:letterSpacing="0.10"
android:text="@string/text_warning"
android:textAllCaps="true"
android:textAlignment="textStart"
android:textAppearance="@style/TextAppearance.MaterialComponents.Caption"
android:textColor="@android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/custom_toast_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/custom_toast_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dimen_12"
android:layout_marginTop="@dimen/dimen_4"
android:layout_marginEnd="@dimen/dimen_12"
android:layout_marginBottom="@dimen/dimen_12"
android:ellipsize="marquee"
android:fontFamily="@font/montserrat_regular"
android:maxLines="2"
android:text="@string/text_mock_description"
android:textAlignment="textStart"
android:textAppearance="@style/TextAppearance.MaterialComponents.Body2"
android:textColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/custom_toast_image"
app:layout_constraintTop_toBottomOf="@id/custom_toast_text" />
</LinearLayout>
</RelativeLayout>
(c). full_color_toast.xml
Our
full_color_toast
layout.
RelativeLayout
ImageView
LinearLayout
TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/color_toast_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="@dimen/dimen_24"
android:background="@drawable/toast_round_background"
android:backgroundTint="@color/warning_color">
<ImageView
android:id="@+id/color_toast_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/dimen_12"
android:background="@drawable/color_view_background"
android:src="@drawable/ic_warning_yellow"
android:contentDescription="@string/app_name"
android:padding="@dimen/dimen_8" />
<LinearLayout
android:id="@+id/custom_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/dimen_8"
android:layout_marginEnd="@dimen/dimen_8"
android:layout_toEndOf="@id/color_toast_image"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="@+id/color_toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dimen_12"
android:layout_marginTop="@dimen/dimen_12"
android:layout_marginEnd="@dimen/dimen_12"
android:fontFamily="@font/montserrat_bold"
android:letterSpacing="0.10"
android:text="@string/text_warning"
android:textAllCaps="true"
android:textAlignment="textStart"
android:textAppearance="@style/TextAppearance.MaterialComponents.Caption"
android:textColor="@android:color/white"
app:layout_constraintStart_toEndOf="@id/color_toast_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/color_toast_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dimen_12"
android:layout_marginTop="@dimen/dimen_4"
android:layout_marginEnd="@dimen/dimen_12"
android:layout_marginBottom="@dimen/dimen_12"
android:ellipsize="marquee"
android:fontFamily="@font/montserrat_regular"
android:maxLines="2"
android:text="@string/text_mock_description"
android:textAlignment="textStart"
android:textAppearance="@style/TextAppearance.MaterialComponents.Body2"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/color_toast_image"
app:layout_constraintTop_toBottomOf="@id/color_toast_text" />
</LinearLayout>
</RelativeLayout>
Step 2. Write Code
Finally we need to write our code as follows:
(a). MainActivity.kt
Our
MainActivity
class.
Just copy the code below and replace the package with your app's package name.
package replace_with_your_package_name
import android.os.Bundle
import android.view.View
import android.widget.CompoundButton
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import www.sanju.motiontoast.MotionToast
import www.sanju.motiontoast.MotionToastStyle
import www.sanju.motiontoastapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity(), View.OnClickListener, View.OnLongClickListener,
CompoundButton.OnCheckedChangeListener {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.switchCustomColors.setOnCheckedChangeListener(this)
binding.successBtn.setOnClickListener(this)
binding.errorBtn.setOnClickListener(this)
binding.warningBtn.setOnClickListener(this)
binding.infoBtn.setOnClickListener(this)
binding.deleteBtn.setOnClickListener(this)
binding.noInternetBtn.setOnClickListener(this)
binding.successBtn.setOnLongClickListener(this)
binding.errorBtn.setOnLongClickListener(this)
binding.warningBtn.setOnLongClickListener(this)
binding.infoBtn.setOnLongClickListener(this)
binding.deleteBtn.setOnLongClickListener(this)
binding.noInternetBtn.setOnLongClickListener(this)
}
private fun setToastColors(newColorsEnabled: Boolean) {
if (newColorsEnabled) {
MotionToast.setSuccessColor(R.color.custom_success_color)
MotionToast.setErrorColor(R.color.custom_error_color)
MotionToast.setDeleteColor(R.color.custom_delete_color)
MotionToast.setWarningColor(R.color.custom_warning_color)
MotionToast.setInfoColor(R.color.custom_info_color)
MotionToast.setSuccessBackgroundColor(R.color.success_bg_color)
MotionToast.setErrorBackgroundColor(R.color.error_bg_color)
MotionToast.setDeleteBackgroundColor(R.color.delete_bg_color)
MotionToast.setWarningBackgroundColor(R.color.warning_bg_color)
MotionToast.setInfoBackgroundColor(R.color.info_bg_color)
} else {
MotionToast.resetToastColors()
}
}
override fun onClick(v: View?) {
when (v!!.id) {
R.id.successBtn -> {
MotionToast.createToast(
this,
"Profile saved",
"Lorem Ipsum is simply dummy this is very simple text Lorem Ipsum is simply dummy this is very simple text Lorem Ipsum is simply dummy this is very simple text",
MotionToastStyle.SUCCESS,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.montserrat_regular)
)
}
R.id.errorBtn -> {
MotionToast.createToast(
this,
"Profile failed",
"Profile Update Failed due to this reason",
MotionToastStyle.ERROR,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
R.id.warningBtn -> {
MotionToast.createToast(
this,
"",
"Please Fill All The Details!",
MotionToastStyle.WARNING,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
R.id.infoBtn -> {
MotionToast.createColorToast(
this,
"",
"Color Toast testing here!",
MotionToastStyle.INFO,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
R.id.deleteBtn -> {
MotionToast.createToast(
this, "Profile Deleted!",
"",
MotionToastStyle.DELETE,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
R.id.noInternetBtn -> {
MotionToast.createToast(
this, "Please turn on internet connection!",
"",
MotionToastStyle.NO_INTERNET,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
}
}
override fun onLongClick(v: View?): Boolean {
when (v!!.id) {
R.id.successBtn -> {
MotionToast.createColorToast(
this,
"Post create đ",
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
MotionToastStyle.SUCCESS,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.montserrat_regular)
)
}
R.id.errorBtn -> {
MotionToast.darkToast(
this,
"",
"Profile Update Failed!",
MotionToastStyle.ERROR,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
R.id.warningBtn -> {
MotionToast.darkColorToast(
this,
"",
"Please Fill All The Details!",
MotionToastStyle.WARNING,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
R.id.infoBtn -> {
MotionToast.darkToast(
this,
"",
"Dark ui testing here!",
MotionToastStyle.INFO,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
R.id.deleteBtn -> {
MotionToast.darkToast(
this,
"",
"Profile Deleted!",
MotionToastStyle.DELETE,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
R.id.noInternetBtn -> {
MotionToast.darkToast(
this,
"",
"Please turn on internet connection!",
MotionToastStyle.NO_INTERNET,
MotionToast.GRAVITY_BOTTOM,
MotionToast.LONG_DURATION,
ResourcesCompat.getFont(this, R.font.helvetica_regular)
)
}
}
return true
}
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
setToastColors(isChecked)
}
}
Reference
Download the code below:
No. | Link |
---|---|
1. | Download Full Code |
2. | Read more here. |
3. | Follow code author here. |
TastyToasty
An easy-to-use library to create tasty đ Toasts with a bunch of flavours.
It also provides effortless methods to create Instagram like Toasts.
Step 1: Installation
Add this in your root build.gradle
file (not your module build.gradle
file):
Add this to your module's build.gradle
file :
Step 2: Usage
Instagram Toasts
- instaFollower:
- instaComment:
VIBGYOR Toasts đ
Standard Toasts
Custom Toasts
// Pass the last attribute as false or null if your do not want the tail in Toast
TastyToasty.makeText(MainActivity.this, "This is a custom toast",TastyToasty.LONG, R.drawable.ic_action_favourite, R.color.violet, R.color.white, true).show();
new TastyToasty.Builder(MainActivity.this)
.setText("This one is using builder method")
.setBackgroundColor(R.color.green)
.setIconId(R.drawable.ic_verified_user)
.showTail(true) // Pass false or null or don't call at all if you don't want the "tail" in your toast
.show();
Note:
//Default text color is white and default background color is pinkinsh red
new TastyToasty.Builder(MainActivity.this)
.setIconId(R.drawable.ic_whatshot)
.show();
Full Example
Let us look at a full android Toast Library sample project.
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:
androidx.constraintlayout.widget.ConstraintLayout
TextView
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 2. Write Code
Finally we need to write our code as follows:
(a). MainActivity.java
Our
MainActivity
class.
Here is the full code:
package replace_with_your_package_name;
import android.os.Bundle;
import com.uk.tastytoasty.TastyToasty;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
TastyToasty.makeText(MainActivity.this, "This is a custom toast",TastyToasty.LONG, R.drawable.ic_action_favourite, R.color.violet, R.color.white, true).show();
*/
/*
new TastyToasty.Builder(MainActivity.this)
.setText("This one is using builder method")
.setBackgroundColor(R.color.green)
.setIconId(R.drawable.ic_verified_user)
.showTail(true) // Pass false or null or don't call at all if you don't want the "tail" in your toast
.show();
*/
/*
//Default text color is white and default background color is pinkinsh red
new TastyToasty.Builder(MainActivity.this)
.setIconId(R.drawable.ic_whatshot)
.show();
*/
}
}
Reference
Download the code below:
No. | Link |
---|---|
1. | Download Full Code |
2. | Read more here. |
3. | Follow code author here. |
FancyToast-Android
Make your native android
Toasts
Fancy.
A library that takes the standard Android toast to the next level with a variety of styling options. Style your toast from code..
Step 1: Declare Repository
Add this in your root build.gradle
file (not your module build.gradle
file):
Step 2: Add Dependency
This library is available on MavenCentreal
Add this to your module's build.gradle
file (make sure the version matches the Maven-Central
badge above):
Step 3: Usage
Each method always returns a Toast
object, so you can customize the Toast
much more. DON'T FORGET THE show() METHOD!
show()
To display an default Toast
:
To display a success Toast
:
To display an info Toast
:
To display a warning Toast
:
To display the error Toast
:
To display the confusing Toast
:
You can also remove the android icon on top-right corner by passing last parameter false.
You can also create your custom Toasts
with passing your image with or without android icon(top-right corner):
FancyToast.makeText(yourContext, "I'm a custom Toast", duration, type, yourimage, boolean value).show();
To display the custom Toast
with no android icon:
FancyToast.makeText(this, "This is Custom Toast with no android icon", FancyToast.LENGTH_LONG, FancyToast.CONFUSING, R.drawable.ic_android_black_24dp, false);
Screenshots
Please click the image below to enlarge.
Full Example
Let us look at a full Fancy Toast
Library Example below.
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.
To start inside your /res/layout/
directory create an xml layout file named activity_main.xml
.
On top of that design your XML layout using the following 2 UI widgets and ViewGroups:
RelativeLayout
Button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.shashank.sony.fancytoastlibrary.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Default" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button"
android:layout_centerHorizontal="true"
android:text="Success" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:text="Error" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_marginTop="20dp"
android:text="Warning" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button4"
android:layout_centerHorizontal="true"
android:text="Info" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button5"
android:layout_alignParentEnd="true"
android:text="Confusing" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_alignEnd="@+id/button"
android:layout_marginTop="23dp"
android:text="Custom icon" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button5"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="Custom \n(No android icon)" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/button6"
android:layout_alignTop="@+id/button8"
android:text="Android" />
</RelativeLayout>
Step 2. Write Code
Finally we need to write our code as follows:
(a). MainActivity.java
Our
MainActivity
class.
Just copy the code below and replace the package with your app's package name.
package replace_with_your_package_name;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.shashank.sony.fancytoastlib.FancyToast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button b1, b2, b3, b4, b5, b6, b7, b8, b9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.button);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
b5 = findViewById(R.id.button5);
b6 = findViewById(R.id.button6);
b7 = findViewById(R.id.button7);
b8 = findViewById(R.id.button8);
b9 = findViewById(R.id.button9);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button)
FancyToast.makeText(this, "This is Default Toast", FancyToast.LENGTH_LONG, FancyToast.DEFAULT, true).show();
else if (v.getId() == R.id.button2)
FancyToast.makeText(this, "Success Toast !", FancyToast.LENGTH_LONG, FancyToast.SUCCESS, true).show();
else if (v.getId() == R.id.button3)
FancyToast.makeText(this, "This is an Error Toast", FancyToast.LENGTH_LONG, FancyToast.ERROR, true).show();
else if (v.getId() == R.id.button4)
FancyToast.makeText(this, "Beware of dog", FancyToast.LENGTH_LONG, FancyToast.WARNING, true).show();
else if (v.getId() == R.id.button5)
FancyToast.makeText(this, "Here is some Info for you", FancyToast.LENGTH_LONG, FancyToast.INFO, true).show();
else if (v.getId() == R.id.button6)
FancyToast.makeText(this, "This is Confusing Toast", FancyToast.LENGTH_LONG, FancyToast.CONFUSING, false).show();
else if (v.getId() == R.id.button7)
FancyToast.makeText(this, "This is Custom Toast", FancyToast.LENGTH_LONG, FancyToast.CONFUSING, R.drawable.ic_android_black_24dp, true).show();
else if (v.getId() == R.id.button8)
FancyToast.makeText(this, "This is Custom Toast with no android icon", FancyToast.LENGTH_LONG, FancyToast.CONFUSING, R.drawable.ic_android_black_24dp, false).show();
else if (v.getId() == R.id.button9)
FancyToast.makeText(this, "This is a Success Toast", FancyToast.LENGTH_LONG, FancyToast.SUCCESS, false).show();
}
}
Toaster-android
A simple library to add custom toast to android applications..
Step 1: Declare Jitpack
Add the JitPack repository to your build.gradle(project)
.
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:
A simple use case will look like this
With a custom drawable
Toaster.pop(
this,
"A simple toast message with image",
R.drawable.ic_baseline_cloud_done_24 /* image */
).show()
Code Snippets
Using templates
- Success
- Warning
- Error
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)
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
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()
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:
androidx.constraintlayout.widget.ConstraintLayout
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. |
Reference
Download the code below:
No. | Link |
---|---|
1. | Download Full Code |
2. | Read more here. |
3. | Follow code author here. |
Toaster-android
A simple library to add custom toast to android applications..
Step 1: Declare Jitpack
Add the JitPack repository to your build.gradle(project)
.
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:
A simple use case will look like this
With a custom drawable
Toaster.pop(
this,
"A simple toast message with image",
R.drawable.ic_baseline_cloud_done_24 /* image */
).show()
Code Snippets
Using templates
- Success
- Warning
- Error
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)
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
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()
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:
androidx.constraintlayout.widget.ConstraintLayout
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. |
pranavpandey/dynamic-toasts
Custom toasts with color and icon on Android..
Dynamic Toasts
A simple library to display themed toasts with icon and text on Android 2.3 (API 9) and above.
Installation
It can be installed by adding the following dependency to your build.gradle
file:
dependencies {
// For AndroidX enabled projects.
implementation 'com.pranavpandey.android:dynamic-toasts:4.1.2'
// For legacy projects.
implementation 'com.pranavpandey.android:dynamic-toasts:1.3.0'
}
Usage
Just call show()
method to display the toast.
Configuration
Various methods can be called anywhere in the app to do customisations.
DynamicToast.Config.getInstance()
// Background color for default toast.
.setDefaultBackgroundColor(@ColorInt int defaultBackgroundColor)
// Tint color for default toast.
.setDefaultTintColor(@ColorInt int defaultTintColor)
// Background color for error toast.
.setErrorBackgroundColor(@ColorInt int errorBackgroundColor)
// Background color for success toast.
.setSuccessBackgroundColor(@ColorInt int successBackgroundColor)
// Background color for warning toast.
.setWarningBackgroundColor(@ColorInt int warningBackgroundColor)
// Custom icon for error toast. Pass `null` to use default icon.
.setErrorIcon(@Nullable Drawable errorIcon)
// Custom icon for success toast. Pass `null` to use default icon.
.setSuccessIcon(@Nullable Drawable successIcon)
// Custom icon for warning toast. Pass `null` to use default icon.
.setWarningIcon(@Nullable Drawable warningIcon)
// Disable icon for all the toasts.
.setDisableIcon(boolean disableIcon)
// Custom icon size in `pixels` for all the toasts.
.setIconSize(int iconSize)
// Custom text size in `SP` for all the toasts.
.setTextSize(int textSize)
// Custom text typeface for all the toasts. Pass `null` to use system typeface.
.setTextTypeface(@Nullable Typeface textTypeface)
// Custom background drawable for all the toasts. Pass `null` to use default background.
.setToastBackground(@Nullable Drawable toastBackground)
// Apply customisations.
.apply();
Call reset()
method to reset all the customisations.
Default toast
Simple toast based on the vanilla Android theme for Toast.LENGTH_SHORT
duration.
Default toast with duration
Simple toast based on the vanilla Android theme for supplied duration.
Default toast with icon
Simple toast based on the vanilla Android theme with a icon for Toast.LENGTH_SHORT
duration.
Default toast with icon and duration
Simple toast based on the vanilla Android theme with a icon for supplied duration.
Error toast
Error toast with #F44336
background for Toast.LENGTH_SHORT
duration.
Error toast with duration
Error toast with #F44336
background for supplied duration.
Success toast
Success toast with #4CAF50
background for Toast.LENGTH_SHORT
duration.
Success toast with duration
Success toast with #4CAF50
background for supplied duration.
Warning toast
Warning toast with #FFEB3B
background for Toast.LENGTH_SHORT
duration.
Warning toast with duration
Warning toast with #FFEB3B
background for supplied duration.
Custom toast
Custom toast based on the supplied background and tint color for Toast.LENGTH_SHORT
duration.
Custom toast with duration
Custom toast based on the supplied background and tint color for supplied duration.
DynamicToast.make(context, "Custom toast with duration", tintColor, backgroundColor, duration).show();
Custom toast with icon
Custom toast with icon and duration
Custom toast based on the supplied icon, background and tint color theme for supplied duration.
DynamicToast.make(context, "Custom toast with icon and duration", drawable,
tintColor, backgroundColor, duration).show();
Cheat sheets
Use DynamicHint.show(view, toast)
method to display it according to the anchor view position.
Full Example
Click the link below to see a full example of how to use this library.
Step 1. Design Layouts
In Android we design our UI interfaces using XML. So let's create the following layouts:
(a). content_dynamic_toasts.xml
Our
content_dynamic_toasts
layout.
First inside your /res/layout/
directory create an xml layout file named content_dynamic_toasts.xml
.
Next design your XML layout using the following 9 UI widgets and ViewGroups:
!--
FrameLayout
androidx.core.widget.NestedScrollView
LinearLayout
androidx.constraintlayout.widget.ConstraintLayout
androidx.appcompat.widget.AppCompatTextView
com.google.android.flexbox.FlexboxLayout
androidx.appcompat.widget.AppCompatButton
View
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.core.widget.NestedScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:scrollbarStyle="outsideOverlay"
android:clipToPadding="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_margin"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingBottom="@dimen/activity_margin_fab"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/gradle_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gradle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/gradle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@id/gradle_title"
app:layout_constraintStart_toStartOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/toasts_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:text="@string/default_toasts"
app:layout_constraintTop_toBottomOf="@id/gradle"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/toasts_default_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toasts_default"
app:layout_constraintStart_toStartOf="parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/without_icon" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_default_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/with_icon" />
</com.google.android.flexbox.FlexboxLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/toasts_inbuilt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:text="@string/inbuilt_toasts"
app:layout_constraintTop_toBottomOf="@id/toasts_default_content"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/toasts_inbuilt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toasts_inbuilt"
app:layout_constraintStart_toStartOf="parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_success"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/success" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/warning" />
</com.google.android.flexbox.FlexboxLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/toasts_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:text="@string/custom_toasts"
app:layout_constraintTop_toBottomOf="@id/toasts_inbuilt_content"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/toasts_custom_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toasts_custom"
app:layout_constraintStart_toStartOf="parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/without_icon" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_custom_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/with_icon" />
</com.google.android.flexbox.FlexboxLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/toasts_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:text="@string/custom_colors"
app:layout_constraintTop_toBottomOf="@id/toasts_custom_content"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/toasts_colors_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toasts_colors"
app:layout_constraintStart_toStartOf="parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_error_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_success_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/success" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_warning_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/warning" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_default_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_custom" />
</com.google.android.flexbox.FlexboxLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/toasts_icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:text="@string/custom_icons"
app:layout_constraintTop_toBottomOf="@id/toasts_colors_content"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/toasts_icons_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toasts_icons"
app:layout_constraintStart_toStartOf="parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_error_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_success_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/success" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_warning_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/warning" />
</com.google.android.flexbox.FlexboxLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/toasts_icon_disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:text="@string/disable_icon"
app:layout_constraintTop_toBottomOf="@id/toasts_icons_content"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/toasts_icon_disable_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toasts_icon_disable"
app:layout_constraintStart_toStartOf="parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_error_icon_disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_success_icon_disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/success" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_warning_icon_disable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/warning" />
</com.google.android.flexbox.FlexboxLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/toasts_icon_disable_tint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:text="@string/disable_icon_tint"
app:layout_constraintTop_toBottomOf="@id/toasts_icon_disable_content"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/toasts_icon_disable_tint_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toasts_icon_disable_tint"
app:layout_constraintStart_toStartOf="parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_error_icon_disable_tint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_success_icon_disable_tint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/success" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_warning_icon_disable_tint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/warning" />
</com.google.android.flexbox.FlexboxLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/toasts_config"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:text="@string/config"
app:layout_constraintTop_toBottomOf="@id/toasts_icon_disable_tint_content"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/toasts_config_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toasts_config"
app:layout_constraintStart_toStartOf="parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_config_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_config_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/background" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/toast_config_icon_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/icon_size" />
</com.google.android.flexbox.FlexboxLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/toasts_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin"
android:text="@string/cheat_sheets"
app:layout_constraintTop_toBottomOf="@id/toasts_config_content"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/toasts_hint_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toasts_hint"
app:layout_constraintStart_toStartOf="parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/hint_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_custom" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/hint_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/custom" />
</com.google.android.flexbox.FlexboxLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<View
android:id="@+id/ads_app_bar_shadow"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@drawable/app_bar_shadow" />
</FrameLayout>
Step 2. Write Code
Finally we need to write our code as follows:
(a). DynamicToastsActivity.kt
Our
DynamicToastsActivity
class.
To begin prepare a Kotlin file named DynamicToastsActivity.kt
.
Moreover we will add imports from android SDK and other packages. Here are some of the imports we will use in this class:
Configuration
from theandroid.content.res
package.Color
from theandroid.graphics
package.Typeface
from theandroid.graphics
package.Bundle
from theandroid.os
package.Menu
from theandroid.view
package.MenuItem
from theandroid.view
package.View
from theandroid.view
package.TextView
from theandroid.widget
package.Toast
from theandroid.widget
package.AppCompatActivity
from theandroidx.appcompat.app
package.AppCompatResources
from theandroidx.appcompat.content.res
package.Toolbar
from theandroidx.appcompat.widget
package.ContextCompat
from theandroidx.core.content
package.
On top of that create a class that derives from AppCompatActivity
and add its contents as follows:
We will be overriding the following functions:
onCreate(savedInstanceState: Bundle?)
.applyOverrideConfiguration(overrideConfiguration: Configuration?)
.onCreateOptionsMenu(menu: Menu): Boolean
.onOptionsItemSelected(item: MenuItem): Boolean
.onClick(v: View)
.
Just copy the code below and replace the package with your app's package name.
package replace_with_your_package_name
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.pranavpandey.android.dynamic.toasts.DynamicHint
import com.pranavpandey.android.dynamic.toasts.DynamicToast
import com.pranavpandey.android.dynamic.toasts.sample.dialog.AboutDialogFragment
import com.pranavpandey.android.dynamic.util.DynamicColorUtils
import com.pranavpandey.android.dynamic.util.DynamicLinkUtils
import com.pranavpandey.android.dynamic.util.DynamicPackageUtils
import com.pranavpandey.android.dynamic.util.DynamicUnitUtils
/**
* Main activity to show the implementation of [DynamicToast].
*/
class DynamicToastsActivity : AppCompatActivity(), View.OnClickListener {
companion object {
/**
* Open source repository url.
*/
const val URL_GITHUB = "https://github.com/pranavpandey/dynamic-toasts"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dynamic_toasts)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
toolbar.setSubtitle(R.string.app_name_sample)
setSupportActionBar(toolbar)
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setColorFilter(DynamicColorUtils.getTintColor(
ContextCompat.getColor(this, R.color.color_accent)))
(findViewById<View>(R.id.gradle) as TextView).text = String.format(
getString(R.string.format_version),
DynamicPackageUtils.getVersionName(this))
fab.setOnClickListener(this)
findViewById<View>(R.id.toast_default).setOnClickListener(this)
findViewById<View>(R.id.toast_default_icon).setOnClickListener(this)
findViewById<View>(R.id.toast_success).setOnClickListener(this)
findViewById<View>(R.id.toast_error).setOnClickListener(this)
findViewById<View>(R.id.toast_success).setOnClickListener(this)
findViewById<View>(R.id.toast_warning).setOnClickListener(this)
findViewById<View>(R.id.toast_custom_icon).setOnClickListener(this)
findViewById<View>(R.id.toast_custom).setOnClickListener(this)
findViewById<View>(R.id.toast_error_color).setOnClickListener(this)
findViewById<View>(R.id.toast_success_color).setOnClickListener(this)
findViewById<View>(R.id.toast_warning_color).setOnClickListener(this)
findViewById<View>(R.id.toast_default_color).setOnClickListener(this)
findViewById<View>(R.id.toast_error_icon).setOnClickListener(this)
findViewById<View>(R.id.toast_success_icon).setOnClickListener(this)
findViewById<View>(R.id.toast_warning_icon).setOnClickListener(this)
findViewById<View>(R.id.toast_error_icon_disable).setOnClickListener(this)
findViewById<View>(R.id.toast_success_icon_disable).setOnClickListener(this)
findViewById<View>(R.id.toast_warning_icon_disable).setOnClickListener(this)
findViewById<View>(R.id.toast_error_icon_disable_tint).setOnClickListener(this)
findViewById<View>(R.id.toast_success_icon_disable_tint).setOnClickListener(this)
findViewById<View>(R.id.toast_warning_icon_disable_tint).setOnClickListener(this)
findViewById<View>(R.id.toast_config_text).setOnClickListener(this)
findViewById<View>(R.id.toast_config_background).setOnClickListener(this)
findViewById<View>(R.id.toast_config_icon_size).setOnClickListener(this)
findViewById<View>(R.id.hint_default).setOnClickListener(this)
findViewById<View>(R.id.hint_custom).setOnClickListener(this)
}
override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
if (overrideConfiguration != null) {
val uiMode = overrideConfiguration.uiMode
overrideConfiguration.setTo(baseContext.resources.configuration)
overrideConfiguration.uiMode = uiMode
}
super.applyOverrideConfiguration(resources.configuration)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.menu_about) {
AboutDialogFragment.newInstance().showDialog(this)
return true
}
return super.onOptionsItemSelected(item)
}
override fun onClick(v: View) {
when (v.id) {
R.id.fab -> DynamicLinkUtils.viewUrl(this@DynamicToastsActivity, URL_GITHUB)
// Default toast without icon.
R.id.toast_default -> DynamicToast.make(
this, getString(R.string.without_icon_desc)).show()
// Default toast with icon.
R.id.toast_default_icon -> DynamicToast.make(
this, getString(R.string.with_icon_desc),
AppCompatResources.getDrawable(
this, R.drawable.ic_toast_icon)).show()
// Error toast.
R.id.toast_error -> DynamicToast.makeError(
this, getString(R.string.error_desc)).show()
// Success toast.
R.id.toast_success -> DynamicToast.makeSuccess(
this, getString(R.string.success_desc)).show()
// Warning toast.
R.id.toast_warning -> DynamicToast.makeWarning(
this, getString(R.string.warning_desc)).show()
// Custom toast without icon.
R.id.toast_custom -> DynamicToast.make(
this, getString(R.string.custom_desc),
Color.parseColor("#FFFFFF"), Color.parseColor("#000000"),
Toast.LENGTH_LONG).show()
// Custom toast with icon.
R.id.toast_custom_icon -> DynamicToast.make(
this, getString(R.string.custom_desc),
AppCompatResources.getDrawable(this, R.drawable.ic_social_github),
Color.parseColor("#FFFFFF"), Color.parseColor("#000000"),
Toast.LENGTH_LONG).show()
// Error toast with custom color.
R.id.toast_error_color -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setErrorBackgroundColor(Color.parseColor("#673AB7"))
.apply()
DynamicToast.makeError(this, getString(R.string.error_color_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Success toast with custom color.
R.id.toast_success_color -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setSuccessBackgroundColor(Color.parseColor("#2196F3"))
.apply()
DynamicToast.makeSuccess(
this, getString(R.string.success_color_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Warning toast with custom color.
R.id.toast_warning_color -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setWarningBackgroundColor(Color.parseColor("#8BC34A"))
.apply()
DynamicToast.makeWarning(
this, getString(R.string.warning_color_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Default toast with custom color.
R.id.toast_default_color -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setDefaultBackgroundColor(Color.parseColor("#607d8b"))
.setDefaultTintColor(DynamicColorUtils.getTintColor(
Color.parseColor("#607d8b")))
.apply()
DynamicToast.make(
this, getString(R.string.default_color_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Error toast with custom icon.
R.id.toast_error_icon -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setErrorIcon(AppCompatResources.getDrawable(
this, R.drawable.ic_toast_icon))
.apply()
DynamicToast.makeError(this,
getString(R.string.error_icon_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Success toast with custom icon.
R.id.toast_success_icon -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setSuccessIcon(AppCompatResources.getDrawable(
this, R.drawable.ic_toast_icon))
.apply()
DynamicToast.makeSuccess(this,
getString(R.string.success_icon_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Warning toast with custom icon.
R.id.toast_warning_icon -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setWarningIcon(AppCompatResources.getDrawable(
this, R.drawable.ic_toast_icon))
.apply()
DynamicToast.makeWarning(this,
getString(R.string.warning_icon_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Error toast without icon.
R.id.toast_error_icon_disable -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setDisableIcon(true)
.apply()
DynamicToast.makeError(this,
getString(R.string.error_icon_disable_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Success toast without icon.
R.id.toast_success_icon_disable -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setDisableIcon(true)
.apply()
DynamicToast.makeSuccess(this,
getString(R.string.success_icon_disable_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Warning toast without icon.
R.id.toast_warning_icon_disable -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setDisableIcon(true)
.apply()
DynamicToast.makeWarning(this, getString(
R.string.warning_icon_disable_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Error toast without icon tint.
R.id.toast_error_icon_disable_tint -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setErrorIcon(AppCompatResources.getDrawable(
this, R.mipmap.ic_launcher))
.setTintIcon(false)
.apply()
DynamicToast.makeError(this,
getString(R.string.error_icon_disable_tint_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Success toast without icon tint.
R.id.toast_success_icon_disable_tint -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setSuccessIcon(AppCompatResources.getDrawable(
this, R.mipmap.ic_launcher))
.setTintIcon(false)
.apply()
DynamicToast.makeSuccess(this,
getString(R.string.success_icon_disable_tint_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Warning toast without icon tint.
R.id.toast_warning_icon_disable_tint -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setWarningIcon(AppCompatResources.getDrawable(
this, R.mipmap.ic_launcher))
.setTintIcon(false)
.apply()
DynamicToast.makeWarning(this, getString(
R.string.warning_icon_disable_tint_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Toast with custom text size and typeface.
R.id.toast_config_text -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setTextSize(18)
.setErrorIcon(AppCompatResources.getDrawable(
this, R.drawable.ic_toast_icon))
.setTextTypeface(Typeface.create(
Typeface.SERIF, Typeface.BOLD_ITALIC))
.setErrorBackgroundColor(Color.parseColor("#2196F3"))
.apply()
DynamicToast.makeError(this, getString(R.string.text_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Toast with custom background.
R.id.toast_config_background -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setToastBackground(AppCompatResources.getDrawable(
this, R.drawable.bg_custom_toast))
.apply()
DynamicToast.makeSuccess(this, getString(R.string.background_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Toast with custom icon size.
R.id.toast_config_icon_size -> {
// Customise toast.
DynamicToast.Config.getInstance()
.setIconSize(DynamicUnitUtils.convertDpToPixels(48f))
.apply()
DynamicToast.makeWarning(this, getString(R.string.icon_size_desc)).show()
// Reset customisations.
DynamicToast.Config.getInstance().reset()
}
// Default hint without icon.
R.id.hint_default -> DynamicHint.show(v,
DynamicHint.make(this, getString(R.string.default_hint)))
// Custom hint with icon.
R.id.hint_custom -> {
// Customise hint.
DynamicHint.Config.getInstance()
.setDefaultBackgroundColor(Color.parseColor("#607d8b"))
.setDefaultTintColor(DynamicColorUtils.getTintColor(
Color.parseColor("#607d8b")))
.apply()
DynamicHint.show(v, DynamicHint.make(this, getString(R.string.custom_hint),
AppCompatResources.getDrawable(this, R.drawable.adt_ic_warning)))
// Reset customisations.
DynamicHint.Config.getInstance().reset()
}
}
}
}
Reference
Download the code below:
No. | Link |
---|---|
1. | Download Full Code |
2. | Read more here. |
3. | Follow code author here. |
FloatingToast
Android library to create customizable floating animated toasts like in Clash Royale app.
An android library to make customisable floating animated toasts.
To use it follow these steps:
Step 1: Add as a dependency
In your build.gradle
Maven Central
jcenter
Step 2: Usage
Create Floating Toasts
Button button = findViewById(R.id.button);
String text = "Hello toast!";
int duration = FloatingToast.LENGTH_MEDIUM;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FloatingToast toast = FloatingToast.makeToast(button, text, FloatingToast.LENGTH_MEDIUM);
toast.show();
}
});
You can also chain your methods and avoid holding on to the Toast object, like this:
You can use the activity context to instantiate the FloatingToast object, like this:
NOTE: Always use the view (which was used to call the toast) to create the FloatingToast object wherever possible rather than using the activity context.
An example with all the customisations:
Typeface customFont = Typeface.createFromAsset(getContext().getAssets(), "fonts/custom_font.ttf");
//Duration of 1250 millis
//Gravity - Mid Top (Default is Center)
//Fade out duration of 1000 millis (Default is 750 millis)
//Text size - 12dp (Default is 16dp)
//Background blur - On (Default is On)
//Float distance - 30px (Default is 40px)
//Text color - White
//Text shadow - On (Default is Off)
//Custom font (No custom font is provided by default)
FloatingToast.makeToast(button, text, FloatingToast.LENGTH_LONG)
.setGravity(FloatingToast.GRAVITY_MID_TOP)
.setFadeOutDuration(FloatingToast.FADE_DURATION_LONG)
.setTextSizeInDp(12)
.setBackgroundBlur(true)
.setFloatDistance(FloatingToast.DISTANCE_SHORT)
.setTextColor(Color.parseColor("#ffffff"))
.setShadowLayer(5, 1, 1, Color.parseColor("#000000"))
.setTextTypeface(customFont)
.show(); //Show toast at the specified fixed position
Public methods
makeToast
View:
String:
int:
makeToast
View:
int:
int:
makeToast
Activity:
String:
int:
makeToast
Activity:
int:
int:
setGravity
Set the location at which the notification should appear on the screen.
int:
setGravity
Set the location at which the notification should appear on the screen.
int:
int:
int:
setFadeOutDuration
Set the fade out duration of the toast. Total animation time of the toast - duration + fadeOutDuration
int:
setTextColor
Sets the text color for all the states (normal, selected, focused) to be this color.
int:
setTextTypeface
Typeface
setTextStyle
int
setTextSizeInSp
float:
setTextSizeInDp
int:
setTextSizeCustomUnit
int:
float:
setFloatDistance
Sets the distance of the toast till which it floats.
float:
setShadowLayer
float
float
float
int
setBackgroundBlur
Sets the blur background for the text in the toast.
boolean:
showAtTouchPosition
Show the view for the specified duration at the touch position.
View:
show
Show the view for the specified duration.
Reference
|2.|Read more here.| |3.|Follow code author here.|
CookieBar
CookieBar is a lightweight library for showing a brief message at the top or bottom of the screen..
CookieBar is a lightweight library for showing a brief message at the top or bottom of the screen.
Screenshot
Step 1: Install
Install the library using the following implementation statement:
via Maven:
<dependency>
<groupId>com.liuguangqiang.cookie</groupId>
<artifactId>CookieBar</artifactId>
<version>1.0.1</version>
<type>pom</type>
</dependency>
Step 2: Use
Use it as shown in the below examples:
A simple CookieBar.
A CookieBar with a icon and a action button.
new CookieBar.Builder(MainActivity.this)
.setTitle("TITLE")
.setIcon(R.mipmap.ic_launcher)
.setMessage("MESSAGE")
.setAction("ACTION", new OnActionClickListener() {
@Override
public void onClick() {
}
})
.show();
Styling
You can change the default style by set the Theme's attributes.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="cookieTitleColor">@color/default_title_color</item>
<item name="cookieMessageColor">@color/default_message_color</item>
<item name="cookieActionColor">@color/default_action_color</item>
<item name="cookieBackgroundColor">@color/default_bg_color</item>
</style>
Or dynamically change the style with a cookie builder. Use the following properties:
- layoutGravity
- backgroundColor
- titleColor
- messageColor
- actionColor
- duration
Full Example
Let us look at a full CookieBar
Toast Library Example below.
Step 1. Design Layouts
(a). activity_main.xml
Our
activity_main
layout.
First you inside your /res/layout/
directory create an xml layout file named activity_main.xml
.
Furthermore design your XML layout using the following 2 UI widgets and ViewGroups:
LinearLayout
Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="64dp">
<Button
android:id="@+id/btn_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top cookie" />
<Button
android:id="@+id/btn_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom cookie" />
<Button
android:id="@+id/btn_top_with_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top cookie with a icon" />
<Button
android:id="@+id/btn_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A custom cookie" />
</LinearLayout>
Step 2. Write Code
Finally we need to write our code as follows:
(a). MainActivity.java
Our
MainActivity
class.
Here is the full code for MainActivity
:
package replace_with_your_package_name;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.liuguangqiang.cookie.CookieBar;
import com.liuguangqiang.cookie.OnActionClickListener;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnTop = (Button) findViewById(R.id.btn_top);
btnTop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new CookieBar.Builder(MainActivity.this)
.setTitle(R.string.cookie_title)
.setMessage(R.string.cookie_message)
.show();
}
});
Button btnBottom = (Button) findViewById(R.id.btn_bottom);
btnBottom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new CookieBar
.Builder(MainActivity.this)
.setTitle(R.string.cookie_title)
.setIcon(R.mipmap.ic_launcher)
.setMessage(R.string.cookie_message)
.setLayoutGravity(Gravity.BOTTOM)
.setAction(R.string.cookie_action, new OnActionClickListener() {
@Override
public void onClick() {
Toast.makeText(getApplicationContext(), "įšåģåīŧææ´å¸
äē!", Toast.LENGTH_LONG).show();
}
})
.show();
}
});
Button btnTopWithIcon = (Button) findViewById(R.id.btn_top_with_icon);
btnTopWithIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new CookieBar.Builder(MainActivity.this)
.setMessage(R.string.cookie_message)
.setDuration(10000)
.setActionWithIcon(R.mipmap.ic_action_close, new OnActionClickListener() {
@Override
public void onClick() {
Toast.makeText(getApplicationContext(), "įšåģåīŧææ´å¸
äē!", Toast.LENGTH_LONG).show();
}
})
.show();
}
});
Button btnCustom = (Button) findViewById(R.id.btn_custom);
btnCustom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new CookieBar.Builder(MainActivity.this)
.setTitle(R.string.cookie_title)
.setMessage(R.string.cookie_message)
.setDuration(3000)
.setBackgroundColor(R.color.colorPrimary)
.setActionColor(android.R.color.white)
.setTitleColor(R.color.colorAccent)
.setAction(R.string.cookie_action, new OnActionClickListener() {
@Override
public void onClick() {
Toast.makeText(getApplicationContext(), "įšåģåīŧææ´å¸
äē!", Toast.LENGTH_LONG).show();
}
})
.show();
}
});
}
}
Reference
Download the code below:
No. | Link |
---|---|
1. | Download Full Code |
2. | Read more here. |
3. | Follow code author here. |
Hamadakram/Sneaker
A lightweight Android library for customizable alerts.
Here are demos:
Step 1: Install it
Grab via Gradle:
Step 2: Usage
In Sneaker 2.0.0 it's possilbe to show sneaker on Activity, Fragment or any ViewGroup
Sneaker.with(activity) // To show Sneaker on Activity
Sneaker.with(fragment) // To show Sneaker on Fragment
Sneaker.with(viewGroup) // To show Sneaker on ViewGroup
Custom:
Sneaker.with(actvitiy) // Activity, Fragment or ViewGroup
.setTitle("Title", R.color.white) // Title and title color
.setMessage("This is the message.", R.color.white) // Message and message color
.setDuration(4000) // Time duration to show
.autoHide(true) // Auto hide Sneaker view
.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT) // Height of the Sneaker layout
.setIcon(R.drawable.ic_no_connection, R.color.white, false) // Icon, icon tint color and circular icon view
.setTypeface(Typeface.createFromAsset(this.getAssets(), "font/" + fontName)); // Custom font for title and message
.setOnSneakerClickListener(this) // Click listener for Sneaker
.setOnSneakerDismissListener(this) // Dismiss listener for Sneaker. - Version 1.0.2
.setCornerRadius(radius, margin) // Radius and margin for round corner Sneaker. - Version 1.0.2
.sneak(R.color.colorAccent) // Sneak with background color
Error:
Sneaker.with(actvitiy) // Activity, Fragment or ViewGroup
.setTitle("Error!!")
.setMessage("This is the error message")
.sneakError()
Success:
Sneaker.with(actvitiy) // Activity, Fragment or ViewGroup
.setTitle("Success!!")
.setMessage("This is the success message")
.sneakSuccess()
Warning:
Sneaker.with(actvitiy) // Activity, Fragment or ViewGroup
.setTitle("Warning!!")
.setMessage("This is the warning message")
.sneakWarning()
Custom View:
val sneaker = Sneaker.with(actvitiy) // Activity, Fragment or ViewGroup
val view = LayoutInflater.from(this).inflate(R.layout.custom_view, sneaker.getView(), false)
// Your custom view code
view.findViewById<TextView>(R.id.tvInstall).setOnClickListener{
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show()
}
sneaker.sneakCustom(view)
Full Example
Follow these steps to create a full Sneaker
Toast Library example.
Step 1. Design Layouts
(a). custom_view.xml
Our
custom_view
layout.
Then design your XML layout using the following 2 UI widgets and ViewGroups:
LinearLayout
TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="@drawable/round_background"
android:backgroundTint="@android:color/holo_blue_dark"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:alpha="0.8"
android:text="@string/best_of_2018"
android:textAllCaps="true"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:text="@string/game_of_the_year"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/tvInstall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="8dp"
android:background="@drawable/round_background"
android:backgroundTint="@android:color/darker_gray"
android:paddingStart="10dp"
android:paddingTop="3dp"
android:paddingEnd="10dp"
android:paddingBottom="3dp"
android:text="@string/textview"
android:textColor="@android:color/white"
tools:text="Install" />
</LinearLayout>
(b). activity_main.xml
Our
activity_main
layout.
LinearLayout
RelativeLayout
TextView
Button
View
FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:weightSum="3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:padding="5dp"
android:text="@string/activity"
android:textColor="@android:color/black"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@+id/btShowError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="@android:drawable/editbox_background_normal"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/show_error" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha="0.5"
android:background="@android:color/black" />
<RelativeLayout
android:id="@+id/viewGroup"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:animateLayoutChanges="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:padding="5dp"
android:text="@string/viewgroup"
android:textColor="@android:color/black"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@+id/btShowSuccess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/editbox_background_normal"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/show_success" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha="0.5"
android:background="@android:color/black" />
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Step 2. Write Code
Finally we need to write our code as follows:
(a). MainFragment.kt
Our
MainFragment
class.
To begin create a Kotlin file named MainFragment.kt
.
Subsequently we will add imports from android SDK and other packages. Here are some of the imports we will use in this class:
Bundle
from theandroid.os
package.Fragment
from theandroid.support.v4.app
package.LayoutInflater
from theandroid.view
package.View
from theandroid.view
package.ViewGroup
from theandroid.view
package.*
from thekotlinx.android.synthetic.main.fragment_main
package.
Furthermore create a class that derives from Fragment
and add its contents as follows:
Here is the full code:
package replace_with_your_package_name
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.irozon.sneaker.Sneaker
import kotlinx.android.synthetic.main.fragment_main.*
class MainFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btShowWarning.setOnClickListener {
Sneaker.with(this)
.setTitle("Warning!!")
.setCornerRadius(5, 5)
.setMessage("This is the warning message")
.sneakWarning()
}
}
}
(b). MainActivity.kt
Our
MainActivity
class.
Here is the full code:
package replace_with_your_package_name
import android.graphics.Typeface
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.widget.TextView
import android.widget.Toast
import com.irozon.sneaker.Sneaker
import com.irozon.sneaker.interfaces.OnSneakerDismissListener
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
with(supportFragmentManager.beginTransaction()) {
this.add(R.id.fragment, MainFragment())
this.commit()
}
btShowError.setOnClickListener {
Sneaker.with(this)
.setTitle("Error!!")
.setMessage("This is the error message")
.setTypeface(Typeface.createFromAsset(this.assets, "font/Slabo27px-Regular.ttf"))
.sneakError()
}
btShowSuccess.setOnClickListener {
val sneaker = Sneaker.with(viewGroup)
val view = LayoutInflater.from(this).inflate(R.layout.custom_view, sneaker.getView(), false)
view.findViewById<TextView>(R.id.tvInstall).setOnClickListener{
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show()
}
sneaker.sneakCustom(view)
}
}
}
Reference
Download the code below:
No. | Link |
---|---|
1. | Download Full Code |
2. | Read more here. |
3. | Follow code author here. |
marcoscgdev/MaterialToast
A fully and highly customizable material designed Toast for Android..
Here is a demo:
Step 1: Adding the depencency
Add this to your root build.gradle
file:
Now add the dependency to your app build.gradle file:
Step 2: Creating a Toast
For Native version:
Also with custom icon:
MaterialToast.makeText(activity, "Hello, I'm a material toast!", R.mipmap.ic_launcher, Toast.LENGTH_SHORT).show();
And also with custom background color (text will be automatically colored based on background color):
MaterialToast.makeText(activity, "Hello, I'm a material toast!", R.mipmap.ic_launcher, Toast.LENGTH_SHORT).setBackgroundColor(Color.RED).show();
With custom duration (in millis):
MaterialToast.makeText(activity, "Hello, I'm a material toast!", R.mipmap.ic_launcher, 4000).setBackgroundColor(Color.RED).show();
Here is a complete code snippet:
new MaterialToast(activity)
.setMessage("Hello, I'm a material toast!")
.setIcon(R.mipmap.ic_launcher)
.setDuration(Toast.LENGTH_SHORT)
.setBackgroundColor(Color.RED)
.show();
Full Example
Let us look at a full MaterialToast
Library Example below.
Step 1. Design Layouts
(a). activity_main.xml
Our
activity_main
layout.
Design your XML layout using the following 3 UI widgets and ViewGroups:
!--
LinearLayout
Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default toast"
android:layout_margin="4dp"
android:onClick="toastDef" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Material toast"
android:layout_margin="4dp"
android:onClick="toastMat" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Colored material toast"
android:layout_margin="4dp"
android:onClick="toastMatCol" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Material toast in view"
android:layout_margin="4dp"
android:onClick="toastMatView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom view material toast"
android:layout_margin="4dp"
android:onClick="toastMatCustom" />
</LinearLayout>
Step 2. Write Code
Finally we need to write our code as follows:
(a). MainActivity.kt
Our
MainActivity
class.
Subsequently we will be creating the following methods:
toastDef(parameter)
- Our function will take aView?
object as a parameter.toastMat(parameter)
- Let's pass aView?
object as a parameter.toastMatCol(parameter)
- Our function will take aView?
object as a parameter.toastMatView(parameter)
- We pass aView?
object as a parameter.toastMatCustom(parameter)
- Our function will take aView?
object as a parameter.
Just copy the code below and replace the package with your app's package name.
package replace_with_your_package_name
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.marcoscg.materialtoast.MaterialToast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun toastDef(v: View?) {
Toast.makeText(this, "Hello, I'm a default toast!", Toast.LENGTH_SHORT).show()
}
fun toastMat(v: View?) {
MaterialToast.makeText(this, "Hello, I'm a material toast!", R.mipmap.ic_launcher, Toast.LENGTH_SHORT).show()
}
fun toastMatCol(v: View?) {
// MaterialToast.makeText(this, "Hello, I'm a material toast!",
// R.mipmap.ic_launcher, Toast.LENGTH_SHORT).setBackgroundColor(Color.RED).show();
MaterialToast(this)
.setMessage("Hello, I'm a material toast!")
.setIcon(R.mipmap.ic_launcher)
.setDuration(Toast.LENGTH_SHORT)
.setBackgroundColor(Color.RED)
.show()
}
fun toastMatView(v: View?) {
// offsetX and offsetY are optional
MaterialToast.makeText(this, "Hello, I'm a material toast!", Toast.LENGTH_SHORT).show(v, offsetY = -60)
}
fun toastMatCustom(v: View?) {
MaterialToast(this)
.setCustomView(View.inflate(this, R.layout.custom_toast_view, null))
.setDuration(Toast.LENGTH_SHORT)
.show()
}
}
Reference
Download the code below:
No. | Link |
---|---|
1. | Download Full Code |
2. | Read more here. |
3. | Follow code author here. |
Toasty
The usual Toast, but with steroids.
Toasty
The usual Toast, but with steroids.
Step 1: Declare Jitpack
Add this in your root build.gradle
file (not your module build.gradle
file):
Step 2: Add Dependency
Add this to your module's build.gradle
file (make sure the version matches the JitPack badge above):
Step 3: Configuration
This step is optional, but if you want you can configure some Toasty parameters. Place this anywhere in your app:
Toasty.Config.getInstance()
.tintIcon(boolean tintIcon) // optional (apply textColor also to the icon)
.setToastTypeface(@NonNull Typeface typeface) // optional
.setTextSize(int sizeInSp) // optional
.allowQueue(boolean allowQueue) // optional (prevents several Toastys from queuing)
.setGravity(int gravity, int xOffset, int yOffset) // optional (set toast gravity, offsets are optional)
.supportDarkTheme(boolean supportDarkTheme) // optional (whether to support dark theme or not)
.setRTL(boolean isRTL) // optional (icon is on the right)
.apply(); // required
You can reset the configuration by using reset()
method:
Step 4: Usage
Each method always returns a Toast
object, so you can customize the Toast
much more. DON'T FORGET THE show() METHOD!
show()
To display an error Toast
:
To display a success Toast
:
To display an info Toast
:
To display a warning Toast
:
To display the usual Toast
:
To display the usual Toast
with icon:
You can also create your custom Toasts
with the custom()
method:
Toasty.custom(yourContext, "I'm a custom Toast", yourIconDrawable, tintColor, duration, withIcon,
shouldTint).show();
Screenshots
Please click the image below to enlarge.
Full Example
Awaiting below is a full android sample to demonstrate this Toast
Library concept.
Step 1. Write Code
Finally we need to write our code as follows:
(a). MainActivity.java
Our
MainActivity
class.
First Create a java file named MainActivity.java
.
Then we will be creating the following methods:
getFormattedMessage()
- It will returnCharSequence
object.
Here is the code showing how to use Toasty
:
package replace_with_your_package_name;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;
import android.view.View;
import es.dmoral.toasty.Toasty;
import static android.graphics.Typeface.BOLD_ITALIC;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button_error_toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.error(MainActivity.this, R.string.error_message, Toasty.LENGTH_SHORT, true).show();
}
});
findViewById(R.id.button_success_toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.success(MainActivity.this, R.string.success_message, Toasty.LENGTH_SHORT, true).show();
}
});
findViewById(R.id.button_info_toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.info(MainActivity.this, R.string.info_message, Toasty.LENGTH_SHORT, true).show();
}
});
findViewById(R.id.button_warning_toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.warning(MainActivity.this, R.string.warning_message, Toasty.LENGTH_SHORT, true).show();
}
});
findViewById(R.id.button_normal_toast_wo_icon).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.normal(MainActivity.this, R.string.normal_message_without_icon).show();
}
});
findViewById(R.id.button_normal_toast_w_icon).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Drawable icon = getResources().getDrawable(R.drawable.ic_pets_white_48dp);
Toasty.normal(MainActivity.this, R.string.normal_message_with_icon, icon).show();
}
});
findViewById(R.id.button_info_toast_with_formatting).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.info(MainActivity.this, getFormattedMessage()).show();
}
});
findViewById(R.id.button_custom_config).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.Config.getInstance()
.setToastTypeface(Typeface.createFromAsset(getAssets(), "PCap Terminal.otf"))
.allowQueue(false)
.apply();
Toasty.custom(MainActivity.this, R.string.custom_message, getResources().getDrawable(R.drawable.laptop512),
android.R.color.black, android.R.color.holo_green_light, Toasty.LENGTH_SHORT, true, true).show();
Toasty.Config.reset(); // Use this if you want to use the configuration above only once
}
});
}
private CharSequence getFormattedMessage() {
final String prefix = "Formatted ";
final String highlight = "bold italic";
final String suffix = " text";
SpannableStringBuilder ssb = new SpannableStringBuilder(prefix).append(highlight).append(suffix);
int prefixLen = prefix.length();
ssb.setSpan(new StyleSpan(BOLD_ITALIC),
prefixLen, prefixLen + highlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return ssb;
}
}
Step 2. 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.
To start inside your /res/layout/
directory create an xml layout file named activity_main.xml
.
Subsequently design your XML layout using the following 3 UI widgets and ViewGroups:
ScrollView
RelativeLayout
Button
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="es.dmoral.toastysample.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:text="@string/error_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button_error_toast"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="@string/success_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_error_toast"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button_success_toast"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="@string/info_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_success_toast"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button_info_toast"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="@string/info_toast_with_formatting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button_info_toast"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button_info_toast_with_formatting"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="@string/warning_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_info_toast_with_formatting"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/button_warning_toast" />
<Button
android:text="@string/normal_toast_without_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_warning_toast"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button_normal_toast_wo_icon"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="@string/normal_toast_with_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_normal_toast_wo_icon"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button_normal_toast_w_icon"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:text="@string/custom_configuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_normal_toast_w_icon"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button_custom_config"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</ScrollView>
Reference
Download the code below:
No. | Link |
---|---|
1. | Download Full Code |
2. | Read more here. |
3. | Follow code author here. |