How to display a Toast message on Button click

Earlier we have created a simple Button in our layout file but this is not done yet because the button is not being clicked yet,so now we will display a Toast mesage when the Button is clicked. For this we have to add something like this in our file under the Button tag:
by adding this we have now generated the onClick method for Button but now also it will not do anything we are expecting.

Now as we have to create a event in Button when it is been clicked we have to add a Toast Message here. For doing this we have to add some imports in our MainActivity.java file, that imports be like:


After adding the above imports its time to create a Toast message which will display when the Button is clicked.
Toast Message goes here:
After adding the above code in your MainActivity.java file your toast message is now created which will display a message "This is a Toast Message" by clicking on the button. Congrats your application now display a toast message on button click, so now run the application in your Emulator or ADB whatever you use.

Full app code goes here:

activity_main.xml
MainActivity.java :
Screenshot:


Follow me on:

Comments