Android Studio Getting Started

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 2.1.3.0 for Windows
  • jdk1.8.0_45.

Follow these steps to build and run a simple Android mobile app that displays Hello World! in an emulator:

  1. Get Started
  2. Create an Android App
  3. Display Full Project Structure
  4. Understand Application Components
  5. Run the Default App
  6. Restore Default Layout
  7. Develop the Application

1. Get Started

  1. Download and install Android Studio.
  2. 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

  1. Open Android Studio.
  2. Select Start a new Android Studio project.
  3. 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.
  4. Click Next.
  5. In Target Android Devices, leave Phone and Tablet selected. Do not select anything else.
  6. Click Next.
  7. In Add an Activity to Mobile, select Basic Activity.and click Next.
  8. 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.
as-initialscreen
Android Studio and MySimpleApp when Android Studio first starts up

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.

  1. MainActivity.java contains the Java code that presents the user with a user interface.
  2. 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:

  1. Locate the drop-down menu below MySimpleApp.When a project first opens after you create it, this menu displays Android.
  2. 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.

See Introduction to Android.

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.

Create AVD

  1. In Android Studio, select Tools > Android > AVD Manager.
  2. In the Your Virtual Devices screen, click Create Virtual Device in the bottom-left corner.
  3. In the Select Hardware screen select Nexus 6.
  4. Click Next.
  5. 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
    ABI: x86
    Target: Android 6.0 (with Google APIs)
  6. Click Next.
  7. In the Android Virtual Device (AVD) screen, take a look at the settings, and then click Finish.

Run the App from Android Studio

  1. Select the MySimpleApp project and click Run.
  2. 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).

 

defaultapp

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:

  1. Select Window > Restore Default Layout.
  2. If the project structure isn’t showing, click the vertical 1: Project on the far left.
  3. Open the MainActivity.java and content_main.xml files.
  4. 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.

Advertisements
Android Studio Getting Started

One thought on “Android Studio Getting Started

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s