Android Tutorial: Creating An Application
Creating an Android application with Android Studio is as easy as a few mouse clicks. This section shows how to create a Hello World application, package it, and run it on the emulator. Make sure you have installed the Android SDK and Android Studio by following the instructions in the previous section Android Tutorial: Environment Setup.
Next, follow these steps. Also, you can follow the official doc: https://developer.android.com/training/basics/firstapp
Creating An Application
1. Click the File menu in Android Studio and select New Project. The first dialog of the Create New Project wizard, shown in Figure 1.1, appears.
2. Enter the details of the new application. In the Application name field, type the name to appear on the Android device. In the Company Domain field, type your company’s domain. If you do not have one, just use example.com. The company domain in reverse order will be used as the base package name for the application. The package name uniquely identifies your application. You can change the package name by clicking the Edit button to the right of the field. By default, the project will be created under the AndroidStudioProjects directory created when you installed Android Studio. You can change the location too if you wish.
3. Click Next. The second dialog opens as shown in Figure 1.2. Here you need to select a target (phone and Tablet, TV, etc) and the minimum API level. This book only discusses Android application development for phones and tablets, so leave the selected option checked. As for the minimum API level, the lower the level, the more devices your application can run on, but the fewer features are available to you. For now, keep the API level Android Studio has selected for you.
4. Click Next again. A dialog similar to that in Figure 1.3 appears. Android Studio is asking you if you want to add an activity to your project and, if so, what kind of activity. At this stage, you probably do not know what an activity is. For now, think of it as a window, and add a blank activity to your project. So, accept the selected activity type.
5. Click Next again. The next dialog that appears looks like the dialog in Figure 1.4. In this dialog you can enter a Java class name for your activity class as well as a title for your activity window and a layout name. For now just accept the default.
5. Click Finish. Android Studio will prepare your project and it may take a while. Finally, when it’s finished, you will see your project in Android Studio, like the one shown in Figure 1.5.
The next section shows you how you can run An Application on A Physical Device.
Running An Application on A Physical Device
There are a couple of reasons for wanting to test your application on a real device. The most compelling one is that you should test your applications on real devices before publishing them. Other reasons include speed. An emulator may not be as fast as a new Android device. Also, it is not always easy to simulate certain user inputs in an emulator. For example, you can change the screen orientation easily with a real device. On the emulator, you have to press Ctrl+F12.
To run your application on a real device, follow these steps.
1. Declare your application as debuggable by adding android:debuggable=”true” in the application element in the manifest file.
2. Enable USB debugging on the device. On Android 3.2 or older, the option is under Settings > Applications > Development. On Android 4.0 and later, the option is under Settings > Developer Options. On Android 4.2 and later, Developer options are hidden by default. To make it visible, go to Settings > About phone and tap Build number seven times.
Next, set up your system to detect the device. The step depends on what operating system you’re using. For Mac users, you can skip this step. It will just work.
For Windows users, you need to install the USB driver for Android Debug Bridge (adb), a tool that lets you communicate with an emulator or connected Android device. You can find the location of the driver from this site.
For Linux users, please see the instructions here.
The next section shows you how you can run your application on the emulator.
Running the Application on the Emulator
Now that you have an application ready, you can run it by clicking the Run button. You will be asked to choose a device.
If you have not created an emulator, do so now. If you have, you will see all running emulators. Or, you can launch one. Click “Use the same device for future launches” to use the same emulator in the future.
Next, click OK.
It will take seconds to launch the AVD. As you know, the emulator emulates an Android device. Just like a physical device, you need to unlock the emulator’s screen when running your app for the first time.
If your application does not open automatically, locate the application icon and double-click on it. Figure 1.7 shows the application you just created.
During development, leave the emulator running while you edit your code. This way, the emulator does not need to be loaded again every time you test your application.
Creating An Android Virtual Device
The SDK ships with an emulator so that you can test your applications without a physical device. The emulator can be configured to mimic various Android phones and tablets, from Nexus S to Nexus 9. Each instance of the configured emulator is called an Android virtual device (AVD). You can create multiple virtual devices and run them simultaneously to test your application on multiple devices.
When you install Android Studio, it also creates an Android virtual device. You can create more virtual devices using the Android Virtual Device (AVD) Manager.
To create an AVD, open the Android Virtual Device (AVD) Manager. You can open it by clicking Tools > Android > AVD Manager. Alternatively, simply click the AVD Manager button on the toolbar.
If you have not created a single AVD in your machine, the first window of the AVD Manager will look like that in Figure 1.8. If you have created virtual devices before, the first window will list all the devices.
To create an AVD, follow these steps.
1. Click the Create a virtual device button. You will see a window similar to that in Figure 1.9.
2. Select Phone from Category and then select a device from the center window. Next, click the Next button. The next window will show. See Figure 1.10.
3. Select an API level and application binary interface (ABI). If you are using a 32-bit Intel CPU, then it must be x86. If it is a 64-bit Intel CPU, chances are you need the x86_64.
4. Click the Next button. In the next step,
you will be asked to verify the configuration details of the AVD you are creating. (See Figure 1.11.)
5. Click the Finish button. It will take more than a few seconds for the AVD Manager to create a new emulator. Once it’s finished, you will see a list like the one in Figure 1.12.
For each AVD, there are three action buttons in the rightmost column. The first icon, a green arrow, is for launching the emulator. The second, a pencil, is for editing the emulator details. The last one, a down arrow, shows more actions such as Delete and View Details.
[…] described in Chapter 1, “Getting Started.” However, instead of creating a blank activity as in Android Tutorial: Creating An Application, you should select Master/Detail Flow, as shown in Figure […]