Toasts In Android Using Android Studio

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.   
  9.     <Button  
  10.         android:id="@+id/button_error"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Show error toast"  
  14.         android:onClick="showtoast "/>  
  15.   
  16.     <Button  
  17.         android:id="@+id/button_success"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="Show success toast"  
  21.         android:onClick="showtoast "/>  
  22.   
  23.     <Button  
  24.         android:id="@+id/button_info"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="Show info toast"  
  28.         android:onClick="showtoast "/>  
  29.   
  30.     <Button  
  31.         android:id="@+id/button_warning"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="Show warning toast"  
  35.         android:onClick="showtoast "/>  
  36.   
  37.     <Button  
  38.         android:id="@+id/button_normal"  
  39.         android:layout_width="wrap_content"  
  40.         android:layout_height="wrap_content"  
  41.         android:text="Show normal toast"  
  42.         android:onClick="showtoast "/>  
  43.   
  44. </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.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.     }  
  8.   
  9.     public  void showtoast(View v){  
  10.         switch (v.getId()){  
  11.             case R.id.button_error:  
  12.                 Toasty.error(context:this,message:"This is an error Toast", Toast.LENGTH_SHORT).show();  
  13.                 break;  
  14.             case R.id.button_success:  
  15.                 Toasty.success(context:this,message:"This is an success Toast", Toast.LENGTH_SHORT).show();  
  16.                 break;  
  17.             case R.id.button_info:  
  18.                 Toasty.info(context:this,message:"This is an info Toast", Toast.LENGTH_SHORT).show();  
  19.                 break;  
  20.             case R.id.button_warning:  
  21.                 Toasty.warning(context:this,message:"This is an warning Toast", Toast.LENGTH_SHORT).show();  
  22.                 break;  
  23.             case R.id.button_normal:  
  24.                 Toasty.normal(context:this,message:"This is an normal Toast", Toast.LENGTH_SHORT).show();  
  25.                   break;  
  26.         }  
  27.     }  
  28. }  
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.  


Similar Articles