Monday, 9 September 2013

Simple Toast

A Toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

A Toast is commonly used for notifying the user for short interval of time.it is easy to use, and best thing to notify the user without interrupting the current Activity.A Toast class has two Constants (Toast.LENGTH_SHORT  and  Toast.LENGTH_LONG ).Basically these are the time outs used for the toast visibility time on the screen.The Toast is the most common method used by Android developers to notify their Application users about something they want to tell in short interval of time.

Types of Toast Simple

Create new Android Project
Project Name: typesoftoasts
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Types Of Toasts
Package Name: com.shaikhhamadali.blogspot.typesoftoast
Create layout file: activity_toast_types

  1.create layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ToastTypes" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world" />

</RelativeLayout>

2. code of activity:

package com.shaikhhamadali.blogspot.typesoftoast;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

public class ToastTypes extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_toast_types);
  //create instance of toast make text (context,text,time);
  Toast t=Toast.makeText(getApplicationContext(), "welcome to hamad's blog", Toast.LENGTH_SHORT);
  //show toast
  t.show();
       }
}

also can use toast like this:

Toast.makeText(getApplicationContext(), "welcome to hamad's blog", Toast.LENGTH_SHORT).show();

  3. note that:
  • you can use this on button on click,on action_down,on the fly etc.
  • Toast.LENGTH_SHORT duration is about 2 seconds or 2000 mili seconds.
  • Toast.LENGTH_LONG duration is about 3.5 seconds and 3500 mili seconds.

  4. conclusion:

  • Some information about basic/simple Toast.
  • Learn more about Toast

  5. About the post:

  • you can customise the Toast, its screen location, Layout etc, see Custom Toast.
  • The code seems to explain itself due to comments, and is very easy to understand but feel free to ask queries regarding this.
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!

  6. Source Code:
        you can download the source code here

Cheers,

Hamad Ali Shaikh