A fully and highly customizable material designed Toast for Android.
Here is the demo image:
You can download the sample apk here.
Step 1: Install it
Add this to your root _MARKDOWN_HASHc197962302397baf3a4cc36463dce5eaMARKDOWNHASH
file:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Now add the dependency to your app build.gradle
file:
implementation 'com.github.marcoscgdev:MaterialToast:1.0.2'
Step 2: Create Toast
- Native version
MaterialToast.makeText(activity, "Hello, I'm a material toast!", Toast.LENGTH_SHORT).show();
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();
NEW! With custom duration (in millis):
MaterialToast.makeText(activity, "Hello, I'm a material toast!", R.mipmap.ic_launcher, 4000).setBackgroundColor(Color.RED).show();
- Complete version
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
Below is the full example:
1. Layouts
We have one layout:
activity_main.xml
<?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>
2. Code
Here is the kotlin code:
MainActivity.kt
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 code here.
Read more here.
Follow code author here.