Alerts
Alerts provide a quick message to the user outside of the application’s main UI. It can be in an overlay window such as a Toast or AlertDialog box. It can also be in the notification bar at the top of the screen.The Toast alert provides a printed message to the screen with a single line of code.There is no need to work with the layout files. For this reason, it is also a handy debug tool, equivalent to the printf statement in C programs.
Using Toast to Show a Brief Message on the Screen
The Toast method has been introduced in the previous chapter in a compact form:
Toast.makeText(this, "text", Toast.LENGTH_SHORT).show();
It can also be written as a multiline command:
Toast tst = Toast.makeText(this, "text", Toast.LENGTH_SHORT);
tst.show();
ToastExample.java Code
package
android.example.toast;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.Toast;
public class ToastExample extends Activity {
    @Override
    public void onCreate(Bundle
savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button;
        // short notification
        button = (Button) findViewById(R.id.short_notify);
        button.setOnClickListener(new
Button.OnClickListener() {
            public void onClick(View v)
{
                Toast.makeText(ToastExample.this, "This is
Short Notification",
                    Toast.LENGTH_SHORT).show();
            }
        });
        // long notification
        button = (Button) findViewById(R.id.long_notify);
        button.setOnClickListener(new
Button.OnClickListener() {
            public void onClick(View v)
{
                Toast.makeText(ToastExample.this, "This is
Long Time Notification",
                    Toast.LENGTH_LONG).show();
            }
        });     
    }
}
main.xml Code
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#225588"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Toast
Example"
        android:textColor="#FF8800"
        android:textStyle="bold"
        android:textSize="36dp"
/>
    <Button
        android:id="@+id/short_notify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Short Toast
Notify" />
    <Button
        android:id="@+id/long_notify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Long Toast
Notify" />
</LinearLayout>
Download Android Toast Example code
More Info
Android Notification-->
Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android
สอนเขียน Android  สอนเขียนโปรแกรม Android
ติดต่อ amphancm@gmail.com


