Click here to Skip to main content
13,453,767 members (45,051 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 16 May 2014

Show a Toast in the Middle of the Android Screen

, 16 May 2014
Rate this:
Please Sign up or sign in to vote.
Simple tip on how to show a toast in response to an event, and center it in the screen

Is the Handler Being Handled?

Sometimes, when you're adding event handlers for things such as button clicks/taps, you aren't going to add the actual code just yet, but you do want to receive a visual verification that the event handler code has been properly hooked up. You can always use LogCat while debugging, but especially when showing the project "as-is" (perhaps in "mockup" state), you might want to just show a simple message that basically says, "Yeah, I know, you clicked me."

I don't know about you, but I'm a little OCD-ish when it comes to having things centered (I still wonder why Windows forms aren't automatically set to display in the middle of the screen - it's a big pet peeve of mine). Anyway, enough of that; here's the code to show a "toast", centered in the middle of the emulator or device, in response to a button's "click" event:

Button btn = (Button) findViewById(;
btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO: Finish
        Toast tostada = Toast.makeText(MainActivity.this,
        "You mashed the button, dude (or dudette)!", Toast.LENGTH_SHORT);
        tostada.setGravity(Gravity.CENTER, 0, 0);;

Run the app, mash the button, and you will see it:

This is obviously a simple little snippet, but it can come in handy on occasion.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

B. Clay Shannon
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006:

You may also be interested in...


Comments and Discussions

GeneralThanks Clay. Pin
Sivaji156522-May-14 18:08
memberSivaji156522-May-14 18:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180318.3 | Last Updated 17 May 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid