Package:- android.widget
Documentation:- http://developer.android.com/reference/ ... Toast.html
A Toast is a view containig a quick little message for the user. The Toast class helps you to create and show these messages.
Example:- to display messages like -"your settings have been saved"
Code Snippets:-
Using java Syntax Highlighting
- package com.vikrant.ToastExample;
- import android.app.Activity;
- import android.widget.Button;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- /**
- * When you push the button on this Activity, it creates a Toast object and using the Toast method.
- *Toast#makeText(android.content.Context,java.lang.CharSequence,int)
- *Toast#LENGTH_SHORT
- *Toast#LENGTH_LONG
- */
- public class ToastExample extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button;
- // short notification
- button = (Button) findViewById(R.id.button_short);
- button.setOnClickListener(new Button.OnClickListener()
- {
- public void onClick(View v)
- {
- // Note that we create the Toast object and call the show() method
- // on it all on one line. Most uses look like this, but there
- // are other methods on Toast that you can call to configure how
- // it appears.
- //
- Toast t = new Toast(ToastExample.this);
- t.makeText(ToastExample.this, "Volume set to Max", Toast.LENGTH_SHORT);
- t.show();
- }
- });
- // long notification
- // The only difference here is that the notification stays up longer.
- // You might want to use this if there is more text that they're going to read.
- button = (Button) findViewById(R.id.button_long);
- button.setOnClickListener(new Button.OnClickListener()
- {
- public void onClick(View v)
- {
- Toast.makeText(ToastExample.this," Your settings have been saved",Toast.LENGTH_LONG).show();
- }
- });
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button android:id="@+id/button_short"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Volume Set"
- android:layout_x="50px"
- android:layout_y="200px" />
- <Button android:id="@+id/button_long"
- android:text="Save Settings"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_x="150px"
- android:layout_y="200px" />
- </AbsoluteLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Useful Methods:-
void cancel()
int getDuration()
int getGravity();
static Toast makeText(Context c, CharSequnce Text_to_be_shown,int duration)
void show();
void setDuration();

...


