• Privacy Policy
  • Contact us
  • Register
  • Log In
  • Facebook
  • Pinterest
  • YouTube
  • WhatsUp
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme

Android Widgets

  • Android DatePicker Example And Tutorial
  • Android SeekBar Exmple and Tutorial
  • Android WebView Example and Tutorial
  • Android RatingBar Tutorial and Example
  • Android ListView Tutorial and Example
  • Android Spinner Tutorial and Examples
  • Android Alert Dialog Tutorial and Example
  • Android Radio Button Tutorials
  • Android CheckBox Tutorial
  • Android Toggle Buttons
  • Android Toast Example
  • Types of Android Button and Examples
  • About Android Studio Widgets

About Android

  • How to Setup Different Android Emulators?
  • What is Android Architecture?
  • History of Android Studio and Version
  • What is Android Studio and Installation
  • Home
  • Courses
  • Android Widgets
  • Android Toast Example

Android Toast Example

Download Premium Version Free
Android Toast Example - Beginners totorials free

Android toast is used for displaying message or information for short time, Toast has a message in form of string to display quickly. You can display the information of message to user for short time using any other method than the question is arise that why we use Toast? So the answer is simple Toast less space of diaplay and less memory to show the information faster and the remaining activity will remain interactive means we can use the app while toast is displaying.

Toast Object:

To make a toast it will require 3 parameters such as:

  • Application Context
  • String or Message
  • Time Period

Application Context you can initialize like this way:

Context context = getApplicationContext();

And String you can Defined as :

private String message = "This is my message";

And for Toast Time Period you can use predefined time Constants which is:

LENGTH_LONG
LENGTH_SHORT

So using all these parameters you can show toast like this way:

Toast.makeText(context, text, duration).show(); The final Toast Code will be:

Toast.makeText(context, message,Toast.LENGTH_SHORT).show();
Android Toast Example
Toast Example

If you want to show multiple toast then you can create a custom Toast Method to display the message like this way:

Create a method:

public void showToast(String info){

}

Now costomize toast :

public void showToast(String info){
Toast.makeText(context, info,Toast.LENGTH_SHORT).show();
}

Now when you want to show toast just call this method like this way:

showToast("This is my message");

Toast Background Color:

To change the color of Android Toast you have to set background color to toast view programmatically like the below code:

Toast toast = Toast.makeText(context, info,Toast.LENGTH_SHORT);
View tv = toast.getView();  // get the toast view to set the color on view
tv.setBackgroundColor(Color.GREEN);  // set color here
toast.show();  // now show toast with green background
Android Toast Example Displaying message
Custom Toat with Green Background

You can also set padding to toast you noticed that it doesn’t has space from its start point and end point.

Toast with Gradient Background:

Toast toast = Toast.makeText(this, "This is my message", Toast.LENGTH_LONG);
View tv = toast.getView();    // get toast view to set properties to toast
tv.setPadding(20,15,20,15);  // padding top, left, right, bottom
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  tv.setBackground(getDrawable(R.drawable.gradeint));  // gradient background using gradient drawable xml file
}
toast.show(); // show gradient toast
Android Toast Example Displaying message
Toast with Gradient Background and With Padding
Android Toast Example Displaying message
Gradient Toast with Round Corner

Toast with Gradient Background and Icon:

if you want o set icon to tast then you can do this using the below method of code:

First of all create a view for toast than call textview to set text and icon together after that set the drawable icon and text to it. Finally set gradient xml file to it and call show method to diaplay the customized toast. Its Done…

Toast toast = Toast.makeText(this, "This is my message", Toast.LENGTH_LONG);
View tv = toast.getView();    // get toast view to set properties to toast
tv.setPadding(20,15,20,15);  // padding top, left, right, bottom
TextView toastMessage = (TextView) tv.findViewById(android.R.id.message); // call textview to set icon and text together
toastMessage.setTextSize(25);  // text size
toastMessage.setTextColor(Color.WHITE); // text color
toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_play, 0, 0, 0); // icon ic_play
toastMessage.setGravity(Gravity.CENTER); // set the toast in center
toastMessage.setCompoundDrawablePadding(16); //padding between text and icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  tv.setBackground(getDrawable(R.drawable.gradeint));  // toast gradient background using drawable xml file
}
toast.show();  // show toast
Android Toast Example Displaying message
Toast with Text and Icon

If you like this tutorial please share it with your friends and if you have any doubts comment below.

Android Toast
What are your Feelings
What are your Feelings
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Updated on April 23, 2021
Types of Android Button and ExamplesAndroid Toggle Buttons

Leave a Comment X

You must be logged in to post a comment.

Copyright © 2023.Nuled Source Code.

  • Native Android
  • Paid Projects
  • Flutter
  • My Tickets
  • Register
  • Log In
  • Register
Loading...
 

Loading Comments...
 

You must be logged in to post a comment.

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.