Jun 3, 2011

Android : Creating notification using Toast


What is toast notification ?
Toast notification is a message popup in android application which popup on application window and automatically fades in. It fills only required area on screen and user's activity and interaction are remains enable, it mean during popup of toast message user can do interaction's like entering text, selection of controls ect.
For this android lib has one class android.widget.Toast, this class has methods to create toast, set their location , duration and message.
For example, simple text message base toast.
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;
 
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//call to show toast
ShowToast("Test Toast");
}
private void ShowToast(String msg )
{
Toast toast = Toast.makeText(Main.this, msg,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
}
}
n above example we have created a toast object using makeText static method of class which returns toast object.
makeText has two overloads as given below
//Make a standard toast that just contains a text 
view with the text from a resource.
static Toast makeText(Context context, 
int resId, int duration)
 
//Make a standard toast that just contains a text from string.
static Toast makeText(Context context, 
CharSequence text, int duration)
 
For setting location of toast notification there is method setGravity. it Set the location at which the notification should appear on the screen.
public void setGravity (int gravity, int xOffset, int yOffset) 
First parameter is gravity and different gravity constants are define in class Gravity. You can see different  Gravity constants and their description using Eclipse IDE.
Other useful methods are
setDuration(int duration) and setMargin(float horizontalMargin, float verticalMargin)
setDuration is used to set duration of toast display and it can be short or long while setMargin is used to specify vertical and horizontal margin.
show() method is used to show the toast.

Custom View Toast:

There is an important method setView(View view) and this is used to show custom view as toast.
This is used when you want to show custom view as toast notification like some colorful text and image and other.
Here is one example
package bimbim.in;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
Button mBtn, mBtn2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtn = (Button) findViewById(R.id.btn1);
mBtn2 = (Button) findViewById(R.id.btn2);
mBtn.setOnClickListener(this);
mBtn2.setOnClickListener(this);
toast = new Toast(getApplicationContext());
}
@Override
public void onClick(View v) {
if (v == mBtn)
ShowCustomeViewToast();
else if (v == mBtn2)
toast.cancel();
}
/*
* private void ShowToast(String msg) { Toast toast =
* Toast.makeText(Main.this, msg, Toast.LENGTH_LONG);
* toast.setGravity(Gravity.TOP, 0, 0); toast.show(); }
*/
Toast toast;
private void ShowCustomeViewToast() {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.customeview,
(ViewGroup) findViewById(R.id.root_layout));
ImageView image = (ImageView) layout.findViewById(R.id.image1);
image.setImageResource(R.drawable.icon);
TextView text = (TextView) layout.findViewById(R.id.text1);
text.setText("Image and Text toast");
toast.setGravity(Gravity.TOP, 50, 50);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
}
Show Custom Toast
Close Custom Toast


2 comments: