Skip to content

Android - Hello World Example

Let’s begin our journey into Android development. Before writing your first example, ensure your Android development environment is correctly set up, as detailed in the ‘Android - Environment Setup’ tutorial. This tutorial assumes you have a basic working knowledge of Android Studio, the official IDE for Android development.

We will now create a simple Android application that displays “Hello, Modern Android!” using current best practices.

The first step is to create a new Android Application using Android Studio. Launch Android Studio. If you see the Welcome screen, click on “New Project”.

  1. Choose your project: Select a project template. For beginners, “Empty Activity” or “Empty Views Activity” (for XML-based UI) or “Empty Compose Activity” (for Jetpack Compose UI) under the Phone and Tablet tab is a good starting point. Let’s assume “Empty Views Activity” for this classic example.

  2. Configure your project:

  • Name: Enter your application name, e.g., “HelloWorldModern”.
  • Package name: This is your application’s unique identifier, typically in reverse domain name format (e.g., com.example.helloworldmodern). Android Studio will generate one based on your name and company domain if provided.
  • Save location: Choose where to save your project.
  • Language: Choose between Kotlin (recommended) or Java. We’ll use Kotlin for primary examples, as it’s Google’s preferred language for Android development, but provide Java equivalents where clarity is needed.
  • Minimum SDK: Select the lowest Android version your app will support. A common choice is API 26 (Android 8.0 Oreo) or API 28 (Android 9.0 Pie) to balance wide device reach with modern features. Choosing a lower API level reaches more devices but may require more backward compatibility code. Higher API levels allow use of newer features without workarounds but reduce device reach. The IDE often provides statistics on device reach for each API level.
  • Build configuration language: Usually Kotlin DSL (build.gradle.kts) is preferred for new projects, but Groovy DSL (build.gradle) is also common.
  1. Click “Finish”. Android Studio will set up your project and sync with Gradle. This might take a few moments.

Before running your app, familiarize yourself with the key directories and files in a modern Android project (using the ‘Android’ view in the Project window):

No.Folder/File & Description
1app/manifests/AndroidManifest.xml
This is the manifest file. It describes fundamental characteristics of the app, defines each of its components (activities, services, etc.), declares permissions, and specifies hardware/software features required.
2app/java/<your.package.name>
Contains your Kotlin (.kt) or Java (.java) source files. By default, it includes a MainActivity.kt (or .java) file, the entry point activity launched when your app starts.
3app/java/<your.package.name> (androidTest)
For Android instrumentation tests.
4app/java/<your.package.name> (test)
For local unit tests.
5app/res
Contains all non-code resources:
drawable/ - For vector drawables (XML defining shapes) and other general drawable resources (bitmaps, nine-patches). For density-specific bitmaps, use folders like drawable-mdpi/, drawable-hdpi/, etc.
layout/ - XML files defining your app’s user interface structure (e.g., activity_main.xml). If using Jetpack Compose, UIs are defined in Kotlin files under the java directory.
mipmap-<density>/ (e.g., mipmap-hdpi, mipmap-xxxhdpi) - For density-specific app launcher icons. Modern apps often use adaptive icons.
values/ - XML files containing simple values: strings.xml (text strings), colors.xml (color definitions), dimens.xml (dimensions), styles.xml and themes.xml (UI appearance).
Other resource types like anim/, font/, menu/, raw/, xml/ can also exist here.
6Gradle Scripts
Contains build configuration files:
build.gradle.kts (or build.gradle) (Project: YourProjectName) - Top-level build file for the entire project. Configures repositories and global plugin versions.
build.gradle.kts (or build.gradle) (Module: app) - Module-level build file. Specifies SDK versions (compileSdk, minSdk, targetSdk), applicationId, versioning (versionCode, versionName), dependencies, and other build settings for the app module.
settings.gradle.kts (or settings.gradle) - Declares included modules and plugin management repositories.
gradle/libs.versions.toml (Optional but common) - Version catalog for managing dependency versions centrally.

The following sections provide a brief overview of some important application files.

The Main Activity File (Kotlin with AndroidX)

Section titled “The Main Activity File (Kotlin with AndroidX)”

