Toast & Custom Toast With Example In Android Studio

In Android, Toast is used to display information for a period of time. It contains a message to be displayed quickly and disappears after specified period of time. It does not block the user interaction. Toast is a subclass of Object class. In this we use two constants for setting the duration for the Toast. Toast notification in android always appears near the bottom of the screen. We can also create our custom toast by using custom layout(xml file).

Toast custom toast in Android

Special Note: In Android, Toast is used when we required to notify user about an operation without expecting any user input. It displays a small popup for message and automatically fades out after timeout.


Important Methods Of Toast:

Let’s we discuss some important methods of Toast that may be called in order to manage the Toast.

1. makeText(Context context, CharSequence text, int duration): This method is used to initiate the Toast. This method take three parameters First is for the application Context, Second is text message and last one is duration for the Toast.

Constants of Toast: Below is the constants of Toast that are used for setting the duration for the Toast.

1. LENGTH_LONG: It is used to display the Toast for a long period of time. When we set this duration the Toast will be displayed for a long duration.

2. LENGTH_SHORT: It is used to display the Toast for short period of time. When we set this duration the Toast will be displayed for short duration.

Below we show the use of makeText() method of Toast in which we set application context, a text message and duration for the Toast.

Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast", Toast.LENGTH_LONG); // initiate the Toast with context, message and duration for the Toast

2. show(): This method is used to display the Toast on the screen. This method is display the text which we create using makeText() method of Toast.

Below we Firstly initiate the Toast and then display it using show() method.

