How To Use/Display Toast in Android Studio [All Customizations]

How To Use/Display Toast in Android Studio [With All Customizations]

It's way easier to Toast a message in Android Studio than you think. We use Toast to show a message to a user when a task starts or task completes or a result of an action or maybe for other respects. Toast is a small popup to provide feedback/information about any action, can be shown for a short or long time. This article also includes most of the customization of Toast that you may need to make it look more professional and unique.

Before we get started we need These:
  • Android Studio
  • An existing App project with an Activity

So, we are all set. Let's Get Started!

Toasting A Message In Android Studio: 

Before we can toast a message, we will need to import a class along with other classes to use the Toast widget:
import android.widget.Toast;
Now to toast a message, use this line:
Toast.makeText(getApplicationContext(),  "YOUR_MESSAGE_HERE", Toast.LENGTH_SHORT).show();

Primary Toast Customizations:

  1. Just change 'YOUR_MESSAGE_HERE' with your message (Note: your message should be inside "")
  2. There are two default options to set the duration of the toast. They are 'LENGTH_SHORT' and 'LENGTH_LONG'.
  3. If you want to toast the message for a long time, then change 'LENGTH_SHORT' with 'LENGHT_LONG'. Additionally, you can use your duration (in Milliseconds).
  4. You can also use your activity name to get the context. To do so, you have to replace 'getApplicationContext()' with 'YOUR_ACTIVITY_NAME.this'

Extra Toast Customizations:

You may use these lines if you want to use extra toast configurations:
Toast toast = Toast.makeText(getApplicationContext(),  "YOUR_MESSAGE_HERE", Toast.LENGTH_SHORT);
//all extra customization codes should be placed here
toast.show();

Now before 'toast.show();' you can add one or more of these codes to customize the Toast:

Changing the Toast Position With/Without Margin:

If you want to show the Toast in a different position then you may add this line:
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 10, 10);

Change 'TOP' and 'RIGHT' according to your desired one. You can change the first '10' to set the width between the Toast container and the edge [Horizontal Margin] (value in px). The second '10' to set the height between the Toast container and the edge [Vertical Margin] (value in px). If you don't want any margin, then use '0, 0' instead

Adding Margin Without Changing Toast Position:

If you don't want to change the Toast position, only want to set margins, then use this:
toast.setMargin(10, 10);

The configurations are the same as described previously for the margins

Changing Toast Text Color:

To change the Toast message color, first of all, we will need to get the TextView from the Toast class. Just add this line first without changing the code to do that:
TextView toastText = (TextView) toast.getView().findViewById(android.R.id.message);

You can change the 'toastText' with your desired one. Now To Set The Text Color use this:
toastText.setTextColor(Color.Black);

If you want to use color from 'color.xml' then instead of 'Color.Blue' use 'R.color.colorPrimary' (change 'colorPrimary' with your desired one). If you want to set the color using color code, then use 'Color.parseColor("#000000")' instead (change '#000000' with your color code)

Changing Toast Text Size:

Copy and paste this first line of code from the previous customization (skip if you already added this line once):
TextView toastText = (TextView) toast.getView().findViewById(android.R.id.message);

Then, to change the text size, use this line:
toastText.setTextSize(20);

Change '20' with your desired font size (in SP)

Changing Toast Background Color:

To change the Toast background color, first of all, we will need to get the View (background) from the Toast class. Just add this line first without changing the code to do that:
View view = toast.getView();

You can change the 'view' with your desired one. Now To Set The Background Color, use this:
view.setBackgroundColor(Color.White);

Check the color customizations from 'Changing Toast Text Color' for using color code or color.xml color instead of 'Color.White'

Need anything else? Try asking us in the comment box, we will reach you as soon as possible.

So, The final Codes will look like this:
package com.zealtyro.example;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

Toast toast = Toast.makeText(getApplicationContext(),  "YOUR_MESSAGE_HERE", Toast.LENGTH_SHORT);


toast.setGravity(Gravity.TOP|Gravity.RIGHT, 10, 10);

TextView toastText = (TextView) toast.getView().findViewById(android.R.id.message);
toastText.setTextColor(Color.Black);
toastText.setTextSize(20);

View view = toast.getView();
view.setBackgroundColor(Color.White);

//place all other customization codes here


toast.show();

}
}

Congratulations!

You have successfully used/displayed Toast to show your message.

Thanks For Allowing Us To Help You :)
If you are confused or want to know something then let us know in the comment box, we will reach you as soon as possible and Don't Forget To Subscribe our NewsletterYouTube Channel and Like Our Facebook Page To Keep Updated With Awesome Things. And Also to Follow us on Twitter to stay updated with the latest news & changes

Comments

You are always welcome to ask us and share your ideas
We try to bring to you the most amazing learning site for tech enthusiasts and thus work to teach you the basic and internal parts of software management, website and apps making, programming, etc. Programming and technical explications are the main points and we hereby provide you the requisite basic and a clear idea about programming and other technological interests. Our main purpose is to make you fluent in programming languages.

Subscribe

Contact Us

Send