Toast on AIR for Android
by Immanuel Noel on Jan.03, 2011, under Flex
Here’s a component which behaves as a ‘Toast’ for AIR applications running on Android. The component can essentially be used across any Flex application, since it is an ActionScript class.
For those of you who don’t know, Toast is the small transparent alert that pop’s up (and doesn’t block your UI) when u set an alarm on Android phones !

Toast on AIR for Android
In the pic above, the text ‘Enter Required Fields’ appears in a ‘Toast’
The Toast, is a ‘component’ of your application, and not a feature integrated into AIR (for Android), which makes the toast persistent only across your own application unlike the ‘Toast’ found under the Android native application development environment.
Usage :
// Toast Usage new Toast(this, "Enter Required Fields", 3000); /* Parameters used, Context (Parent) : this Text to display : "Enter Required Fields" TimeOut : 3000 */
Optional parameters include,
timeOut -> Display Duration in milliseconds; Type:Number; Default:3000
width -> Toast Width; Type:Number; Default:290
height -> Toast Height; Type:Number; Default:40
color -> Toast background color; Type:String; Default:’#454545′
x -> X Position. Auto-calculated if not specified; Type:Number; Default:NaN
y -> Y Position. Auto-calculated if not specified; Type:Number; Default:NaN
Source :
package immy
{
import flash.events.TimerEvent;
import flash.utils.Timer;
import spark.components.BorderContainer;
import spark.components.Label;
import spark.components.View;
import spark.layouts.VerticalLayout;
public class Toast extends BorderContainer
{
/**
* CONSTRUCTER
*/
public function Toast(context:View, textToDisplay:String, timeOut:Number = 3000, width:Number = 290, height:Number = 40, color:String = '#454545', x:Number = NaN, y:Number = NaN):void
{
super();
// TIMER TO 'REMOVE' TOAST AFTER THE SPECIFIED 'TIMEOUT'
var timer:Timer = new Timer(timeOut, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(event:TimerEvent):void{
context.removeElementAt(context.numElements - 1);
});
timer.start();
createUI(textToDisplay, color, width, height);
// TOAST POSITION
positionToast(context);
}
/**
* CREATE TOAST UI COMPONENTS
*/
private function createUI(textToDisplay:String, color:String, width:Number, height:Number):void
{
selfProperties(color, width, height);
addText(textToDisplay);
}
/**
* ASSIGNING ATTRIBUTES TO THE BASE CONTAINER
*/
private function selfProperties(color:String, width:Number, height:Number):void
{
this.width=width;
this.height=height;
this.setStyle('backgroundAlpha','0.75');
this.setStyle('backgroundColor',color);
this.setStyle('borderColor','#242424');
this.setStyle('borderWeight','2');
this.setStyle('cornerRadius','7');
var vl:VerticalLayout = new VerticalLayout();
vl.horizontalAlign = "center";
vl.verticalAlign = "middle";
this.layout = vl;
}
/**
* CREATE LABEL
*/
private function addText(textToDisplay:String):void
{
var lbl:Label = new Label();
lbl.setStyle('color','#EDEDED');
lbl.setStyle('fontSize','18');
lbl.text = textToDisplay;
lbl.setStyle('textAlign', 'center');
lbl.width = this.width;
this.addElement(lbl);
}
/**
* POSITIONING THE TOAST
*/
private function positionToast(context:View):void
{
// POSITION TOAST
if(Number(x) && Number(y))
{
this.x = x;
this.y = y;
}
else
{
if(Number(x) && !Number(y))
{
throw new Error("Toast : 'Y' attribute also needs to be specified");
}
this.x = (context.width / 2) - (this.width / 2);
this.y = (context.height * 3 / 4) - (this.height / 2);
}
// IF PREVIOUSLY ADDED COMPONENT IS A TOAST, RE-ORDER TO MANAGE TIMER
if(context.getElementAt(context.numElements - 1) is Toast)
context.addElementAt(this, context.numElements - 1);
else
context.addElement(this);
}
}
}
A future release of AIR for Android will probably bring in a ‘Toast’ to substitute the one made available by the Android SDK. Until then, here’s something to make use of !
2 Comments for this entry
1 Trackback or Pingback for this entry
-
Tweets that mention Toast on AIR for Android « On AIR with Flex -- Topsy.com
January 3rd, 2011 on 9:23 PM[...] This post was mentioned on Twitter by Sujit Reddy G. Sujit Reddy G said: RT @immanuelnoel: Blogged : Toast on AIR for Android http://tinyurl.com/22nsbdx [...]



January 4th, 2011 on 12:34 AM
Nice one
And if you look for a way to show notifications (even complex ones) in Flex Applications, here is a lib that i created which comes handy:
http://code.google.com/p/flex-toasterlib/
Thanks for sharing,
Fabien
January 4th, 2011 on 10:51 AM
Thats a good one Fabien. Will try it out