吐司吐司=吐司。makeText(getApplicationContext(),“Android中的简单Toast”,Toast。长度(长);//使用Toast的上下文、消息和持续时间启动Toast
干杯show();//展示烤面包片

3. setGravity(int,int,int): This method is used to set the gravity for the Toast. This method accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

Below we Firstly initiate the Toast, set top and left gravity and then display it using show() method.

吐司吐司=吐司。makeText(getApplicationContext(),“Android中的简单Toast”,Toast。长度(长);//使用Toast的上下文、消息和持续时间启动Toast
干杯设置重力(Gravity.TOP | Gravity.LEFT,0,0);//设置吐司的重力。
干杯show();//展示烤面包片

4. setText(CharSequence s): This method is used to set the text for the Toast. If we use makeText() method and then we want to change the text value for the Toast then we use this method.

下面我们首先使用makeText()方法创建一个新的Toast,然后设置Toast的文本。

吐司吐司=吐司。makeText(getApplicationContext(),“Android中的简单Toast”,Toast。长度(长);//使用Toast的上下文、消息和持续时间启动Toast
干杯设置重力(Gravity.TOP | Gravity.LEFT,0,0);//设置吐司的重力。
干杯setText(“更改的Toast文本”);//设置祝酒词的文本
干杯show();//展示烤面包片

5、设置持续时间(int duration): 此方法用于设置Toast的持续时间。如果我们使用makeText()方法,然后我们想更改Toast的持续时间,那么我们就使用这个方法。

下面我们首先使用makeText()方法创建一个新的Toast,然后设置Toast的持续时间。

吐司吐司=吐司。makeText(getApplicationContext(),“Android中的简单Toast”,Toast。长度(长);//使用Toast的上下文、消息和持续时间启动Toast
干杯设置重力(Gravity.TOP | Gravity.LEFT,0,0);//设置吐司的重力。
干杯设置持续时间(Toast.LENGTH\u SHORT);//设置烤面包的持续时间。
干杯show();//展示烤面包片

6、充气(int、ViewGroup): 此方法用于从 xml. 在此方法中,第一个参数是布局资源ID,第二个参数是根视图。

下面我们检索布局充气器,然后从 xml 文件

//检索布局充气器并从xml中充气布局
LayoutInflater充气器=getLayoutInflater();
视图布局=充气机。充气(R.layout.custom\u toast\u layout,
(ViewGroup)findViewById(R.id.toast\u layout\u root));

7、setView(视图): 此方法用于设置Toast的视图。在这个方法中,我们传递充气的布局,我们使用充气()方法充气。

下面,我们首先检索布局充气器,然后充气布局,最后创建一个新的Toast,并在setView()方法中传递充气的布局。

//检索布局充气器并从xml中充气布局
LayoutInflater充气器=getLayoutInflater();
视图布局=充气机。充气(R.layout.custom\u toast\u layout,
(ViewGroup)findViewById(R.id.toast\u layout\u root));

//使用上下文创建新的Toast
Toast Toast=新Toast(getApplicationContext());
干杯设置持续时间(Toast.LENGTH\u LONG);//设置烤面包的持续时间
干杯setView(布局);//设置充气布局
toast.show(); // display the custom Toast

 Android中的自定义Toast:

在Android中,有时简单的Toast可能并不令人满意,然后我们可以定制Toast。要创建自定义布局,请在XML中定义视图布局,并将根视图对象传递给setView(View)方法。

在Android中实现自定义Toast的步骤:

步骤1: 首先使用getLayoutFlater()(或getSystemService())检索布局充气器,然后使用充气(int,ViewGroup)从XML中充气布局。在充气方法中,第一个参数是布局资源ID,第二个参数是根视图。

第2步: 使用Toast(上下文)创建新的Toast,并设置Toast的一些属性,例如持续时间和重力。

Step 3: 调用setView(View)并在此方法中传递膨胀的布局。

Step 4: 使用Toast的show()方法在屏幕上显示Toast。

在下面的示例中,我们展示了Toast和custom Toast的功能。


Android Studio中的Toast和自定义Toast示例:

下面是Android中Toast和自定义Toast的示例。在本例中,我们显示了两个 按钮一个用于简单Toast,另一个用于自定义Toast并对其执行单击事件。每当用户单击简单Toast 按钮 当用户单击自定义Toast时,屏幕上会显示消息“简单的Android Toast” 按钮 屏幕上会显示一条消息“Android中的自定义Toast”,并显示一幅图像。为了创建自定义toast,我们首先检索布局膨胀器,然后从xml文件中膨胀自定义toast布局。之后,我们得到 TextView and ImageView 从膨胀的布局中,并在 TextView and ImageView. 最后,我们创建一个新的Toast并在setView()方法中传递膨胀的布局,然后使用Toast的show()方法显示Toast。

以下是最终输出,下载 Android Studio 代码和示例的逐步说明:

下载代码

Toast And Custom Toast Example in Android

步骤1: 创建一个新项目并将其命名为Steexample

第2步:  打开res->layout->activity\u main。xml(或)main。xml并添加以下代码:

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:工具=”http://schemas.android.com/tools"
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”
android:paddingBottom=“@尺寸/活动\u垂直\u边距”
android:paddingLeft=“@尺寸/活动\u水平\u边距”
android:paddingRight=“@dimen/activity\u horizontal\u margin”
android:paddingTop=“@dimen/activity\u vertical\u margin”
工具:context=“.MainActivity”>
<!-- 按钮用于简单定制的吐司-->
<按钮
android:id=“@+id/simpleToast”
android:layout\u width=“200dp”
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout\u marginTop=“150dp”
android:背景=“#f00”
android:text=“简单吐司”
android:textColor="#fff"
android:textSize="20sp" />

<按钮
android:id=“@+id/customToast”
android:layout\u width=“200dp”
android:layout_height="wrap_content"
android:layout_below="@+id/simpleToast"
android:layout_centerHorizontal="true"
android:layout_margin="50dp"
android:background="#0f0"
android:text="Custom Toast"
android:textColor="#fff"
android:textSize="20sp" />


Step 3:  Now create a xml layouts by right clicking on res/layout -> New -> Layout Resource File and name it custom_toast_layout.xml







Step 4: Open   src -> package -> MainActivity.java

In this step we open MainActivity and add the code for initiate the Button’s and perform click event on Button’s. Whenever a user click on simple Toast Button a Toast with message “Simple Toast In Android” displayed on the screen and when a user clicks on custom toast Button a message “Custom Toast In Android” with a image displayed on the screen. For Creating a custom toast we firstly retrieve the layout inflater and then inflate the custom toast layout from the xml file. After that we get the reference of TextView and ImageView from the inflated layout and set the text and image in the TextView and ImageView. Finally we create a new Toast and pass the inflated layout in the setView() method and then display the Toast by using show() method of Toast.

package com.abhiandroid.toastexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
导入android。小装置。文本视图;
导入android。小装置。干杯
导入android。小装置。按钮
导入android。看法视图组;

公共类MainActivity扩展了AppCompatActivity{

    按钮simpleToast,customToast;

    @覆盖
    创建时受保护的void(Bundle savedInstanceState){
        超级的onCreate(savedInstanceState);
        setContentView(R.layout.activity\u main);
        //获取按钮的参考
        simpleToast=(按钮)findViewById(R.id.simpleToast);
        customToast=(按钮)findViewById(R.id.customToast);
        //在简单Toast按钮上执行setOnClickListener事件
        simpleToast。setOnClickListener(新视图。OnClickListener(){
            @覆盖
            公共void onClick(视图v){
                //启动带有消息和持续时间的Toast
                吐司吐司=吐司。makeText(getApplicationContext(),“Android中的简单Toast”,Toast。长度(长);//使用Toast的上下文、消息和持续时间启动Toast
                干杯设置重力(Gravity.BOTTOM | Gravity.CENTER\u HORIZONTAL,0,0);//设置吐司的重力。
                干杯show();//展示烤面包片

            }
        });
        //在自定义Toast按钮上执行setOnClickListener事件
        自定义Toast。setOnClickListener(新视图。OnClickListener(){
            @覆盖
            公共void onClick(视图v){
                //检索布局充气器并从xml中充气布局
                LayoutInflater充气器=getLayoutInflater();
                视图布局=充气机。充气(R.layout.custom\u toast\u layout,
                        (ViewGroup)findViewById(R.id.toast\u layout\u root));
                //从充气布局中获取TextView和ImageVIew的引用
                text视图到ast text视图=(text视图)布局。findViewById(R.id.toastTextView);
                ImageView到估计视图=(ImageView)布局。findViewById(R.id.to估计视图);
                //在文本视图中设置文本
                toastTextView。setText(“Android中的自定义Toast”);
                //在ImageView中设置图像
                ToEstimatageView。setImageResource(R.drawable.ic\u启动器);
                //使用上下文创建新的Toast
                Toast Toast=新Toast(getApplicationContext());
                干杯设置持续时间(Toast.LENGTH\u LONG);//设置烤面包的持续时间
                干杯setView(布局);//设置充气布局
                toast.show(); // display the custom Toast

            }
        });
    }
}

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!

2 thoughts on “Toast & Custom Toast With Example In Android Studio”