Define Custom Location For Toast Using Android Studio

Introduction

 
In the previous article, we learned about Toast. Continuing in this article we are going to see how to define a custom location for  Toast in Android apps using Android Studio. It will allow us to define color, structure, style, design, shape, etc.
 
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 locate the Gradle Scripts>>Build. Gradle 1
 
Android
 
And type the following dependency in your app's build.gradle.
Android
 
Code copy is here,
  1. maven  
  2. {  
  3.  url "https://jitpack.io"  
  4. }  
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. implementation 'com.muddzdev:styleabletoast:2.0.1'  
Step 4
 
Next, go to app >> res >>Styles and
 
Android
 
Just type the code as follows.
 
Android
 
Code copy is here,
  1. <style name="exampleToast">  
  2.        <item name="colorBackground">#94ffab</item>  
  3.        <item name="textColor">#000</item>  
  4.        <item name="iconLeft">@drawable/ic_android</item>  
  5.        <item name="strokeColor">#000</item>  
  6.        <item name="strokeWidth">30dp</item>  
  7.        <item name="length">LONG</item>  
  8.        <item name="cornerRadius">3dp</item>  
  9.    </style>  
Step 5
 
Next, go to app >> res >> layout >> activity_main.xml. Select activity page
 
Android
 
And just type the code as follows.
 
Android
 
Code copy is here,
  1. <Button  
  2.         android:layout_width="wrap_content"  
  3.         android:layout_height="wrap_content"  
  4.         android:onClick="showToast"  
  5.         android:text="showToast"  
  6.         app:layout_constraintBottom_toBottomOf="parent"  
  7.         app:layout_constraintLeft_toLeftOf="parent"  
  8.         app:layout_constraintRight_toRightOf="parent"  
  9.         app:layout_constraintTop_toTopOf="parent"/>  
Step 6
 
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page:
 
Android
 
And just type the code as follows,
 
Android
 
Code copy is here,
  1. import android.support.v7.app.AppCompatActivity;  
  2. import android.os.Bundle;  
  3. import android.view.Gravity;  
  4. import android.view.View;  
  5. import android.widget.Toast;  
  6.   
  7. public class MainActivity extends AppCompatActivity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.     }  
  14.       
  15.     public void showToast(View v)  
  16.     {  
  17.      Toast toast = Toast.makeText(this,"hello",Toast.LENGTH_SHORT);  
  18.      toast.setGravity(Gravity.CENTER_VERTICAL| Gravity.START,90,0);  
  19.      toast.show();  
  20.     }  
  21. }  
Step 7
 
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 8
 
Verify the preview.
 
->After the code is applied, the preview will appear like this.
 
Android
 
Step 9
 
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 and the code used in activity_main.xml will make the Toast to appear and to define its attributes.
 

Summary

 
In this article we created the app named Toast, then we have inserted a Gradle and we learned how to use the circular process bar and finally, we have deployed that as an output.
 
*Support and Share, Thank You*


Similar Articles