The main activity code, typically MainActivity.kt (if you chose Kotlin), is where your app’s primary user interface and logic reside. It’s converted to Dalvik Executable format (.dex) and runs on the Android Runtime (ART). Here’s a default example using AndroidX libraries:

package com.example.helloworldmodern
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

And here’s the Java equivalent:

package com.example.helloworldmodern;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Key points:

  • androidx.appcompat.app.AppCompatActivity: This is a base class from the AndroidX library, providing compatibility for modern features on older Android versions.
  • onCreate(savedInstanceState: Bundle?): This is a lifecycle callback method. It’s called when the activity is first created. This is where you should perform basic application startup logic that should happen only once for the entire life of the activity.
  • setContentView(R.layout.activity_main): This links the activity to its UI layout, defined in res/layout/activity_main.xml. R is an auto-generated class that provides access to your app’s resources.

Every Android application must have an AndroidManifest.xml file in its root directory (specifically app/src/main/AndroidManifest.xml). This file presents essential information about your app to the Android build tools, the Android operating system, and Google Play. For example, a default manifest might look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworldmodern">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.HelloWorldModern">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
  • <manifest>: The root element. The package attribute here is traditionally the unique ID, though modern Gradle builds often define the applicationId in build.gradle.kts which overrides this manifest package name for packaging and Play Store purposes.
  • <application>: Declares the application. Attributes like android:icon, android:label, and android:theme define the app’s default icon, label, and theme, often referencing resources in res/mipmap/ and res/values/.
  • <activity>: Declares an activity. android:name=".MainActivity" specifies the class name (relative to the package). android:exported="true" means this activity can be launched by components of other applications (necessary for launcher activities).
  • <intent-filter>: Specifies the types of intents an activity (or other component) can respond to.
    • <action android:name="android.intent.action.MAIN" />: Declares this activity as the main entry point of the application.
    • <category android:name="android.intent.category.LAUNCHER" />: Declares that this activity should be listed in the system’s app launcher.

The manifest is also where you declare permissions, services, broadcast receivers, content providers, and other app components.

The strings.xml file (located in res/values/) centralizes all user-visible text. This is crucial for internationalization (translating your app) and makes managing text easier. Example:

<resources>
<string name="app_name">HelloWorldModern</string>
<string name="hello_message">Hello, Modern Android!</string>
</resources>

You reference these strings in your layout XML (e.g., @string/hello_message) or Kotlin/Java code (e.g., getString(R.string.hello_message)).

The activity_main.xml file in res/layout/ defines the UI for MainActivity. Modern Android development often uses ConstraintLayout for flexible and efficient layouts, though LinearLayout or FrameLayout are fine for simpler UIs. Here’s an example using ConstraintLayout to display a TextView:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_message"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • androidx.constraintlayout.widget.ConstraintLayout: A powerful and flexible layout manager. UI elements (Views like TextView) are positioned by defining constraints relative to parent or other sibling views.
  • TextView: A UI element to display text.
    • android:text="@string/hello_message": Sets the text from strings.xml.
    • app:layout_constraint...: Attributes defining how the TextView is constrained within the ConstraintLayout (here, centered).
  • tools:context=".MainActivity": Used by Android Studio’s layout editor to know which activity this layout is associated with, enabling previews and lint checks.

Alternatively, if you chose an “Empty Compose Activity”, your UI would be defined declaratively in Kotlin within your MainActivity.kt file using Jetpack Compose functions, and there might not be an activity_main.xml file.

To run your app:

  1. Select a deployment target:
    • Emulator: If you don’t have a physical Android device, create an Android Virtual Device (AVD) via Tools > AVD Manager. Choose a device definition and system image.
    • Physical Device: Connect your Android phone/tablet via USB. Enable Developer Options and USB Debugging on the device. (Search “Enable Developer Options Android” for steps).
  2. Run: Click the ‘Run’ button (green play icon) in the Android Studio toolbar, or select Run > Run ‘app’.
  3. Android Studio will build your app, install it on the selected target (emulator or device), and launch it. You should see your “Hello, Modern Android!” message.

Congratulations! You’ve developed and run your first modern Android application. Continue exploring Android’s capabilities to become a proficient developer. For further learning, consult the official Android documentation: https://developer.android.com/docs