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:
- 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.
- 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:
- 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.
- 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.
- 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.
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() } }