Code2care : TechZone Apps & Tutorials



Toast messages in Android Programming are used to display Notification messages on Activity Screen as popup. This message is displayed as Text that fades-in when displayed and after the duration that has been set is over fades-out.

If you want to learn more about the basics of toast messages in android refer : Toast Android Introduction

By default a Toast message is displays only text and that to at the bottom of the Activity screen and normally in gray background with border-radius.

We can customize the Toast message by changing its default implementation defining your own toast xml file.



Custom Toast Example 1



Lets create a custom Toast message with a image and 2 TextViews,

custom_toast_android.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_custom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88FFFFCC"
android:orientation="vertical"
android:padding="5dp" >

<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sub text for toast message"
android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>
</LinearLayout> </LinearLayout>



MainActivity.java
package com.example.customtoastexample;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Default toast Example
		Toast toast = Toast.makeText(this,
				"Hello, this is a android toast message!", Toast.LENGTH_LONG);
		toast.show();
		
		
		
		//1. Create object of LayoutInflater Class
		LayoutInflater layoutInflater = getLayoutInflater();
		
        //2. Inflate the layoutInflater
        View layout = layoutInflater.inflate(R.layout.custom_toast_android,
                                       (ViewGroup) findViewById(R.id.toast_custom));

        //Map both the textviews defined
        TextView toastHeader = (TextView) layout.findViewById(R.id.textView1);
        TextView toastContextText = (TextView) layout.findViewById(R.id.textView2);
        
        
        
        //Set text to both the textviews
        toastHeader.setText("Custom Toast");
        toastContextText.setText("This is an example of custom toast.");
        
        //Create toast object using getApplicationContext()
        Toast customToast = new Toast(getApplicationContext());
        
        //Set duration for the toast
        customToast.setDuration(Toast.LENGTH_LONG);
        customToast.setView(layout);
        
       //Display the toast
        customToast.show();

	}	

}


Custom toast example 1




Custom Toast Example 2



Lets now create the a toast that is displayed at the center of the Activity screen with background color black and text color as white. We can display the toast at center using setGravity() method with Gravity.

package com.example.toastexample;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);


		//1. Create object of LayoutInflater Class
		LayoutInflater layoutInflater = getLayoutInflater();
		
        //2. Inflate the layoutInflater
        View layout = layoutInflater.inflate(R.layout.custom_toast_android,
                                       (ViewGroup) findViewById(R.id.toast_custom));

        //3. Map both the textview defined
        TextView toastText = (TextView) layout.findViewById(R.id.textView1);
        
        
        
        //4. Set text to both the textviews
        toastText.setText("Custom Toast Message!");
        
        //5. Create toast object using getApplicationContext()
        Toast customToast = new Toast(getApplicationContext());
        
        //6. Set duration for the toast
        customToast.setDuration(Toast.LENGTH_LONG);
        
        //7. Set the view
        customToast.setView(layout);
        
        //8. Setting Gravity of toast to center of screen 
        customToast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);
        
       //9. Display the toast
        customToast.show();

        
        

	}
	
	
	

}


<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/toast_custom"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/shape"
   android:layout_margin="10dp"
   android:orientation="vertical"
   android:padding="5dp" >

<TextView
   android:id="@+id/textView1"
   android:padding="10dp"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#ffffff"
   android:textSize="20dp"
   android:text=""
   />

</LinearLayout>


shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle" >

   <corners android:radius="14dp" />
   <solid android:color="#595959" />

   <padding
   android:bottom="0dp"
   android:left="0dp"
   android:right="0dp"
   android:top="0dp" />

<size
   android:height="60dp"
   android:width="270dp" />

<stroke
   android:width="3dp"
   android:color="#44050117" />

</shape>


Top











Posts related to android.


1. Share image and text Twitter using your Android Application Programatically

2. The Android Virtual Device 'myEmulator' is currently running an emulator and cannot be deleted.

3. Make Android View Scrollable both Horizontally and Vertically

4. Can we move apps like WhatsApp, Facebook to external MicroSD card

5. Parsing Data for android-21 failed unsupported major.minor version 51.0

6. Android : Prevent App for rotation landscape or portrait

7. ADT quit unexpectedly error on Mac OSX Android Eclipse SDK

8. Android-Failed to install apk on device EOF Timeout Error

9. How to change Android EditText Cursor Color

10. JavaDocs for Android SDK Eclipse IDE

11. Two Buttons next to each other in Android Layout

12. Change Title text for Android Activity using java code

13. How to add Newline to text in Android TextView

14. Android : Unable to load VM from snapshot : Mac OS X Error

15. INVALID FILE NAME: MUST CONTAIN ONLY [a-z0-9_.] Android Eclipse Error

16. Hide Tittle bar and Navigation bar from android activity

17. appcompat_v7 errors after updates to API level 21 Material Theme

18. How to Copy Text to Android Clipboard Programatically ClipboardManager (Both Old and New SDK)

19. [Solution] Running Android Lint has encountered a problem NullPointerException Error

20. Remove Trailing zeros BigDecimal Java

    more...






Popular tags
android
x 175
eclipse
x 29
notepadplusplus
x 20
macosx
x 14
sharepoint
x 14
html
x 14
mac
x 13
androidstudio
x 11




1000+ C Programs     PHP Tutorial     JSON Tutorial     Swift Tutorial     India Pinocdes     About Us     Privacy Policy


Code2care © 2012-17