onClick
We made a button in XML and defined it’s onClick property which when we go to Java will allows us to make it
into a function that when the button is clicked the function clickFunction is then called upon.
We had to import the View.
we’re putting a line of code into Log, the log tells us what is going on behind the scenes when the app is running.
So we’re putting a type of log called “i” for info, which tells you certaion information about your application.
We add a tag that describes the log, and we can display that message in the Logcat.
Log
OUTPUT:
EditText
To see what the user enters in the EditText box the log when the button is pressed we need to include it in the onCLick function we made
We first need to make a EditText variable which has a type of EditText
we will name it nameEditText
and we’ll set it equal to
the EditText box view and we’ll need to use findViewById()
to find that view. We need to use R
to be able to acess resources
and then we specify id
and then the name of the EditText box id we made which is nameEditText
.
Example
XML
Java
User Interface
Log Cat
Toast
To use the Toast features you have to import the Toast class first
You can display a pop up using toast by using the following
Displays the text typed in the EditText
box.
ImageView
We can use the onClick function from the button to change the image of the ImageView.
We use setImageResource(R.id.<filename>);
to change the image of the ImageView
Example
Example of changing an image onClick
Currency Changing Application
XML
UI
Java
This is the onClick function we covered
The function that’s doing the converting
OUTPUT