Android Studio For Beginners Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to create an Android app that I call quick app launcher and we're going to use this app as an example of how you can create an app that goes to multiple screens in the first example we're going to be creating this second activity button and that button will take you off of this screen and go to another screen we're also going to be passing information from this screen to make the other screen just say hello world the other button here called Google Search is going to determine whether or not this Android device has a browser and if it does have a browser show us google.com so if I can demonstrate here we're going to click the second activity button and we made the text hello world up here on this second page if I click the back button the other button called Google search will first determine if the Android device has a browser and then if it does go to WWE Google comm so if I click on that it found our Google Chrome browser and went to that web page so in this app we're doing two things we are either switching screens within our app or we're going to another screen outside of our app let's get started the app that we're creating today is going to focus on two of the core elements to Android development we're going to first focus on activities and then we're also going to be focusing on intense there's actually four core elements that you should grasp as we proceed forward here so let me just quickly define what these are you should think of an activity as a rectangular area that's going to display something it's a very general thing but a lot of people will think of it as the screen that they're on but also as you put things on the screen a lot of those objects like the buttons that I just showed you in the demonstration those extend from the activity class they those buttons themselves are rectangular and they display something so they too are activity objects they're just more specifically buttons but they could be text views or whatever so the second thing is what is an intent well an intent is an action that's being requested or we want to have performed and it could either be just some part of a process that we're trying to work out within our code or it could be a user interaction like a tap or a swipe let's say it's a tap or a swipe an intent is going to be like a translator translating that sign language so to speak into something that we want the Android device to perform so think of it as a translator or a mediator that is going to say here's what we want to have done what is out there on the device that could do it for us and that's where intent services and broadcast receivers come in an intent service is something that might be able to perform the task that the intent is requesting to have done and it could work on it in the background if necessary that way our user interface is not being held up by performing those tasks the user can still interact with advice while background tasks are being performed and then a broadcast receiver would then send out a broadcast to the the apps out there on the app that are running and say hey I'm done with this is anybody need to use it so the original intent could say oh I I want to process that information in some way and then it could proceed forward so that's how they all relate to one another and today we're going to be focusing on an activity and an intent we're going to build a second activity that one activity goes to the other and also we're going to focus on focus on having that activity launch another app out there on the Android device all right so now let's dive into actually creating it to get started let's click start a new Android studio project so we first need to give it a name and I'm going to call it quick launcher and the company do name name doesn't matter until you start publishing your app so I'm just going to put under schools org and it's going to create package names that are in Reverse domain name notation so it's going to say org diameter schools that quick launcher and use that in our app you can see that it added a number two down here to the project location automatically that's because I have my dumb my demo app already created that you saw earlier in the video so let me just click Next and this is the minimum API level in other words the oldest level device that can use this app so I'm just going to go with lollipop Android 5.0 and then click Next the activity that we're going to use as our example is an empty activity so we're going to select this one right here and just click Next and the top box is the name of the Java class that it's going to create mainactivity.java is usually the starter activity of your apps and the layout name the layout name has to be in all lowercase and if there's multiple words it needs to be separated by underscores that's the naming convention for layouts so we're just going to leave it as is and click finish this is going to take a minute to build so I'm going to show you when this thing comes up the two preferences that I usually set in my Android studio projects and once you set set them they're going to stay that way so the first thing is if you go to Android studio and select preferences I like to have the auto completion or auto import feature turned on this is going to be found under the editor category and you're going to click general and when you expand general auto import is the very first option and I have all of these boxes checked and what that does is as soon as I get finds a class is not imported into your java it will just do it if there is two different classes that you need to pick from then then you're going to have to use your import tool by just holding down option and hitting enter on a Mac or I think it's alt enter on a PC all right so I'm going to go ahead and cancel out of that and the second thing is you might want line numbers turned on so I'm gonna right-click in the gutter over here and then just turn on your mind numbers right there that will help you out throughout this okay next up you can see Android studio has finally loaded so the loading bar down here is all finished and over here you should have the project Explorer open now what I find is students sometimes have it looking like this for whatever reason and it's because the project folder is shrunk down so make sure that it's open here so we can examine it a little bit and if it's not on Android right here it could be on project and project is how it is basically organized on disk and Android is where you're pretty much going to leave it so these are the files that are going to matter to you now automatically what you should have that popped up is two tabs mainactivity.java and activity main that XML which shows you how it looks the folders that you're going to interact with the most in this app at least is going to be in the Java folder so we're going to interact with mean activity here and this is where we where you can add more classes and more activities and what also is important is the manifest up here every Android app needs to have a manifest that for instance specifies which activity to launch first so here you can see that main activity is the first one that's going to get launched it's a launcher activity another file that you're going to interact with a lot is found under the resources folder so that's abbreviated re s and you'll see it under values like for instance strings this is where you should be putting all of the strings of your apps so that it's better for internationalization of your app and being able to translate the text into multiple languages but it's also an organizational thing it helps you organize all the text in one place rather than in several different classes so just to show you the power of it we can just change the name of our app from quick launcher to quick app launcher and when I save that change and then I go back to view it you can see it top-2 that change and it would change it everywhere because it's a reference so this action bar up here is referencing the text in the strings file it's not actually you know hand coded in so that's nice the same thing is true for the colors of the app you can edit the colors of the app here and you can also add style sheets but that's for a future video another folder that we would interact with later on is the drawable what you would want to do is open this folder on disk and you can drag and drop images into it and that is going to Android studio will automatically remember things that you put in there and you can reference them directly all right so to get started here now that this is fully loaded then you've kind of gone over the project here let's go ahead and add the buttons to our activity main.xml so in this portion of the video we're going to handle the visual elements of our app here called quick app launcher and I'm going to shrink down the project explorer for now that'll give us a little bit more room on our screen so we're not going to need this textview here so I'm just going to select it and then push the Delete key on my keyboard and - that's going to get rid of it and we're going to drag two buttons on alright so I'm going to take one button out of the pallet and drop it in here and take another button out of the pallet and drop it in here as well so the first thing that I can do is we're going to select the top one and we're going to name it second activity so up here I'm going to call it second activity BTN and I'm going to push enter a lot of times is going to ask if you want to update the references to this button everywhere and you're going to select yes if it asks you that the next thing I'm going to do is I'm going to change the text of this so let me scroll this down a little bit and then you can see right here it says text this is what it actually displays so we're going to make this button go to another activity or another screen so that's what it's going to say now I'm going to change this one down here as well so this one with its selected I'm going to scroll up and I'm going to change the ID of it to Google button or BTN and text of it will simply say Google all right now how do we align this stuff so you'll see these little constraint buttons that are right next to it and for now what we're going to do is we're going to align these relative because the device could be held in portrait mode or landscape mode which either looks like this in portrait or its up-and-down or landscape which is side to side so to maintain like a consistent look out what we can do is we can just click and hold so that we get these little arrows and you can glue it to different sides now if you glue it to two sides equally here you can see I've got a glued to the right side and to the left side so it centers it and if I glue it to the top and the bottom it's going to Center it vertically just Google button out of the way so that's one way that you could do it now I'm going to remove those constraints by just selecting the button and clicking this X and that'll actually get rid of them and what I can do is maybe I wanted to have it centered vertically or I'm sorry horizontally like this and I want this button to be relative to it as well so I'm going to have it glued to the bottom of it and then what you could do is you also have these options down here where you can take this and say Center vertically in the parent so this is going to put it down here but it's this top line to the button that I had selected that's actually centered vertically and you can offset it a little bit if you want you can just click and drag it and what's happening is its setting some properties you can scroll up here that are a little offset so you can see like these are offset by five pixels right here vertically the slider bar here should get fifty this slider bar is horizontally so if I move this all the way to the right you see this slide that way that was to the left I'm sorry and then if I put it back it's back in the middle but what that does is if we change the device from portrait to landscape it's still relatively centered all right so that's different from being an absolute position so we go back to this and we'll develop them there all right now we also need another activity to go to so how do we do that well let me open up my project Explorer again and I'm going to expand out app and expand out Java with the Java folder selected you can make new activities now if I want it to be in this package I have to select that as well so I'm going to do that right now my select my first package which actually has the main activity in it and I'm going to right click select new and you'll have your activities down here so when you hover over activity if you're making any one of these and just select them we're going to be making an empty activity but sometimes I you know want to point out that you also have a gallery here if you're not quite sure it brings up the same screen you saw earlier in the video and again we're making an empty activity so you can see that and just click Next and here it plugged in the package name that we had selected so we had word up metal scrolls that quick launcher selected that's the package it's going to go into the activity name top one here is the Java name of the file so when I call it second activity and down here it's going to create the layout name so this is the XML file that creates the layout now if I check this box it could be a launcher activity that could be launched from within the app or outside of the app I'm going to leave that unchecked and then click finish ok so it's building that right now it should give us a blank second activity to clear out some of these other tabs that I had open from earlier and now I'm going to go to activity second that XML and this should show so it's going to look like I'm going to shrink this down so we have more space and we're just going to add one textview into this so if I drag a textview into this and then I'm just going to use these constraints to glue it the top and the bottom there's other ways to do this just a demonstrate once again using those constraints now it's centered in into the center of this screen alright now we have two activities we have the main activity in the second activity but these buttons don't do anything so we're going to go over to mainactivity.java and add code to update these things all right so first of all let's go ahead and make a reference to the button that we created over in activity main so we first have to reference that button from our code how do we do that so over in mainactivity.java and again if you don't have it open let me go back to project should be under your Java folder and more than likely in the first package here of main activities you can reopen it there okay so let's create a button variable so we're going to call this button variable second activity what that's going to point to is search for that button so we're going to cast our search using a method called fine view by ID you're going to use this method a lot so fine view by ID takes an ID and this I'm going to type R which is going to look in our resources so R is the resources and what do we want to look by do we want to look by an ID string whatever well we're going to search by ID so I'm going to type ID and put another dot as a separator here and I made a Google button and I made a second activity button well of course we want the second activity button so I'm going to push tab to autocomplete and then add a semicolon at the end and now I have a reference to that button that can now use that reference called second activity in here and actually I what I should have done is wanted to call the second activities etienne okay so I just updated the name of that alright now what we can do is we can add a click listener to it so to do that I can say second activity button the BTN dot set on click listener now with this popping up right here I would want to select this alright and then you can do that with your mouse or take tab alright and that's going to autocomplete now within this we want to create a new onclicklistener so how do we do that well new view so within this view we want to set an onclicklistener now see how it's popping up here you want to get used to selecting this so since it's highlighted in like a dark boring here that's the one that I want and I can use the IntelliJ IDE IDE here to autocomplete that so what you want to do here is as type of view dot onclicklistener yeah that's exactly what we want so you want to use that autocomplete and over here you can see that this is now popping up a safe do I want to implement the methods well yes I do it's required that this onclicklistener has an on click event that it's overriding so we're going to add that and it just pops in so I'm saving myself a little bit of typing I'm show you show you that there's also support shortcuts to use the autocomplete as much as you can so that you save yourself from typos okay so here now we're going to get to the point where we're going to focus on an intent so back at the beginning of this video I said an intent is saying this is what we want to have happen and Android is going to try to make that happen for us so we're going to create an intent object and the name of this is going to be starred intent so that's our nickname for this variable and what do we want to create we want to create a brand new intent now an intent is going to take an application context they just want to okay what is the context of this app this action so when you start typing get it's going to be the first option here called get application context and again you can autocomplete by pushing tab and we're going to put put in a comma and then the next thing that we're going to be putting in here is what class do we want to create an object of well we created this second activity class over here so that's what we want you want to go to that second activity screen and that is an activity so we have an intent and we have an activity and we're trying to link the two together I'm going to show you how to pass information to that's a second screen or that second activity in a minute they're not first so let's go ahead and just start this second activity there's a method called start and ten and within that you have to just pass it and intent well the intent that we just created is called start intent and we're going to fire it up and see what it does so when we click this button I'm sorry I just misspoke we want to start activity and we want to pass it the intent I'm sorry if I said that backwards okay so let's see what this does I'm going to go ahead and push play and this is going to take a minute to build but within your virtual device what we should see happen is the button will go to that second screen and display that textview what we want to do to it is pass it some information that changes the text of the text view from text view to something else like hello world and that's what we're going to put on this line of code here in just a moment but I want to show you the difference between the two and this is going to take a moment for it to load okay so our button here now has an onclicklistener so the onclicklistener is going to say hey I have this intent the user has interacted with this button what do we want to do with it our intent is to go to this second activity so go ahead and start that that's really the only instructions we've given it so I'm go ahead and click second activity and here you can see it's got that second screen and it only says textview in here so how do we send information from one screen to the next because a lot of times you might have a form and then on the next screen you have to process that information in some way to display something so I'm going to go ahead and shrink that down and this is how you do it it's really simple I'm going to replace this line right here that common with starting 10th which is the name of our intent and we want to put some extra information in here so I put extra and the way that this works is we can put as much extra information into like a little bundle that is sent to this other activity and then that other activity can unbundle it and use that information but it's done in key value pairs so we have to give it a key which is just the symbolic like eye identifier of it and then also give it the value so we can look that up by its key so what's typically done is you take your package name here like org mentor schools quick launcher and then you create a name for it so I'm going to call it something and then the next thing you give it is the actual value that you want to pass to the other screen so I'm going to make it say hello world in all caps and then fill in an exclamation point okay so now I can look up on the other screen using that key and it's going to display hello world so let me go ahead and stop that and I'm going to go over to second activity and we're going to look to see if this activity was past some extra information because here's what could happen later on as you build these apps and instead of going straight to second activity or instead of going to main activity your app could actually launch into the second activity directly so there's a chance where it does get information and/or it doesn't so here we can say if you look at blocks on if and we're going to check the intent of loading this second activity if the intent has extra information such as and let's go ahead and go back to see I'm going to copy and paste this it's wrong one just so I get the spelling exactly right I'm going to copy that key go over the second activity so if it has extra information that possesses that key then do the following so I'm going to create a little if structure here a decision structure that it's going to change that text view so we can reference that textview by creating a variable for it so from the color TV for textview what is that going to point to you well that points to the textview that if we find the ID called art ID and it was just textbooks we didn't change it then we can I'm going to for simplicity sake just create a new screen restring called taxed which is going to get the intense extra information get extras and get the string that has that key and just store it as I didn't complete that so here I wanted to get what I wanted again I wanted to get the string we have to that key okay now I'm going to just put text in that text view so I can just say TV set text with text all right so text view is the view that's sitting there in the middle of the activity text is going to hold a reference to whatever value that key has and this line of code is going to go put that text in that textview simple all right so let me go ahead and push play again and launch my virtual device give that a minute to load up and if you remember last time we click the button all the side was textview if everything goes well this time it should say hello world so I'm going to go ahead and click inside my activity to activate it and click this and here it change the text so we successfully passed information from this first screen when I click this button to second activity so just to show you where that hello world came from it came from here we put some extra information in our intent sent it down over all right so now this is an example of how to launch another activity within our own app so let me put in a little comment up here attempts to launch an activity with boom next up let's try to launch another app outside of our app that's on our Android device so now our intent is different we're going to attempt to launch an activity outside our app so we can use some of the built-in Android programs to accomplish the tasks that we want to accomplish so for instance maybe we want to go have our app go to a certain webpage when they click a button so let's do exactly what we did on these two lines so we're going to name it a little differently we're going to create a button variable and it's going to be our Google button and what this variable points to is something that we're going to cast as a button we're going to find the view by ID and we're going to look in resources for an ID that is called Google button all right which it found it so now we can go to the next line add a click listener to it so let's go to let's add Google button set on click listener and I want to set that up so I'm going to type auto-completed that and you can do that with the tab or the enter key and we want to set a new view on click listener and see how it's already selected the second one yes one exactly that submit tab all right when I hit tab it's going to put in that required information for on click listener on click listener implements in on click event so it needs to have that so let's go ahead and scale extra space for readability and let's say that we want it to go to a certain web address well the readability I'm going to create a string called Google and it's going to fold this web address WWE comm all right so now we have to parse that as a URI so Universal resource identifier and we have to give it a name so I'm gonna call it web address and what is that it well it's going to simply parse that string called Google and hold that now we get back to our intents so our intent is to launch something on our Android device that can browse to that web page so we're going to create an intense and we have to give that intent a name so I'm going to call it go to Google and what that's going to be is a brand new intent and in this case what we review is we're going to create an action view alright so this is a different kind of intent instead of the context of our application this is going to go outside of our application and launch that what we wanted to do we want to send it to this web address to try to open up ok so now here's the thing is with an intent it's going to try to find send out a broadcast that says hey there are any apps on this device that can do this action that's being requested of opening up this web address and if the device answers back yes then we can proceed forward that way but if there are no apps that respond back to this request then we won't be able to do it so we're going to need an if statement that checks to see if a response back was not null in other words the respect we got a response back from this request saying yeah there's an app on this device that can do this for us so I'm going to say if go to Google can resolve an activity that will be able to handle this request then get the package manager and just make sure that it's not null so that's what that means so in other words if you've got a list of of something that can actually perform this and go ahead and give it that activity so then we can actually start the activity called go to Google alright so let's see if it works let's go ahead and push play and this should restart our emulator with our new code built into it and down here by the way is the progress of that latest build okay so we already know the first button works so we're going to click the second button and here it tested this and said yes something came back and said I can open up that web address which was our browser and it went to google.com now just to make sure that that wasn't just a trick or a magician's trick we're going to try Yahoo and run that again all right so let's go ahead and push play parents building if we come again so I'm gonna quit Google and this time I expect it to go to Yahoo and here again I went to yeah okay so that right there is an example of launching an activity within our own app and an activity outside our app to finish up our video today I'd like to cover the main ideas of what an activity and an intent is that we brought up within this app so an activity is simply a rectangular area that's going to display something we often think of it as the screen that we're developing but many of the things that we're going to be putting on those screens actually extend from the activity class so they are also rectangular areas like that textview that we made display hello world or the button that we click that launched our Google Chrome browser so we put that code into an on create method and the reason why we put it there was because an activity goes through a life cycle of events and the first event in that life cycle is the oncreate method so we're putting it there so that we can attach the listener events to listen for those clicks and also update any information if it is passed any information so that was a good spot to put it in but in a future video we'll go through that life cycle the other really important thing that we showed multiple times in this video was the find view by ID you're going to use that method a lot to locate the resources in your activity so you'll pass it a resource ID or a resource streams that's our dot ID r dot string or not layout you'll see that a lot the other thing that we did was we analyzed intent the intent class represents some sort of action that you want to perform and you can send and get information using intents so if you want to move from one screen to the next you could put extra information into your intent and we use the put extra to pass a key value pair we could then retrieve that on the other screen by calling the get intent method and then getting the extras and then getting that particular string by giving it the key that we wanted to find out that it what its value was and then lastly we were able to launch other activities using the start activity method
Info
Channel: Bill Butterfield
Views: 451,887
Rating: undefined out of 5
Keywords: Java, Android, Android Studio
Id: 6ow3L39Wxmg
Channel Id: undefined
Length: 39min 7sec (2347 seconds)
Published: Mon Apr 17 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.