Before we continue I'd like to point out that you may have noticed that we're currently working on a 1
Windows environment whereas before we were working on a Linux environment. We're simply doing this because 2
the windows machine is faster than the lab Linux machine. 3
It's just a matter of convenience but the entire section is exactly the same. 4
The only difference is the file system. 5
So for example here the project is saved in D Drive in Linux 6
it will be under your home directory but that is it. 7
Whatever we're doing here in Windows it will work exactly the same in Linux if you have any issues in 8
this regard. 9
Just let us know and we can help you out but you shouldn't face any issues. 10
Just follow everything we're doing OK. 11
So right now we have three UI elements. 12
Here's the helloworld that is included by default. 13
And here we added a send button and an edit list. 14
So what we're going to do now is interact with the UI elements. 15
We're going to add text in the edit text and when you click the send button the text that's added to 16
the edit box is going to appear on the text to view over here. 17
So to do this we have to go on the activity that's linked to this user interface. 18
Now there are two ways of creating event handlers. 19
We're going to show you both ways. 20
First let's see the more common way. 21
We start by going to the main activity. 22
Now this is going to involve some programming. 23
If you don't have programming experience don't worry we'll provide all the code. 24
It's just a matter of copying and pasting. 25
We'll also provide the entire project that you can just import and follow what we're doing. 26
If you prefer just follow what we're doing and try to understand which code goes where, to be a reverse 27
engineer you don't have to be a developer OK. 28
So here we're going to create an event handler so we're going to create what's called a method. 29
The method is going to be called Public void and we're going to call it post item. 30
Here we are going to write. 31
View .OK. 32
Don't worry too much about that red warning. 33
We'll go over those soon. 34
First we're going to refer to the edit text that we have in our user interface to do this we're going 35
to use the class: 36
EDIT text and we're going to define a variable and call it ET. 37
This is not a programming course so we're not going to go into too much detail about these details. 38
We mainly want to give the reverse engineer an idea of the process that the malware author goes through 39
when developing an app so here what we have to do is find view by ID. 40
R. ID. 41
Edit text, so what we just did here was create a variable called ET. 42
Edit text and we are telling it... 43
This edit text is going to be the UI element with the following ID, so here we're linking the code 44
to the user interface element, similarly we're going to do the text view, text to view TV equals find 45
view by Id. 46
Actually we need to add an ID to the text to view so let's call it textview. 47
OK. 48
So we go back to our find view and pass R dot Id dot text view so now we've added a reference to our 49
edit text and a reference to our text. 50
View what we're going to do is simple. 51
We're just going to say TV dot settext 52
E T dot get text. 53
What does this mean. 54
So we're referring to the TV text view and we're telling it to send the text of the edit text to the 55
content of the text to view OK. 56
So it's quite straightforward. 57
We're just creating an event handler that's getting the text from the edit text and posting it into 58
the textview that's literally what we just said over here. 59
So here we see that there is an error so it won't compile. 60
You're seeing there's that red line under here now. 61
Android Studio has this auto complete function where it tells you when there's an error and how to fix 62
it. 63
So in this case here it's telling you that we were missing a class reference, we're missing the following 64
imports 65
so here it's actually telling you what you need to do. 66
Do you need to add Android dot view class. 67
If so press Alt and Enter when you press alt and enter you see that it automatically added this import 68
in this class. 69
And then the errors are gone. 70
OK. 71
So if we run this now you'll see that nothing happens. 72
So why is this. 73
We run the application you have your user interface and you click it. 74
But nothing happens. 75
The reason is that we need to attach this event handler to the button and this hasn't been done yet. 76
So to do this we're going to use the user interface click on the button and there should be on click 77
on click over here click and you're going to choose it automatically detects what event handlers are 78
available for you just click on that. 79
And now it's been attached. 80
So what happens now when we run the application 81
there you go. 82
It worked so if we write test and click you see that test gets written in the text of you OK that's 83
your first simple user interface application. 84
Next we'll show you how to do an event handler in another way so let's go ahead and remove this event 85
handler and let's we just detach the event handler on click from the button. 86
So now we're back in a situation where if we click on the button in the application nothing happens. 87
Now another way of creating an event handler is by doing everything in code. 88
This is useful when creating UI elements and event handlers dynamically so we won't refer to the event 89
handler within the XML but rather we'll just create it in Java programmatically so in this case for 90
example we can go on the on create method so the UN create is what happens when the activity starts 91
so when the activity is created for the first time this code is executed and used to be able to attach 92
your own actions over here to run after the first execution so in this case what we're going to do is 93
we're going to say button Btn and equals button find view by Id R dot I d dot button send. 94
So what we're going to do here is we're going to put an event handler directly on the button so to do 95
this we're going to say Btn and Dot set on click listener so here I'm going to show you how developers 96
work google stack overflow button on click and usually in the first post you will find your reply copy 97
and paste and change your ideas. 98
This is how many developers actually work. 99
There's no need to remember all of the details, stack overflow usually has the answer. Then in the on 100
click function we just copy the same code as earlier fix it up a bit. 101
What we have here is we created a button defined in an event handler telling it on click add the text 102
from the edit text to the text to view. 103
Now we have to run this, enter text and click and as you can see we get the same behavior without specifying 104
anything on the user interface. 105
We did everything programmatically, so in both scenarios you get the exact same behavior. 106
It's just a matter of preference for whichever option you choose.