Before creating your first Android Studio project, you'll need to make sure you've set up some basic software. As I've described previously, you'll need the Java Development Kit, or JDK, from Oracle. You also need to have installed and updated Android Studio. If you haven't gone through these steps yet, stop watching this video, and go back to the previous videos that describe how to set up the software. In this course, I'm using Android Studio 2.2.1, and I'm targeting API Level 24, for Android 7 Nougat.
- [Instructor] Before creating your first Android Studio project you'll need to make sure you've set up some basic software. As I've described previously you need the Java Development Kit or JDK from Oracle. You also need to have installed and updated Android studio. If you haven't gone through these steps yet, stop watching this video and go back to the previous videos in the course that describe how to set up the software. On this course I'm using Android Studio 2.2.1 and I'll be compiling and targeting with API level 24 for Android 7.0 Nougat.
You can follow along with this or any later version of Studio and the Android SDK. To create your first app, start at the Android Studio welcome screen and click start a new Android Studio project. I'm going to name my first application simply Hello World. A couple of things change automatically. First I'm starting with the company domain of dgassner.example.com. That domain isn't actually saved anywhere but it translated in this screen to a Unique Package Name.
Each Android app has to have a Universal Unique Package Name and this is used to identify the app in the Google Play Store, in the Amazon App Store and in other distribution channels. To create a Universal Unique Package Name you start with your company or organisational domain and then using Java style package naming you reverse the beginning and ending of the domain and then append an app name or category to that. If you don't like the way the package name came out, you can click edit and you can change it here.
I'm going to change my package name to com.example.android.firstapp and I'll use that same package name for all of the sample applications throughout this course. That way when I deploy an app to a device it will replace the existing app instead of creating a whole bunch of sample apps on the same device. Now click done to save the package name and then I'll look at the project location. You can create your project anywhere on your hard disk. If you try to create a project in a directory that has a space in it, you'll see a warning on this screen.
Although typically it still works fine. The default location when you first start up Android Studio is to place the applications in a directory named Android Studio Projects under your home directory. You can accept that or you can place the application anywhere you want. The name of the directory that contains the project, again, can be anything you want but shouldn't have any spaces in it. On the next screen you're which SDK you want to target as the minimum SDK and the default API 15 for Android 4.0.3.
As I showed previously you can click on the help me choose link and you can see how many different devices you'll be supporting. And this shows that by setting your minimum SDK to API 15, you'll be targeting over 97% of devices that currently access the Google Play Store, that's what these numbers mean. With each step as you increase the minimum SDK you'll reduce the number of devices you can target. So I have one application in the Google Play Store that targets KitKat, Android 4.4 and I can only reach 74% or so of the available devices.
But by not targeting the earlier versions I'm reducing the amount of testing and increasing the number of features that I know will work across all of the devices I'm supporting. I'll come back here and I'll once again set my minimum SDK to API 15 and I'm not going to check any of the other options to support other form factors. And I'll click next. Each Android app starts with what's called an activity. An activity defines a particular screen. And there are a number of templates available in Android Studio to let you define what that activity will look like.
The simplest is called the Empty Activity and that's the default. It presents a blank screen with just one bit of text on it. A Basic Activity includes a little more complexity. It has a floating action button in the lower right corner and a little bit of Java code that lets you see how to react when the user touches or clicks on that button. There's a Fullscreen Activity, an activity that supports ads, one that includes Google Maps and so on. One of the most useful is called the Navigation Drawer Activity.
It includes all the sample code you need to add a navigation drawer. That is a UI element that slides in from the left when the user either drags from the left or touches an icon. And there's also one called the master detail flow that gives you a great example of how to use fragments to adapt an application to different screen sizes. For our first app we're going to use the Basic Activity. It has a minimal bit of code, but it does show you how to accomplish some very important tasks in Android.
On the next screen you're asked to name activities, layouts and other resources. The activity is typically named Main Activity and it's associated layout file which is an XML file will be named activity_main. This follows an important convention, activity names are Java classes, so you're using Java class naming conventions. Upper case initial character and CamelCase to help you identify the class. The layout file is what's known in Android as a resource and the name of the file is translated to a resource ID.
Resource ID's in Android should be all lower case, no spaces or special characters other than the underscore. And so you use the underscore to separate words instead of CamelCasing. The title is a string that the user can see, so that can be anything you want including spaces and special characters. But in practice the title of the activity isn't always seen by the user, so you can pretty much ignore this. Finally there's a menu file that's going to be generated and it's also a resource, just like the layout.
So you follow resource naming conventions and by default this one's called menu_main. If you want you can build this activity using a fragment, that will make it easier to adapt the activity to different screen sizes, but because this is our first app, we're going to keep it simple and we're not going to use the fragment. I'll click Finish and that will result in creating the project. The first time you create a project in Android Studio, studio might need to go out to the web and download some additional components.
Also if you're working on Windows, as I am, you might see a Firewall warning. If that happens click allow access to continue. The firewall warning is because you're setting up debugging. That is you're going to be using an application called the Android Debug Bridge to communicate between Android Studio and your testing devices. On Windows you need to open the ports to accomplish that communication and that's what that Firewall warning is for. If you're working on Mac, you won't see that warning.
The first time you open up a project in Android Studio you'll get the tip of the day dialog. You can scroll through the different tips and learn more about Android Studio and then if you don't want to see this dialog each time you start up Android Studio you can uncheck the option to show the tips on start up. And then close the dialog. When the project is first created you might see some errors in this screen, known as the layout preview screen. Be patient, it will take some time to rebuild the project and make those errors go away.
If the errors still are there, when the project settles, that is when you don't see any messages at the bottom of the screen indicating builds or any processes are happening you can try refreshing the screen or you can try pulling down this list and changing to a different device preview. This for example is how the application might look on a Nexus 5X and this is how it might look on a Nexus 6P. You can even use this preview screen to see what the layout might look like on a tablet.
This is a Nexus 9. And I'll return to the Nexus 5X, that will be my standard testing device for this course. So now that you've created the project, you'll be ready to test it for the first time. After giving you a brief tour of the interface of Android Studio, I'll show you how to set up some devices so you can deploy and test your application.
- Installing Android Studio
- Creating your first Android Studio project
- Managing profile files, including Gradle scripts and support libraries
- Defining screens with activities
- Implementing designs in XML layouts