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

Related Topics

Content Providers of Android with Example

An essential part of Android that performs the function of a relational database by storing application data is called a content provider. The function of the content provider in the...

13 minutes read.

What is Android Studio

Android Studio powered by the IntelliJ platform is an integrated development environment (IDE) and is officially recognized for developing androidapplications. Its foundation lies on JetBrains' IntelliJ IDEA and is specialized...

3 minutes read.

Android Date picker example

How to pick date from the user in Android? Write an android example to pick date from the user and display it. Android provides control for the user to pick a date...

3 minutes read.

Android Studio Resources

To build an outstanding Android application there are many items that you need to use. Aside from the coding of your application, you need to handle various resources such as...

5 minutes read.

How to crop Image from Camera and Gallery in Android?

In this tutorial, we'll learn how to crop photos in the Android Gallery and Camera. There is no crop feature in this project, contrary to what was covered in the...

5 minutes read.

Button Widget in Android Tutorial

Explanation: Button is a widget under Buttons section which is used to execute specific code. Example: XML file: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">     <Button         android:id="@+id/button"         android:layout_width="wrap_content"    ...

1 minute read.

Android Navigation Drawer

A sliding menu used to show important links in the Android Application is called Navigation Drawer. The navigation drawermakes it easier to navigate between the fragments. It is not displayed...

5 minutes read.

Android Time picker example

How to pick Time from the user in android? Write an android example to pick Time from the user and display the date to the user. Android provides controls for the user...

3 minutes read.

Dashboard UI Design in Android

One of the essential components that draw the user’s attentions into the application's operation is the dashboard design. It offers details on the application's general functionality in one location. Imagine...

11 minutes read.

Android Studio's Layout Editor

The Layout Editor allows you to efficiently create layouts by dragging UI elements from the Palette into the visual design editor instead of manually creating the layout XML file by...

8 minutes read.

Creating a Calendar View App in Android

This article demonstrates how to make an Android application that uses CalendarView to display the Calendar. It also provides the option to pick the current date and view the date....

3 minutes read.

AdapterViewFlipper in Android with Example

The AdapterViewFlipper class, a subclass of the ViewAnimator class, is what we use to switch from two or multiple views when only one is present at a time. The AdapterViewFlipper...

10 minutes read.

Android GridView

Theview group that displays items in a two-dimensional scrolling grid is called GridView. The data isadded into this grid layout from anArray List or database. The adapter class isutilized to...

4 minutes read.

How to Update Gradle in Android Studio?

The Android Gradle plugin adds a number of features that are unique to develop Android apps to the Gradle build system, which is the foundation of the Android Studio development...

2 minutes read.

Android EditText

In Android, EditText is a UI widget utilized to allow the user to input or change the text. While using the EditTextwidget in your Android application, you need to use...

4 minutes read.

Top Programming Languages for Android Development

For mobile apps, there are so many programming languages. You would need to spend days simply researching if you had to pick one from a list of 30 coding languages...

8 minutes read.

Android ScrollView

A ScrollView is a view group that is used to create vertically scrollable views in an android application. Only a single direct child can be contained by ScrollView. For placing...

5 minutes read.

Android Project Folder Structure

JetBrains community developed Android Studio, the official IDE (Integrated Development Environment) for Android app development and is freely distributed by Google. After the complete Android Architecture setup, we can construct an...

4 minutes read.

How to insert the mobile number on an android application using EditText

Number Insertion in android application: Sometimes while dealing with numbers, we don't want to insert any string inside an number so, we premiliary define an EditText so we externally specify...

2 minutes read.

Android RecyclerView

RecyclerView is a ViewGroup brought to AndroidStudio as successor to ListView and GridView. These are all improvements and are included in the latest v7 support package. It was written to...

8 minutes read.