Monday, October 16, 2017

Android: Android Button Toast


Objective: 
The objective of this Android tutorial is to display the button and give pop-up message on button click. Refer the screen shots below.

Screenshots:


On-click of “CLICK ME” button, it should display the Toast as shown below.


Instructions to setup:
1. Git clone the project and you should see the project \1_Button_Toast downloaded
2. Now open the project in Android Studio
3. Press the keyboard – Shift + F10 to run the App, select the configured virtual device and click OK.

4. You can now see the output in the Android Emulator.

Source Code Explanation:
1. Activity_main.xml:
1. Overall Layout is set as "RelativeLayout"
2. Button component is set with:
id as "button"
text as "Click Me"
textColor as "white"


2. MainActivity.java:
1. Instantiate Button bt1 by using findViewById() by passing id name "button", as defined in activity_main.xml
2. Set On-click listener function for the button and onClick invoke the Toast.makeText().show() with required parameters as shown in the code below.





No comments:

Post a Comment