Two Laptop Bag*
The moose likes Android and the fly likes How to create a toast via button click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "How to create a toast via button click" Watch "How to create a toast via button click" New topic
Author

How to create a toast via button click

zoheb hassan
Ranch Hand

Joined: Apr 01, 2009
Posts: 150

This is the code i wrote for it, I just want to post a toast message when user clicks a button

The main.xml file is as follows




and the Toaster.java file is as follows




Can you please look at this code and tell me what i am doing wrong
Thanks
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

1. To listen for click events, you need to attach a OnClickListener with the button (using setOnClickListener() API)
2. When you override the onClick() method, use it to display Toast.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
zoheb hassan
Ranch Hand

Joined: Apr 01, 2009
Posts: 150

Hey Monu, I did initially try this piece of code however i did not work,It was throwing the following error

The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)


The code i initially tried is as follows


zoheb hassan
Ranch Hand

Joined: Apr 01, 2009
Posts: 150

Well Problem solved.I just had to set a property with a button in xml

all the code remains the same the only addition takes place in main.xml file under button attribute which is



Thats about it
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

This article has good explanation:
http://techbreaths.com/2013/01/how-create-toast-message-android/


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Android ituts
Greenhorn

Joined: Apr 02, 2013
Posts: 4



 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to create a toast via button click
 
Similar Threads
nullpointer when i try to initiate onclicklistener
how to add a scrollbar to a view
[Resolved] Application crashes on starting second Activity
emulator window was out of view and was recentred
Why error in this code