FRED WIDJAJA: All right. Hi, everyone. Thank you welcome for joining me at this seminar. So today we going to be talking about how to develop apps for Android, and particularly, we're going to be using Android Studio in order to develop our apps. So as you all may know. Android uses Java, and so we'll be going through a brief introduction on Java itself. So I know many of you only have experience programming in C. So that's totally fine, and that's because Java is very, very similar to C, with little differences. So the types in Java are fixed in the number of bytes. So in C, we have shorts and longs, and they differ in number of bits from machine to machine. In Java, it's already set as a standard. So bytes have eight bits, shorts have 16 bits, and so on and so forth. We also have chars, booleans, and also floats and doubles. So the way you program in Java, it's very similar. So for example, we have for loops and while loops, and if and else statements are exactly the same as you would do in C. All right, so the special thing about Java is that it is object oriented. Now, what are objects? So if you look at the things around you, you'll notice that every real world object has two common characteristics. And that is that they have state and they have some sort of behavior. So for example, dogs would have states which are name, color, breed, maybe if the dog is hungry or not. And some behaviors that dogs would have would be barking, or wagging a tail, or fetching a ball. So we can also apply this to inanimate objects. So for example, cars, they have the model, year, max speed, and so on and so forth. And they have some behavior. For example, accelerating, or braking, or going in reverse. So Java objects attempt to model this by having fields and methods. So fields are the states that the Java object may have, while method would represent the behavior that a Java object would have. So when I say methods and fields, I really just mean variables and functions. They are exactly the same, except that they apply to a certain object. OK, so moving on to classes. Java classes are like the blueprints for an object. They tell you what methods it has, what fields it has, and even here, we have something called access modifiers which we'll get to in a while. So to declare a Java class, it's very similar in syntax to a C struct. Here we start off with public class Car, and then we have an open brace and then some number of fields that you want the object to have. And then here we have some methods, and we also have a object constructor. Now what this object constructor does is it tells the object, OK, I want to initialize this object with some sort of fields. So here, we want every Car to have a model and a year. So we have a constructor just specifically for the model and the year, and this would set the model field to a certain value, and the year field as well. Now about the access modifiers I was talking about. Here we have the public and private access modifiers. So you can think of this as the RWX in your directories. So they allow certain people to read, and have access to these fields and methods. So if I say that model and year and speed are private, that means that other objects cannot attempt to access this object's fields there are private. But when I set the access modifier to be public, now that means that other objects are free to access and even used the methods that are defined here. So this is the basic Car object that I have. We have the accelerate, break, and getSpeed methods. To this point, is everyone clear on Java objects and how to declare them? Cool, all right. So if you want to use the Java object in a program itself, then we can use this new Car constructor. So here we're saying we want to declare a new object, which is of type Car. And here it's applying the model and year, which we have defined in this object right here. So this is also another object, if you notice. It's a class for the main program itself. And we have this PSVM, or public static void main, function, and its the starting point of your program, similar to the main function in your C program. Here, we can access and use the object's functions using the dot and then the function name notation. And we can even specify certain arguments or parameters for that function. OK, so now moving on to inheritance. So real world objects may share the same characteristics. So for example, there's different types of bicycles. For example, mountain bike, city bikes, or tandem bikes. So they have this common characteristic of being a bike. And so bikes may have two wheels and they may have a handlebar, maybe even a tail light or something like that. So Java gives us this power to represent objects that have certain common characteristics through inheritance. Now, what inheritance does, is it passes on an object's fields and methods on to a child object. So for example, if I have a car, and it has the state of having four wheels and one steering wheel, then we can pass this on to certain different types of cars. For example, we have the sedan, sports car, and an SUV. Now, these three different types of cars would still have four wheels and one steering wheel, but then they may have certain special characteristics, such as having five seats and a for a sedan, for example. So to make use of this inheritance feature, we use this extends keyword. So remember back to the Car object that I defined previously. Now I define a Sedan object. So this Sedan object would have the same fields and functions, or methods, that the Car would have. But then, here I specify certain additional functions, for example, the set number of passengers and the get number of passengers. Here, you may also notice that we can override methods, and that means we're just replacing the behavior to act differently in certain cases. So, for example, I want to set a max speed for our car. And to do that, I check whether this speed would go over the max speed. And if it doesn't, then I call the super class' accelerate method. So this is the super.accelerate() calls the super class' accelerate method. Finally, we're going to talk about interfaces. So interfaces are a way to expose only certain methods to the outside world. So they're like a table of contents, and you can think of them as like the C header files, just like methods with empty bodies, and they specify what the object or the class has to have, or what methods the object or class has to have. So for example, if I have a Bicycle interface, and it has three different methods, and here if I implement this interface, then that means that this Bicycle would need to implement the same three methods that I defined in the interface. Up to this point, any other questions? All right, cool. So this was a very brief and quick tutorial on Java. There's way more features and topics on Java, that you can actually go through. For example, there's generics, there's data types, there's even enums and stuff. So if you want to learn more about it, do follow these links, and they'll teach you more about them. OK, so, let's move on to the actual meat of the seminar, and that is actually Android programming. So before we actually get to programming, I want to talk about some jargon. So an activity is Android's way of saying it's a single screen within the application. So, for example, if you have a Gmail app, we have the view emails activity. And then if you click a certain email, that will show another activity where it's a view email activity. And then we also have the compose activity, or the edit message activity. And so activities are generally just a single screen within the application itself. Now, views are the components that make up the screen, or the activity itself. So they draw a certain thing in your screen. So for example, it could be a text field, it could be a label, it could be a checks box. So it is many different types of views. So now we have intents, and that's a message that is passed on to another activity. And they contain information as to what you want this activity to execute. Finally, we have the manifest file, and that is an XML file that gives your phone all the information about your app. So that's includes the version, and the activities that you will have. OK, so let's start. So we're going to be using Android Studio. So if you guys have this already installed, feel free to follow along. We'll try and start over from scratch. And so I'm going to try and create a new project. And I'm going to call my project Android 101. And here we put the name of the project in the application name field. Company Domain, you don't really have to worry about that. This is just how Android identifies your application. So you have a domain name, you can type in your domain name inside of there. But if you don't, you can feel free to type in anything you want. So here I just type in frederickw.com. And then, finally, I choose where I want to store my project in. And I'm going to just choose my desktop. So Users, Frederick Widjaja, and Desktop. OK. Then click Next. Here, I'm going to choose the minimum API to be supported. So in this case, I'm going to choose API 15. And it's fine if you actually just choose 4.4 which is the latest API. Or 5, in fact. But then you just need to worry about which phones you're really supporting, and which phones you're targeting. Click Next. OK, so we come to this screen, and we're just going to use create an activity through this. So I'm going to click blank activity and then Next. We're going to call it main activity, I think that's fine. Just going to call it, press Finish right here. Wait for it to load, and there we go. So here we notice three different folders. We have the manifest folder that contains our manifest. If you open it, we'll see our package. Or how Android identifies your application and then we have this application tag, which contains the title of our application as well as the icon for the application. We can actually run this application straight. So why don't we try and do that? I'm going to use Genymotion. If you brought your own phone, you can link it up and run it if you want to. But I'm just going to use an emulator, and I'm going to start this. Ooh. Well I think that's going to take a while, so I think we'll just get straight into explaining more about this. So here we have the Java directory, or where your source files are contained. Here we have the main activity itself. You can see that it extends the activity class. So our main activity is a screen that will be in our application. And then here we notice that a number of functions are auto generated for us. We have the onCreate function, and that will be called whenever-- STUDENT: Do you mind making the text larger? FRED WIDJAJA: OK, sure. I'll try and increase the font size. Think that should be good. That good? STUDENT: Just a little bit higher, like 20 or something. FRED WIDJAJA: 20? OK. OK, all right. So here we have onCreate method that would be called when our activity is created in the [? show ?]. We have the onCreateOptionsMenu, and we'll see that there's actually an action bar, and then we can add certain menu items on to it. And then here we have the onOptionsItemSelected. So this is like an event listener, so whenever an option in our action bar is clicked, this function will be called, and it will be passed with a certain item that was clicked. OK, I'm going to see-- that's not working well. Let's try and start. Oh dear. OK. This is worrying. OK, so anyway. Here we have the layout for our activity itself. This is our main activity, and here we notice that there's the action bar. And then we have this hello world label, and we can actually edit text right here. If you go and scroll down to the Hello World, we can change it to whatever we want. For example, it could be, Hi there. Welcome. And there we go, it changes. Oh dear. This style not working. OK, that's worrying. OK, anyway, we can move on without that. So for now, let's try and do some really basic stuff. So here we going to have a label, and then we're going to have a text view-- sorry, not a text view, we're going to have an edit text. And that is a component for you to type and stuff. So we have a plain text, and then we just add it onto our interface. Here, I want to also add some margin to the top so that it looks nicer. So we're going to go on to the properties, and then at the top margin, we're going to add in 20 DPs. So now, DPs are just a unit of measure in Android, and they stand for density independent pixels. So, as you may know, Android, there's many different types of screens and then they have different screen densities. So here it's just going to be a density independent pixel, so it's going to be same throughout all the screens. Going to extend this edit text, and then I'm also going to add a button. And it's going to say OK. 20 DPs. OK. So now we have three different components. And the way we can refer to these in our main activity class is by using this method called findViewByID. So I'm going to define some fields. So we have in TextView, so let's say helloText. And then if you're using Android Studio, you can use the shortcut alternate, enter. And click Import Class to automatically import the corresponding classes that we need. And then here I'm going to add an ID to this TextView and call it text_hello. Now that we have defined an ID for this TextView, we can use this to refer to the textview that we just defined. So helloText equals TextView findViewByID, and then we specify the ID that we want, which is r.ID.text_hello. So this is very similar to JavaScript and jQuery, where we can actually just refer to certain elements in the layout just by using the ID that we have specified. So in general, to access a certain object or a certain field in our layout, you're going to have to use this findViewByID method. And then we're going to do the same things for the other two elements. Here, I am going to add the ID text_name. Yes. As well as for this one. Button_ok. Now what I want this to do is when we click the OK button, and we have inputted some sort of name into the text field, then it's going to update this label to say, hi, name, welcome. So, back in our main activity class, we're also going to try and refer to those elements that we have to find. So we have the EditText nameText. Oops. Enter and alternate, import class. And then also the OK button. Similarly, we're going to do the same thing over here. nameText equals EditText findViewByID, r.ID.text_name. And then the OK button. findViewByID, r.ID, and then button_ok. OK, so now we have all three elements that we want to refer to. So now I'm going to attach an on-click listener to our button to listen for user clicks. To that we just use the setOnClickListener event. Now, here it's saying we need to specify setOnClickListener, and you can do that by just having a new onClickListener, and press Enter. And then you have this anonymous class, or anonymous object. And we also have this onClick method, and that's basically what it's going to be called whenever our button's clicked. So here, I want to get the name that was input into an text field first So String name equals to nameText.getText(). Again, this is like the Java syntax for referring to functions within the object. So nameText.getText, and then we're going to convert it to a string. Now we have the name that was input by the user. And then we're going to generate a new string, so it's going to be called hello. I'm going to say, hi, name, welcome. And finally, you want to update the helloText. So to do that, we simply call helloText.setText(hello). OK. Oops. I'm going to try and re-run Genymotion one final time and see if it works. If it doesn't, it's totally fine. OK. So I guess it doesn't work. Actually, let me try and do this. Could be this. Genymotion. OK. All right. Yes, it works. OK. That was frightening. OK. I did not mean to do that. OK, so we have this Android emulator. Let me turn this off. So we have our Android emulator. So we're going to try and run this app that we made. Just going to click the Play button, and it's going to say, waiting for adb. Oh, man. Restart. OK, I guess this still doesn't work. OK, nevermind, then. this is going to be a pity. But so let's say that we want to create a new activity, and we're going to have a picture in that activity. To do that, we can just click this File, and then New. And then here we can select Activity, and then Blank Activity. Here, we're going to call it maybe CatActivity. OK, so now it generates two other files. One is called activity_cat, and the other is called catActivity, which is a class. So let's say that we want to add a picture of a cat right here. So when you do that, we're going to use the ImageView class. We're going to add it onto here. And then set the margin to be 20 DP. And now we're going to find an image of a cat on internet. So let's see. Cat. OK. I guess this is fine. OK. So now we have our image. So we're going to add it into our project. Can do this by right clicking, and then-- we can actually just open this, go downloads, sorry, desktop. Going to try and find it. Cat. Right here. I'm going to copy it into my folder. Now, it's going to ask me a few options. Now, I just want this to be in the drawable folder, and that's going to be for all different screen resolutions. Alternatively, we can actually just select which screen resolution you want this image to be in. So going to select the drawable folder, click OK. Just going to name it cat.jpg. OK, so, now it's added into our image view. we're going to have to use this source right here, and then we can use this dot dot dot button, and select the cat image. OK. So now I want to make it such that it's just the right size. So I'm going to select this adjust view bounds option right here, and that automatically sets it to the right size. OK. Now, to actually launch this activity from our previous activity, you're going to have to use something called the intent. So let's say I'm going to add another button that launches the activity. I'm going to add it here, right here. And then it's going to have the text activate it. Oops. OK. Now, how do we actually open the new activity within this activity? So same deal as before. We need to add an ID to this button, and call it button_activate. And then in our main activity, going to have this new Button activateButton. And then also use the findViewByID method. activateButton equals Button findViewByID r.ID.button_activate. OK. And then we also going to add in an onClickListener. New onClickListener. And then we're going to use this thing called Intent. Intent equals new Intent(this). And then the name of the activity that we want to show up. So here in this, we're going to have catActivity.class. Sorry. We're going to have to specify @MainActivity.this. So now the .this property is referring to the current instance of the object itself. OK. Up to this point, any questions? All good? All right. And then if you want to start the activity, it's pretty simple. We just call the the startActivity method. And then we pass in the Intent object that we have just defined. Unfortunately, we can't really show this. I was hoping that we can actually run this and show it along in the seminar, but unfortunately not. Oh, man. I see. Oh, we're going to try and kill ATB.exe. OK. Restart. Did it work? Oh, OK. So now it's actually compiling. Oh. Unfortunately, there's no devices running. Let's try and restart my emulator. Ah, there we go. It appeared. OK. So I'm going to launch my app on my Android emulator. And we have the things that we defined just now. So we have the label, the text view, the text field, and then button. So I'm going to input my name right here, and there we go, it changed the text. So if we type in Foo, it's going to change to Foo. And if I click the Activate button, which we have linked to actually launch a new activity, hopefully this will work, there we go. It launches the new activity. To go back, we can press the Back button right here. Or not. Oh, there we go. Oops. That was bad. OK. So now that we have implemented some basic stuff in Android, we can try and do some more complicated stuff. So first, let's try and pass messages from one activity to another. So let's say we want to pass in a name from the main activity to the cat activity. And a name would be retrieved from this nameText. OK, so first, we're going to retrieve the name right here. String name equals nameText.getText().toString(). OK. And then we can use this thing called intent.putExtra(name), and then it actually also wants a key for this extra field. So I'm going to define a new key, we'll call it EXTRA_NAME. So I'm going to use this key, CatActivity.EXTRA_NAME, and then pass in the name itself. OK, so now we can actually retrieve this within the new CatActivity. To do that, we have to use the intent equals getIntent method. So now it's going to retrieve the intent that was used to call this new activity. So if you want to retrieve the string that we actually put inside the intent, you can use an intent.getStringExtra and then pass in the same key, OK, so now let's say we you want to change this field. Change this field right here. And let's make the font size of this field larger. So let's say I want this to be 20 DP. OK. And I'm going to add an ID for this TextView, and I'm going to call it text_cat. Sorry. And back in CatActivity, same deal as before. We're going to define some fields. Here it's going to be a TextView catText. And then catText equals TextView findViewByID, r.ID.text_cat. OK. And then back in here, I'm going to set the text right after we retrieve for it from the intent. setText(name). OK, so let's try that out. Click the Play button. I'm going to use the same device. Back in here. So let's say I didn't input-- if we don't specify any name, let's see what happens. So now there's nothing that appears. So we go back and input our name, it's just going to say Fred. So we can actually pass messages from one activity to another. OK. So there's a bunch of other views that you can use in Android. So now we have progress bars, we have list views, we have toggle buttons, and if you really wanted to learn more about that, you can actually visit these web pages. They contain many different resources. So for example, the API guide right here actually gives you some code examples for certain features in the Android library. And a few tutorials I found useful when learning about Android. That's the official tutorial itself, and also this other tutorial right here. And finally, you can actually use different libraries for your Android application. Now, these actually extend the functionality of your app without having to really write much code. The downside is, of course, that you actually have to learn the libraries yourself. So for example, if you want to make use of Dropbox cloud technology, then you can use the Dropbox API. Similar with the Facebook API. And then there's actually a few different other miscellaneous libraries that you can use. If you're trying to get, like, JSON from a certain website, then I think the Google JSON library would be pretty helpful. So now, that converts a JSON formatted text into a Java object, and vice versa. And then there's Retrofit and Picasso. And that's if you want to use-- that's for if you're using a rest API. And Picasso, if you're using images for your Android application. All right. Thank you very much, and have fun coding.