Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

First Android App Tutorial

Introduction

Welcome to the world of Android development! In this tutorial, we will walk you through the process of creating your first Android app from start to finish. This guide is designed for beginners, so no prior experience with Android development is necessary.

Setting Up Your Development Environment

Before we dive into coding, we need to set up our development environment. Follow these steps:

  1. Download and install Android Studio.
  2. Open Android Studio and follow the setup wizard to install the necessary SDKs and tools.
  3. Once the setup is complete, you are ready to create your first Android project.

Creating a New Project

Let's create a new Android project:

  1. Open Android Studio and click on Start a new Android Studio project.
  2. Enter the following details in the Create New Project dialog:
    • Application Name: MyFirstApp
    • Company Domain: example.com
    • Project Location: Choose a location for your project
  3. Click Next.
  4. Select Phone and Tablet and ensure that API 21: Android 5.0 (Lollipop) is selected as the minimum SDK. Click Next.
  5. Select Empty Activity and click Next.
  6. Leave the activity name as MainActivity and click Finish.

Understanding the Project Structure

Once your project is created, you will see several folders and files in the Project window. Here are the key components:

  • Java - Contains the Java source code files.
  • res - Contains resources such as layouts, strings, and images.
  • AndroidManifest.xml - Describes the essential information about your app.

Writing Your First Code

Let's write some code to display a simple "Hello, World!" message on the screen:

1. Updating the Layout

Open res/layout/activity_main.xml and replace its content with the following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/helloTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        android:layout_centerInParent="true"
        android:textSize="24sp"/>

</RelativeLayout>
                

2. Updating the Activity

Open java/com/example/myfirstapp/MainActivity.java and replace its content with the following:

package com.example.myfirstapp;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
                

Running Your App

Now that we have written our code, let's run the app:

  1. Click on the Run button in Android Studio.
  2. Select an existing Android Virtual Device (AVD) or create a new one if necessary.
  3. Click OK to start the emulator and run your app.

You should see the "Hello, World!" message displayed on the screen of the emulator.

Conclusion

Congratulations! You have successfully created your first Android app. In this tutorial, we covered:

  • Setting up the development environment
  • Creating a new Android project
  • Understanding the project structure
  • Writing and running your first code

Feel free to explore and experiment with your new app. Happy coding!