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.
Give a name to the project and click "Next".
Select the "Phone and Tablet" and click "Next".
Select an empty activity and click "Next".
At last, give the activity name and click on "Finish".
Step 2
Setup the gradle by just locate the Gradle Scripts>>Build. Gradle 1
And type the following dependency in your app's build.gradle.
Code copy is here,
- maven
- {
- url "https://jitpack.io"
- }
Step 3
Locate the Gradle Scripts>>Build.Gradle 2
And type the following dependency in your app's build.gradle.
Code copy is here,
- implementation 'com.muddzdev:styleabletoast:2.0.1'
Step 4
Next, go to app >> res >>Styles and
Just type the code as follows.
Code copy is here,
- <style name="exampleToast">
- <item name="colorBackground">#94ffab</item>
- <item name="textColor">#000</item>
- <item name="iconLeft">@drawable/ic_android</item>
- <item name="strokeColor">#000</item>
- <item name="strokeWidth">30dp</item>
- <item name="length">LONG</item>
- <item name="cornerRadius">3dp</item>
- </style>
Step 5
Next, go to app >> res >> layout >> activity_main.xml. Select activity page
And just type the code as follows.
Code copy is here,
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="showToast"
- android:text="showToast"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"/>
Step 6
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page:
And just type the code as follows,
Code copy is here,
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.widget.Toast;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- public void showToast(View v)
- {
- Toast toast = Toast.makeText(this,"hello",Toast.LENGTH_SHORT);
- toast.setGravity(Gravity.CENTER_VERTICAL| Gravity.START,90,0);
- toast.show();
- }
- }
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.
Step 8
Verify the preview.
->After the code is applied, the preview will appear like this.
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.
Run the application in your desired emulator (Shift + F10).
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*