
Toast: Is used to display information for the short period of time for the user.
Custom Toast: Toast can be customize like setting the image inside the toast, it can be used based on situations like (No Internet connection, Success messages …)
Creating Layout:
The main layout for our project is “activity_main” with two buttons and custom layout to display toast
activity_main.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >
<Button
android:id=”@+id/btnToast”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Toast” />
<Button
android:id=”@+id/btnCustomToast”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Custom Toast” />
</LinearLayout>
custom_toast.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/toast_layout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#fff”
android:orientation=”horizontal”
android:padding=”5dp” >
<ImageView
android:id=”@+id/image”
android:src=”@drawable/hoffen”
android:layout_width=”wrap_content”
android:layout_height=”fill_parent”
android:layout_marginRight=”5dp” />
<TextView
android:id=”@+id/text”
android:layout_width=”wrap_content”
android:layout_height=”fill_parent”
android:textColor=”#000″/>
</LinearLayout>
Creating Activity:
In the MainActivity “Toast.makeText” is used to show the Toast which has three arguments. The first argument defines the current Activity. The second argument is the message to be displayed in toast. The third argument “Toast.LENGTH_SHORT” and “Toast.LENGTH_LONG” is used to show toast for a short period and long period of time.
package com.example.customtoast;
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.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button noramalToast;
Button customToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noramalToast = (Button) findViewById(R.id.btnToast);
customToast = (Button) findViewById(R.id.btnCustomToast);
noramalToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, “Normal Toast”, Toast.LENGTH_LONG).show();
}
});
customToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(“Custom Toast with image”); // Message shown
// in Custom
// Toast
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);
toast.show();
}
});
}
}
CustomtoastManifest :
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.customtoast”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”21″ />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
</manifest>