Android Tutorial

Android Tutorial Android Toast Button Widget In Android Tutorial Imageview Widget In Android Android Date Picker Example Android Time Picker Example Android Operating System Android Activity Android Architecture Android Content Providers Android Edittext Android Emulator Android Fragments Android Gridview Android Broadcast Receivers Event Handling In Android What Is Android Studio Android Navigation Drawer Android Recyclerview Android Scrollview Android Navigation Android Operating System Android ListView Android Studio Resources Android Studio's Layout Editor Android TextView Android Styles and Themes How To Show Image On An Android Application How To Insert An Email In Android Application In Android Version 8 How To Insert The Mobile Number On An Android Application Using Edittext Difference between android developer and web AdapterViewFlipper in Android with Example Android 9.0 Pie vs. Android 10, which one is better Android Development or Web Development which one is the best to choose Android Project Folder Structure Assets folder in Android How to reduce the size of APK in Android Top 7 Android App Development Books Top Programming Languages for Android Development Android Navigation Application Components Difference between android developer and web developer Event Handling in Android How to reduce the size of APK in Android Top 7 Android App Development Books Top Programming Languages for Android Development AdapterViewFlipper in Android with Example Android 9.0 “Pie” vs. Android 10, which one is better Android Development or Web Development which one is the best to choose Android EditText Android Fragments How to Update Gradle in Android Studio Navigation Drawer ScrollView What is Android Studio? Android Architecture Android Content Providers Android GridView Android RecyclerView Android Studio’s Layout Editor

Android Toast

Android is one of the most successful operating systems available in today's world. It is an open-source phone platform, compatible with almost all the devices. There are numerous features and operations that can be performed on android enabled devices. We also have widgets on android which allow interactive components. There are various dominations of android widgets such as button, toast, checkbox, spinner, alert dialog, etc. In this article we will take a look at toast widget.

What is Android Toast?

An android toast provides feedback to the users about the operation being performed by them. It displays the message regarding the status of operation initiated by the user. A toast does not interrupt the currently occurring process; it only occupies the space provided by the programmer and disappears in a short time. The message displayed by the toast does not require any special input by the user. Usually the message is displayed at the end of the screen, but the position, layout and timeout can be altered by the programmer. We have seen what toast does, not let us see why it is called as 'toast'? Well it is because the toast pops up on our phone screen in the same way as bread toast pops up from toaster.

Toast was developed for the following reason

Even after giving input, the users sometimes used to remain confused whether the input is accepted by the system or not. In such cases toast lets the user know that the operation initiated is being carried out. The status regarding completion of tasks sometimes used to remain in dilemma. Display of other relevant messages was sometimes needed.

Toast Class

Toast belongs to android.widget.Toast class with its parent class being java.lang.Objectclass.

Constants of Toast class:

  1. public static final int LENGTH_LONG - It displays messages for long duration of time. It is used for more serious messages, which the programmer does not want to go unnoticeable by the user.
  2. public static final int LENGTH_SHORT - It displays messages for short duration of time. It is used to display normal messages, which are important but will not cause a serious problem if went unnoticed.

Methods of Toast class:

  1. public static Toast makeText(context, text, duration) - This method makes the toast which contains some text value and has timeout. Here context specifies the context in which the toast is being passed, text displays the message to be printed on the user screen. Now the message can be passed in double simply by writing it in the method parameter or by passing a java string object. Duration specifies the amount of time for which the message is to be displayed on the screen.
  2. public void show() - once the toast has been created, we need a method to display the it. It is provided by show (). We can simply display the toast by calling this method.
  3. public void setMargin (float horizontalMargin, float verticalMargin) - The toast message needs to be displayed on the screen but it is important to decide on what portion of screen the message is to be displayed? It is provided by setMargin method. It is used to manipulate the margins; both horizontal and vertical margin.
Program to show making of toast:
package example.tutorialandexample.com.toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class Mactivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show()
}
}