Giới thiệu
Toast trên Android chỉ có 2 độ dài cố định: LENGTH_SHORT (~2 giây) và LENGTH_LONG (~3.5 giây). Bài viết này hướng dẫn cách hiển thị Toast lâu hơn tùy ý bằng cách sử dụng CountDownTimer để lặp lại việc show Toast.
Các bước thực hiện
Bước 1: Tạo dự án mới
Tạo project: File → New Project.
Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Display Toast"
android:textColor="@android:color/background_dark"
android:onClick="showToast" />
</RelativeLayout>
Bước 3: Xử lý logic (src/MainActivity.kt)
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var mToastToShow: Toast
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
}
fun showToast(view: View) {
val toastDurationInMilliSeconds = 10000L // 10 giây
mToastToShow = Toast.makeText(this, "Hello world, I am a long toast.", Toast.LENGTH_LONG)
val toastCountDown = object : CountDownTimer(toastDurationInMilliSeconds, 5000) {
override fun onTick(millisUntilFinished: Long) {
mToastToShow.show()
}
override fun onFinish() {
mToastToShow.cancel()
}
}
mToastToShow.show()
toastCountDown.start()
}
}
Bước 4: Cấu hình Manifest (androidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Nhấn nút "Display Toast" sẽ hiển thị Toast trong 10 giây (lặp lại mỗi 5 giây).
Giải thích cơ chế
Toast không cho phép tùy chỉnh duration trực tiếp. Kỹ thuật này:
- Tạo một Toast với
LENGTH_LONG - Dùng
CountDownTimervới tổng thời gian mong muốn (10000ms) - Mỗi
onTick(5000ms) gọishow()lại để reset timer hiển thị - Khi
onFinish, gọicancel()để dừng
Cách tiếp cận hiện đại hơn: Snackbar
Để hiển thị thông báo lâu hơn với action, nên dùng Snackbar (Material Design):
import com.google.android.material.snackbar.Snackbar
fun showLongSnackbar(view: View) {
Snackbar.make(view, "This is a long message", Snackbar.LENGTH_INDEFINITE)
.setAction("DISMISS") { }
.setDuration(10000) // Chỉ hoạt động với LENGTH_INDEFINITE + setDuration (API 30+)
.show()
}
Lưu ý
- Toast lặp lại có thể gây nhấp nháy (flicker) giữa các lần show.
- Không nên lạm dụng Toast dài - UX khuyến nghị dùng Snackbar, Dialog, hoặc in-app banner.
- Trên Android 12+, Toast bị giới hạn hiển thị từ background.