Plain Toast
Custom Toast
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
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:
- activity_toast_types:
<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:id="@+id/tVHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Plain and Custom Toast demo"
android:textStyle="bold|italic"
android:textColor="#6A96B0"
android:textSize="50dp" />
<Button
android:id="@+id/btnPlain"
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="48dp"
android:layout_marginTop="74dp"
android:text="Plain Toast"
android:textSize="40dp" />
<Button
android:id="@+id/btnCustom"
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_alignRight="@+id/btnPlain"
android:layout_below="@+id/button1"
android:layout_marginTop="200dp"
android:text="Custom Toast"
android:textSize="40dp" />
</RelativeLayout>
- customized_toast:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customizedToast"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gradiant"
android:layout_margin="25dip">
<LinearLayout
android:layout_margin="5dip"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/celebration"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:paddingLeft="20dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tVToast"
android:textColor="#ffffff"
android:textSize="25sp"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
- gradiant:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="linear"
android:centerX="50%"
android:startColor="#FF003333"
android:centerColor="#FF05C1FF"
android:endColor="#FF003333"
android:angle="270"/>
<!-- <gradient
android:centerColor="#FF05C1FF"
android:centerX="50%"
android:centerY="50%"
android:endColor="#FF003333"
android:gradientRadius="50"
android:startColor="#FF003333"
android:type="radial" /> -->
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>
2. code of activity:
package com.shaikhhamadali.blogspot.typesoftoast;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ToastTypes extends Activity {
Button btnPlain,btnCustom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_types);
btnPlain=(Button)findViewById(R.id.btnPlain);
btnPlain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//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();
}
});
btnCustom=(Button)findViewById(R.id.btnCustom);
btnCustom.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//create instance of inflater
LayoutInflater inflater = getLayoutInflater();
//create instance of View and inflate out toast layout in view
View layout = inflater.inflate(R.layout.customized_toast, (ViewGroup) findViewById(R.id.customizedToast));
//set your desired text to out toast text view
((TextView) layout.findViewById(R.id.tVToast)).setText("welcome to hamad's blog.");
//create instance of Toast
Toast toast = new Toast(getApplicationContext());
//set time duration;
toast.setDuration(Toast.LENGTH_SHORT);
//set view of toast that is custom toast layout
toast.setView(layout);
//show toast
toast.show();
}
});
}
}
also can use toast like this (for plain Toast):
Toast.makeText(getApplicationContext(), "welcome to hamad's blog", Toast.LENGTH_SHORT).show();
3. note that:
- you can use these 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.
- you can set the typeface of your toast text view.
- you can use image as background.
- you can apply custom shape to your toast.
- to generate custom shape online i have added new link in useful links named as "Gradient Shape Generator Online" (android patterns).
4. conclusion:
- Some information about basic/simple Toast.
- Some information about Custom Toast.
- Know how to create your custom generated toast.
- know how to show simple toast in different ways.
5. About the post:
- The code seems to explain itself due to comments, if you have any question you can ask too!
- 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,
you can download the source code here
Hamad Ali Shaikh