Toasty - library example in android studio
What Will I Learn?
Toast - One of the most important features of Android. When there is a confirmation is shown of any work on Android, it is called Toast. Suppose, you've got an app to watch a movie in your Android device. Then you have register into the app. As soon as you are registered in the app, you can see the writing that says -" Thanks for registering". This text is called toast. Toast is an essential tool for an Android app. Many times to show a lot of toast in the Android app. To make this work easier, this toasti android library What you can learn from this tutorial -
- Through this tutorial you will learn how to add "Toasty" library in android studio projects.
- Through this tutorial you will learn how to use toast in android studio projects.
- Through this tutorial you will learn how to add library in android studio projects.
Requirements
- Android Studio (Android Application crating IDE)
- The GitHub repository of "Toasty" library
- Genymotion (As Android Virtual Device)
Difficulty
- Simple and easy.
Tutorial Contents
Lets try an example where we will use TOASTY library inside android application -
Paste the following code inside the "dependencies()" method of build.graddle(app) file :-
compile 'com.github.GrenderG:Toasty:1.2.5'
Paste the following code inside the "repositories" area of build.graddle(app) file :-
maven { url "https://jitpack.io" }
Paste the following code inside the "colors.xml " file :-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorAccent">#FF4081</color>
<color name="fourth_color">#2196f3</color>
<color name="third_color">#9c27b0</color>
<color name="second_color">#e91e63</color>
<color name="colorPrimary">#3F51B5</color>
<color name="first_color">#f44336</color>
<color name="fifth_color">#f44336</color>
<color name="colorPrimaryDark">#303F9F</color>
</resources>
There will be no more code inside the colors.xml file . You can see that I have used 8 types of colors through the color codes. I will use these inside the activity_main.xml & MainActivity.java file.
Paste the following code inside activity_main.xml file :
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.example.mmaruf.myapplication.MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast for showing wrong " />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast for showing any successful work done " />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast to show any info " />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast to show any warning " />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="An example for showing normal Toast " />
</LinearLayout>
No other code will be there inside the file. I have used 6 different buttons to show different kinds of toast by the TOASTY library.
Paste the following code inside MainActivity.java file :
import android.support.v7.app.AppCompatActivity;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
import android.view.View;
import android.widget.Button;
import es.dmoral.toasty.Toasty;
public class MainActivity extends AppCompatActivity {
Button first_button;
Button second_utton;
Button third_button;
Button fourth_button;
Button fifth_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toasty.Config.getInstance()// initializing toasty library
.setErrorColor(ContextCompat.getColor(getApplicationContext(),R.color.first_color))//1st color code for the first button
.setErrorColor(ContextCompat.getColor(getApplicationContext(),R.color.second_color))//2nd color code for the second button
.setErrorColor(ContextCompat.getColor(getApplicationContext(),R.color.third_color))//3rd color code for the 3rd button
.setErrorColor(ContextCompat.getColor(getApplicationContext(),R.color.fourth_color))//4thcolor code for the 4th button
.setErrorColor(ContextCompat.getColor(getApplicationContext(),R.color.fifth_color))//5th color code for the 5th button
.tintIcon(true)// This line is for coloring the icons
.apply();
first_button =(Button) findViewById(R.id.button1);
second_utton =(Button) findViewById(R.id.button2);
third_button =(Button) findViewById(R.id.button3);
fourth_button =(Button) findViewById(R.id.button4);
fifth_button =(Button) findViewById(R.id.button5);
first_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View my) {
Toasty.error(getApplicationContext(), "Example of a Wrong TOAST", Toast.LENGTH_SHORT, true).show();
}});
second_utton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View my) {
Toasty.success(getApplicationContext(), "Example of a successful TOAST", Toast.LENGTH_SHORT, true).show();
}});
third_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View my) {
Toasty.info(getApplicationContext()," Example of a info TOAST", Toast.LENGTH_SHORT, true).show();
}});
fourth_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View my) {
Toasty.warning(getApplicationContext(), "Example of a warning TOAST", Toast.LENGTH_SHORT, true).show();
}});
fifth_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View my) {
Toasty.normal(getApplicationContext(), "example of a general TOAST", Toast.LENGTH_SHORT).show();
}}); }}
Nothing more will be inside the MainActivity.java file. You will see that I have initialized TOASTY inside the onCreate() method. I have initialized also 5 buttons. Then I have used buttonOnClick method to show the toasts.
Result :
After doing all the works mentioned above, if we run the project inside android studio, we will able to see the following result :
Posted on Utopian.io - Rewarding Open Source Contributors

Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @mmaruf I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x