Introduction

In this article, we are going to see how to create Toasts in Android using the Android Studio. Toasts show you a request message; i.e., an interruption or warning message which is displayed on-screen.
So, let us begin.
Step 1
Create a new project in Android Studio.
Android
Give a name to the project and click "Next".
Android
Select the "Phone and Tablet" and click "Next".
Android
Select an empty activity and click "Next".
Android
At last, give the activity name and click on "Finish".
Android
Step 2
Setup the gradle by just locating the Gradle Scripts>>Build. Gradle 1.
Android
Type the following dependency in your app's build.gradle.
Android
Code copy is here,
  1. maven { url "https://jitpack.io"}
Step 3
Locate the Gradle Scripts>>Build. Gradle 2.
Android
And, type the following dependency in your app's build.gradle.
Android
Code copy is here.
  1. compile 'com.github.GrenderG:tasty:1.2.5'
Step 4
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
Android
And just type the code as shown below.
Android
Code copy is here.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:id="@+id/button_error"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="Show error toast"
  13. android:onClick="showtoast "/>
  14. <Button
  15. android:id="@+id/button_success"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="Show success toast"
  19. android:onClick="showtoast "/>
  20. <Button
  21. android:id="@+id/button_info"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="Show info toast"
  25. android:onClick="showtoast "/>
  26. <Button
  27. android:id="@+id/button_warning"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="Show warning toast"
  31. android:onClick="showtoast "/>
  32. <Button
  33. android:id="@+id/button_normal"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="Show normal toast"
  37. android:onClick="showtoast "/>
  38. </Linearlayout>
Step 5
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page.
Android
And just type the code.
Android
Code copy is here,
  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. }
  7. public void showtoast(View v){
  8. switch (v.getId()){
  9. case R.id.button_error:
  10. Toasty.error(context:this,message:"This is an error Toast", Toast.LENGTH_SHORT).show();
  11. break;
  12. case R.id.button_success:
  13. Toasty.success(context:this,message:"This is an success Toast", Toast.LENGTH_SHORT).show();
  14. break;
  15. case R.id.button_info:
  16. Toasty.info(context:this,message:"This is an info Toast", Toast.LENGTH_SHORT).show();
  17. break;
  18. case R.id.button_warning:
  19. Toasty.warning(context:this,message:"This is an warning Toast", Toast.LENGTH_SHORT).show();
  20. break;
  21. case R.id.button_normal:
  22. Toasty.normal(context:this,message:"This is an normal Toast", Toast.LENGTH_SHORT).show();
  23. break;
  24. }
  25. }
  26. }
Step 6
After step 4, sync all the dependency gradles and Mainactivity.java resource files by clicking the Sync button on the top right corner of the Gradle page.
Android
Step 7
Verify the preview.
After the code is applied, the preview will appear like this.
Android
Step 8
Next, go to Android Studio and deploy the application. Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.
Android
Run the application in your desired emulator (Shift + F10).
Android
Explanation of source code
The source code provided in this article is just the dependencies of Toast activity and the code used in activity_main.xml will make the messages appear and to define its attributes.

Summary

In this article, we created the app named Toasty project. Then, we inserted a Gradle and we learned how to use the Toasts. Finally, we have deployed that as an output.
I hope you have liked this article.