With Android Studio you can create apps for smart devices that use the Android operating system, such as: phones, tablets, wearable devices, TV, Auto, and Glass. To get started quickly, complete this simple tutorial that was written with the following software:
- Android Studio 220.127.116.11 for Windows
Follow these steps to build and run a simple Android mobile app that displays Hello World! in an emulator:
- Get Started
- Create an Android App
- Display Full Project Structure
- Understand Application Components
- Run the Default App
- Restore Default Layout
- Develop the Application
1. Get Started
- Download and install Android Studio.
- If you get an installation error that Android Studio does not point to a valid JVM, update your JAVA_HOME environment variable to point to your JDK installation. See this page on Stack Overflow for tips.
2. Create an Android App
- Open Android Studio.
- Select Start a new Android Studio project.
- In the Create New Project window, enter:
Application Name: MySimpleApp
Company Domain: example.com.
Package Name: Android Studio generates the package domain from the application name and the company domain. When you enter the application name, the package name displays as .mysimpleapp. As you type the company domain, Android Studio forms the full package name by appending the letters one-by-one starting before the period in .mysimpleapp and going left so that you have com.example.mysimpleapp.
Project Location: indicates the directory on your hard drive that holds the project files. This location was determined when you installed Android Studio. This is an editable field, and you can change the location before you proceed.
- Click Next.
- In Target Android Devices, leave Phone and Tablet selected. Do not select anything else.
- Click Next.
- In Add an Activity to Mobile, select Basic Activity.and click Next.
- In Customize the Activity, click Finish to accept the default name fields for this activity. Android Studio takes a minute or so to open and display the MySimpleApp project.
The left panel displays a flattened project structure. The right panel displays two tabs containing the following opened files: MainActivity.java and content_main.xml. The content_main.xml tab is open and, among other things, has an emulator that displays the default Hello World! message.
- MainActivity.java contains the Java code that presents the user with a user interface.
- content_main.xml file contains layout information for what is displayed to users. Initially this file contains general sizing and padding settings and a TextView widget for displaying text to the user. The default TextView text setting is “Hello World!”.
The left panel under app > res > layout > shows activity_main.xml (in addition to content_main.xml). The activity_main.xml file contains layout information for global and default interface elements such as the app bar, Summary menu stub, and a floating action button. It also contains an include for content_main.xml, which is where the layout information for application-specific user interface elements such as buttons, text view, and text edits resides.
3. Display Full Project Structure
The flattened project structure representation on the left does not reflect the actual project structure on the disk. To make them match, do the following:
- Locate the drop-down menu below MySimpleApp.When a project first opens after you create it, this menu displays Android.
- On the drop-down menu, select Project.
4. Understand Application Components
An activity is an application component that is a single screen with a user interface for doing something such as playing a game or reading a menu. When you create a phone or tablet project, you start with the default activity, edit the default activity as needed, and add more components as needed. The list of application components includes activities, intents, intent filters, services, content providers, app widgets, processes, and threads.
5. Run the Default APP
In this step, you will run the default app on the emulator. To do this, first you create an Android Virtual Device (AVD) definition, and second, you run the app.
- In Android Studio, select Tools > Android > AVD Manager.
- In the Your Virtual Devices screen, click Create Virtual Device in the bottom-left corner.
- In the Select Hardware screen select Nexus 6.
- Click Next.
- In the Recommended tab on the System Image screen, choose the top system image.My screen shows the following on the top line. Just pick the top one if your installation shows different images.
Release Name: Marshmallow
API Level: 23
Target: Android 6.0 (with Google APIs)
- Click Next.
- In the Android Virtual Device (AVD) screen, take a look at the settings, and then click Finish.
Run the App from Android Studio
- Select the MySimpleApp project and click Run.
- In Select Deployment Target, select your emulater and click OK.
It takes a minute or so for the emulator to display with the app.
About the App
When you created the project, you selected Basic Activity instead of Empty Activity. The default differences between a basic activity and an empty activity are the following:
Basic Activity defaults:
- Has a Settings menu stub and a floating action button (pink mail icon) on the first page.
- Has a content_main.xml file in addition to the activity_main.xml file.
- Uses a relative layout (see Layouts).
Empty Activity defaults:
- Has only the activity_main.xml file.
- Uses a linear layout (see Layouts).
Settings Menu Stub
Click the three vertical dots in the upper-left corner to display the Settings menu stub. You can see the layout for the Settings menu stub in:
app > src > main > layout > menu > menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.mysimpleapp.MainActivity"> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" app:showAsAction="never" /> </menu>
Pink Floating Action Button
Click the pink floating action button in the lower-right corner. A message displays telling you to “Replace with your own action.” You can see the settings for the floating action button in:
app > src > main > layout > activity_main.xml:
<android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email" />
You will learn more about these files in Android Studio: Develop the Application.
6. Restore Default Android Studio Layout
If you have been experimenting with Android Studio, you have probably noticed you can change the layout quite a bit. If you want to get back to something close to the layout you saw when you first installed and opened Android Studio, do the following:
- Select Window > Restore Default Layout.
- If the project structure isn’t showing, click the vertical 1: Project on the far left.
- Open the MainActivity.java and content_main.xml files.
- Set the drop-down menu under MySimpleApp to Android.
7. Develop the Application
See Android Studio: Develop the Application for a follow-on to this posting that adds a button and a second page to the application.