Categories
Flex

Toast on AIR for Android



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);
		}
	}
}

Download

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 ! 🙂

P2P Apps for Mobile Devices
Mime types for a Flex Server

By Immanuel Noel

A techie at heart. Works with Adobe at Bangalore, India. Currently do DevOps. Been a part of the ColdFusion Engineering, Flash Runtime Engineering, Flash Builder Engineering teams in the past. Was a Flash Platform Evangelist, evangelizing the Adobe Flex platform. Spoke at numerous ColdFusion / Flash and Flex tech conferences. This blog is a collection of some of my strides with technology.

More on me on the home page

4 replies on “Toast on AIR for Android”

Leave a Reply

Your email address will not be published. Required fields are marked *