What is Toast?
A toast is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user’s current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events. Read more at Android developer site
What are we going to develop?
We are going to develop a simple android activity which will have one button at the center of the screen. On clicking the button, custom toast message will be appeared.
- Layout creation:Create new android project [File >> New >> Android Project] with project name CustomToastExample
- Click next and select target android device version [I chose version 2.2]
- Click next and enter package name – ‘com.prgguru.android’
- Click finish
We need to create two layouts:
- Layout of the application – main.xml
- Layout of the custom toast message – toast_custom_layout.xml
Application layout – main.xml:
Open main.xml under /res/layout and add replace it with the below XML.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:android1 = "http://schemas.android.com/apk/res/android" android:id = "@+id/main" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "horizontal" android:padding = "10dp" > < Button android1:id = "@+id/button1" android1:layout_width = "match_parent" android1:layout_height = "wrap_content" android1:layout_gravity = "center" android1:text = "Show custom toast" /> </ LinearLayout > |
Custom toast layout – toast_custom_layout.xml:
Create new layout XML under /res/layout with name toast_custom_layout.xml and the below XML.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:android1 = "http://schemas.android.com/apk/res/android" android:id = "@+id/toast_layout_root" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:background = "#DAAA" android:orientation = "horizontal" android:padding = "10dp" > < ImageView android1:id = "@+id/imageView1" android1:layout_width = "wrap_content" android1:layout_height = "match_parent" android1:src = "@drawable/red_heart" /> < TextView android1:id = "@+id/textView1" android1:layout_width = "wrap_content" android1:layout_height = "match_parent" android1:text = "This is custom Toast message" android1:textAppearance = "?android:attr/textAppearanceMedium" /> </ LinearLayout > |
We need to inflate the custom toast xml into Toast view:
Following snippet does the job of creating and showing custom toast.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| //Retrieve the layout inflator LayoutInflater inflater = getLayoutInflater(); //Assign the custom layout to view //Parameter 1 - Custom layout XML //Parameter 2 - Custom layout ID present in linearlayout tag of XML View layout = inflater.inflate(R.layout.toast_custom_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); //Return the application context Toast toast = new Toast(getApplicationContext()); //Set toast gravity to bottom toast.setGravity(Gravity.BOTTOM, 0 , 0 ); //Set toast duration toast.setDuration(Toast.LENGTH_LONG); //Set the custom layout to Toast toast.setView(layout); //Display toast toast.show(); |
CustomToastExampleActivity.java
Here is the Activity code of this application.
Here is the Activity code of this application.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| package com.prgguru.android; 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.Toast; public class CustomToastExampleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); //Find the button Button btn = (Button) findViewById(R.id.button1); //Create onclick listener class btn.setOnClickListener( new View.OnClickListener() { //When button is clicked, toast will be showed public void onClick(View v) { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_custom_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.BOTTOM, 0 , 0 ); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); } }); } } |
Demo:
Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screen:
Run click on the project >> Run as >> Android application
You could see following screen:
Reference :
http://android.programmerguru.com/android-custom-toast-example/
No comments:
Post a Comment