[Android] Chương 1.9 : Thông báo Toast.

TỔNG QUAN

Là một dạng popup message hiển thị trên màn hình.
Tùy thuộc vào message mà popup toast sẽ có kích cỡ tương ứng khi hiển thị.
Toast    tự động hiển thị trong thời gian được cho trong duration.
Toast  trong lúc hiển thị, không ảnh hưởng đến activity khác và không bắt các sự kiện của người dùng.

CÁCH SỬ DỤNG
Để hiển thị một đoạn Toast, đơn giản như sau:
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast
.show();
Trong đó:

context: application context
message: Nội dung message
duration: Thời gian hiển thị
.show():  Hàm hiển thị Toast

Toast  có thể được tùy biến giao diện:

LayoutInflater inflater = getLayoutInflater();

  View layout = inflater.inflate(R.layout.custom_toast,

                                 (ViewGroup) findViewById(R.id.toast_layout_root));

  

  TextView text = (TextView) layout.findViewById(R.id.text);

  text.setText("This is a custom toast");

  

  Toast toast = new Toast(getApplicationContext());

  toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

  toast.setDuration(Toast.LENGTH_LONG);

  toast.setView(layout);

  toast.show();