Toast
trong Android sử dụng rất dễ, chỉ cần sử dụng 1 dòng mã là đủ. Tuy nhiên, đó là sử dụng Toast
mặc định của Android, nếu Toast
có View
phức tạp nhiều hơn thì sẽ nghĩ đến Custom Toast.
Sử dụng Toast mặc định của Android
Gõ 1 dòng code đơn giản như dưới đây:
Toast.makeText(MainActivity.this, "This is Toast Default", Toast.LENGTH_SHORT).show();
Phương thức makeText()
dùng để tạo Toast
, trả về đối tượng Toast
:
this
: truyền vào context hiện tại.text
: 1 đoạn văn bản muốn hiển thị -"This is Toast Default"
.duration
: hiển thịToast
, chỉ cho phép truyền vào 2 giá trị là:LENGTH_SHORT
: hiển thịToast
trong thời gian ngắn.LENGTH_LONG
: hiển thịToast
trong thời gian lâu hơn.
Phương thức show()
dùng để hiển thị Toast
.
Đặt vị trí hiển thị Toast
trên màn hình Android, bằng phương thức setGravity()
và setMargin()
.
Custom Toast trong Android
Custom Toast là thay thế View
hiển thị văn bản của Toast
mặc định trong Android bằng 1 View
khác.
Custom Toast
Tạo View
bằng tập tin XML hoặc Java.
Tạo file toast_layout.xml trong thư mục layout của project
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border_toast_layout"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/home_icon" />
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:textColor="#ffffff"
android:textSize="16dp" />
</LinearLayout>
View này gồm 1 ImageView
hiển thị bên trái và 1 TextView
hiển thị thông báo nằm ở bên phải ImageView
.
Màu nền của View
là 1 drawable @drawable/border_toast_layout dùng để bo tròn 4 góc và tạo màu nền gradient cho View
.
File border_toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp" />
<gradient
android:angle="90"
android:centerColor="#2980b9"
android:endColor="#2c3e50"
android:startColor="#1abc9c" />
</shape>
Tạo View ánh xạ từ toast_layout.xml thông qua lớp LayoutInflater
:
final View viewToast = LayoutInflater.from(this).inflate(R.layout.toast_layout, null);
Có thể tìm ra các View
con, trong viewToast
và đặt giá trị cho các View
.
((TextView)viewToast.findViewById(R.id.tv_text)).setText("This is Custom Toast In Android \n Eitguide.com");
Tạo đối tượng Toast
và sử dụng phương thức setView()
cho Toast
:
Toast toast = new Toast(MainActivity.this);
// Set custom view for toast
toast.setView(viewToast);
toast.setDuration(Toast.LENGTH_SHORT);
//toast.setGravity(Gravity.TOP, 0, 0);
//toast.setGravity(Gravity.RIGHT, 0, 0);
//toast.setGravity(Gravity.BOTTOM, 0, 0);
//toast.setGravity(Gravity.LEFT, 0, 0);
//toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
//toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
Những dòng //
trên là những dòng mã dùng để đặt vị trí hiển thị của Toast
.
Tải source code đầy đủ:
- Tải trực tiếp MasterToastAndroid-master.zip.
- Tải thông qua Github.
Những điều nên nhớ khi sử dụng Toast
- Context của
Toast
không được truyền vàonull
. - Chỉ được thể hiện
Toast
trong UI thread, nếu vẫn thể hiện Toast trong những thread khác sẽ ném ra ngoại lệ và crash chương trình.
Vì sao không thêm View
vào Activity mà vẫn có View
hiển thị lên màn hình? Do Toast
trong Android không thuộc sự quản lý của Activity mà nó thuộc sự quản lý của WindowManager
.