Category

Beginner

Category

how to use toast in android with example - androidride.com

Have you ever seen “touch again to exit” or any other white text in a black capsule shape as pop up in Android???

Those popup messages are toast. In this post, you will learn how to use toast in android with a simple example.

Okay… what is toast ???

Toast is the subclass of java.lang.Object class, and it helps developers to make notification message that appears at the bottom of the screen by default. The temporary message pops up and fades away after a short time. Just like hot bread pop up from a toaster. While showing toast you can still access your activity.

Okay…Let’s make a toast in android

How to make toast in Android?

how to make toast in android - androidride.com

You can make toast in 2 simple steps.

    1. Call static method  makeText() of Toast class and give
      •  Context
      • CharSequence or String resource id
      • Int duration : LENGTH_LONG or LENGTH_SHORT
    2. Call show() method

Make a toast in one statement.

Don’t forget to add show() method, because it happens sometimes for me and think why the code doesn’t work after a long stare at the bottom of the screen.

You can make toast in background services, if your application not in the foreground while toast showing up,  toast may confuse the users. So use appropriate text with the toast to assist users to digest easily.

What happens if there is no text ??

what happens if the code like below??

then the output will be as shown below.no text in toast android -androidride.com

When you call makeText with your parameter value. Internally, it inflates $YOUR_SDK$/platforms/android-22/data/res/layout/transient_notification.xml and set your content to textview which have id message.

android-22 is API level, It may change based on your SDK.

How to change the position of Toast?

Just told you earlier, Toast pop up at bottom of the screen, center-horizontally. Toast class provides

setGravity(int gravity_constant,int xOffsetValue, int yOffsetValue)

method to position your toast where you want.

  1. int gravity_constant: Android provides lots of Gravity constant values. Just like CENTER, TOP, RIGHT, BOTTOM and so on.
  2. int xOffsetValue : If you want to change the position of your toast to the right, increase this value.
  3. int yOffsetValue : If you want to change the position of your toast to downward, increase this value.

Below code will place your toast to center of the screen.

you can use more gravity constants using the vertical line (|) symbol.

Below code places your toast to CENTER-RIGHT end.

More about Toast class

static constants

LENGTH_LONG: It shows view or toast for almost 3.5 seconds.

LENGTH_SHORT: It shows view or toast for almost 2 seconds.

Public constructor

Toast(Context context)

It creates a Toast object.

Public methods

i) public void cancel()

It hides the toast if it is already showing or if it isn’t visible yet.

ii) public int getDuration()

It returns the duration of view or toast. (LENGTH_LONG or LENGTH_SHORT)

iii) public void getGravity()

It returns location if that is already set.

iv) public float getHorizontalMargin()

it returns the horizontal margin in float.

v) public float getVerticalMargin()

it returns the vertical margin in float.

vi) public View getView()

It returns the view. You can access view items using this method.

vii) public int getXOffset() && public int getYOffset()

It returns X offset and Y offset in Pixels.

viii) public void setDuration(int duration)

Use this method to set duration of your toast or view.

ix) public void setMargin(float horizontal_margin, float vertical_margin)

Use this method to set margins of your view or toast. actually here margin means how much space needed between toast and side of the screen.

x) public void setText(int Resource_id or CharSequence string)

There are two variations of setText(…) method. One receives parameter as charSequence and other receives as string resource id. You can update the text in the toast that previously you create with makeText() method.

xi) public void setView(View view)

It sets the view for toast. It’s helpful while creating a custom view.

xii) public void show()

It shows the toast or view.

Let’s Create a project…

In this project, we will make a button inside linearlayout. When the button is tapped, a toast will appear at the center of the screen with application name in it.

File->New->New Project

Application name: Toast Ex

company domain: androidride.com

click Next and select your minimum SDK and empty activity template.

Activity name: MainActivity

Layout name: activity_main.xml

click Finish.

open activity_main.xml, and paste below code.
Linearlayout gravity attribute place button at the top position, center-horizontally. android:onClick attribute calls showToast() method in MainActivity. Then the toast appears.

open MainActivity.java and paste below code

Here, makeText() method returns a toast object and Using toast instance setGravity() method position toast at the center of the screen and show() method helps to display the toast.

Run now

android toast example screenshot- how to use toast in android - androidride.com

Don’t worry if your actionbar color is blue. You can change it through by copy-pasting my colors.xml given in full code.

For more information
Visit Toast documentation

Download Toast Example

Sharing is caring…..