IMAD
Understanding Button Interactions
Button Click Functionality
Button Reference: A variable called
button
is created to reference the componentBTN click me
.setOnClickListener: This built-in Android Studio function allows you to define actions when the button is clicked.
Changing Text: When the button is clicked, the action will set the text attribute of the TextView, identified as
TXT hello world
, to "button clicked".
Code Explanation
Declaring Button Variable:
button
refers toBTN click me
in the designer, initialized withfindViewById
.Event Trigger: Upon clicking the button, due to
setOnClickListener
, the TextView text changes as specified.Observation: Clicking the button effectively changes the text from "hello world" to "button clicked".
Variable Declarations in Programming
Syntax Overview
Variable Declaration: Example of variable declaration—
val myInt = 25
.Data Types Covered:
Integer: Holds whole numbers (e.g.,
myInt
).Double: Handles decimal values (e.g.,
myDouble = 3.14
).Char: Represents single characters (e.g.,
myChar = 'a'
).Boolean: True or false values (e.g.,
myBoolean = true
).String: Sequence of characters (e.g.,
myString = "Programming is fun"
).
Proper Use of Data Types
Restrictions: You cannot assign decimal values to integer variables.
Character Representation: Strings use double quotes whereas chars use single quotes.
Using Additional Buttons
Introduction of Button Two
Second Button Reference: A variable called
buttonTwo
will reference the buttonBTN display me
.setOnClickListener: Similar to
button
, using this will define action upon clicking this new button.
Displaying Multiple Variables
Code Logic: On clicking
buttonTwo
, the TextView text should display multiple variables.String Interpolation: Uses three double quotes to display formatted text.
Syntax for Displaying Variables
Structure:
Data Type, followed by a colon, then a dollar sign and the variable name (e.g., "int: 25").
Each variable's value must be prefixed with its data type and denoted with a colon and a dollar sign for proper conversion.
Example for Different Data Types:
int: $myInt
will display the integer stored inmyInt
(25).double: $myDouble
correlates to 3.14 stored inmyDouble
.char: $myChar
for character 'a'.boolean: $myBoolean
for true or false status.string: $myString
for the string content.
Important Note
Avoid declaring
textView.text
repeatedly for each variable; apply it once before compiling all variables to be displayed at once.Use formatting to present all values together with proper spacing.