Android Toast Example:
A Toast in android displays a small piece of message to user indication the work done or any errors. It’s a light weight display alert and easy to use in any where in the code.
You can customize the toast as per the link and make it dynamic according to your requirement this is advanced level of toast i.e., in terms of color font style.
Android toast has a duration specification using which we can provide the duration for toast display and toast doesn’t have any action button.
In this tutorial, android toast example is explained by showing with both short toast and long toast.
The syntax for android toast is
Toast.makeText(context, text, duration).show();
context –> Activity in which you want toast to be displayed
text –> Text to be displayed as toast.
duration –> long or short time period
show() –> show is compulsory don’t miss it.
Android Toast Example Video Tutorial :
Go through the below tutorial for more detailed updates on android toast implementation.
https://youtu.be/lyueJvh_Y3U
Creating a toast is simple, there are two types of toast
1) Short length toast
2) Long length toast
Toast can also be customized according to the user requirement by setting gravity for the toast.
toast.setgravity();
Also we can create a layout for toast a display it in next tutorial i will show you how to make a custom toast using a layout just as you design for any other screen.
toast.setView();
Android toast message not disappearing ?? Can set toast duration as i said above we can handle the time for which the toast is appeared on the screen.
toast.setDuration();
Lastly very important thing in a toast is showing it on the screen using
toast.show();
MainActivity.java
In the main activity we are making initialization we can also use View databinding here for efficiency.
public class MainActivity extends Activity { Button shorttoast, longtoast; // initializing buttons @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); shorttoast = (Button)findViewById(R.id.shorttoast); longtoast = (Button)findViewById(R.id.longtoast); shorttoast.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(MainActivity.this, "latestcareer --> short toast", Toast.LENGTH_SHORT).show(); //getting short length toast } }); longtoast.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(MainActivity.this, "latestcareer --> long toast", Toast.LENGTH_LONG).show(); //getting long length toast } }); }
Android toast example kotlin
We will try to see how this toast is displayed in kotlin way
fun showToast(context: Context, message: String) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
Now try to call this method from any where in code as shown below
activity_main.xml
Add buttons to xml screen by using which we can select short and long toast to display and toast doesn’t require any UI design.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.androidcoding.abhi.toast.MainActivity" > <Button android:id="@+id/shorttoast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="130dp" android:layout_gravity="center" android:text="Click here to get short toast" /> <Button android:id="@+id/longtoast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_gravity="center" android:text="Click here to get long toast" /> </LinearLayout>
Output :
The screen depicting usage of android toast example
If you have any query’s on android toast, please share them below in comment section.
Also like and share this tutorial for more interesting android tutorials . If required full code ask me below.