0% found this document useful (0 votes)
49 views4 pages

Damon: BY - JANUARY 18, 2010

This document discusses how to create and display toast notifications in Android applications. It provides code to create a toast object from a text string, and shows how to call the toast's show() method to display it. It then shares a full Android application example that uses an EditText field and Button to allow the user to enter text and display it as a toast notification when the button is clicked. The class implements the OnClickListener interface to execute the toast display code when the button is pressed.

Uploaded by

Ajaypal Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Damon: BY - JANUARY 18, 2010

This document discusses how to create and display toast notifications in Android applications. It provides code to create a toast object from a text string, and shows how to call the toast's show() method to display it. It then shares a full Android application example that uses an EditText field and Button to allow the user to enter text and display it as a toast notification when the button is clicked. The class implements the OnClickListener interface to execute the toast display code when the button is pressed.

Uploaded by

Ajaypal Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

BY DAMON | JANUARY 18, 2010

To celebrate my new purchase for a Google Nexus One Ive started looking into Android
development.
Android is built in Java and since I studied Java at University I decided to have a little play.
From experience of using Android there are times when on screen messages pop up for
whatever reason. In Android these are called Toast.
Ive included a full class which explains how these Toast Objects are created. If youre just
interested in getting a Toast message to appear then the following code will work for you.
// Create a piece of toast.
Toast pieceToast = Toast.makeText(getApplicationContext(), toastText.getText(),
Toast.LENGTH_SHORT);

// Show the toast.


pieceToast.show();

The following class is a complete application albeit a very small one. Its purpose is to display
a text field which allows the user to enter text and button which will show a Toast message.
/**
* This is a Android example to which shows how to display a piece of toast.
* Makes use of OnClickListener interface
*
* @author Damon Skelhorn
*/

package com.damon86.toastExample;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ToastExample extends Activity implements OnClickListener {


/** Called when the activity is first created. */

// The text and button used on the display.


EditText toastText = null;
Button showToast = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create a new Layout.


LinearLayout rootLayout = new LinearLayout(getApplicationContext());

// Create textfield
toastText = new EditText(getApplicationContext());
toastText.setText("Hello Toast!");

// Create button and set OnClickListener


showToast = new Button(getApplicationContext());
showToast.setText("Show Toast");
showToast.setOnClickListener(this);

// Add components to the Layout.


rootLayout.addView(toastText);
rootLayout.addView(showToast);

// Add the layout as the content view for the application.


setContentView(rootLayout);
}

/**
* Implemented method - onClick.
* Show the user a piece of toast.
*/
@Override
public void onClick(View v) {

// Create a piece of toast.


Toast pieceToast = Toast.makeText(getApplicationContext(),
toastText.getText(), Toast.LENGTH_SHORT);

// Show the toast.


pieceToast.show();
}
}

The class implements the OnClickListener Interface. For those who do not know what an
interface is, its a absract type which contains a set of empty methods know as method
signatures. When a class implements a interface, Java expects your class to contain these
methods. You can then write any code within these methods.
To make sure the button executes the code contained in the onClick method, the
onClickListener is set on the button by using the following code;
showToast.setOnClickListener(this);

Download complete ToastExample Project

You might also like