Your First App

Time to put everything so far to work. In this lesson we'll build the smallest real screen TrailLog can have: a single screen that simply displays "Welcome to TrailLog" in a text label. It's intentionally simple — the goal is to see your whole toolchain (Kotlin, the layout system, and the emulator) working together end to end before we start adding real features.

The Generated MainActivity.kt

When you created the TrailLog project with the Empty Views Activity template back in Install Android Studio, Android Studio generated a starting MainActivity.kt file for you. Let's look at a trimmed-down version of it and understand exactly what each part does:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

A few things worth understanding line by line:

  • class MainActivity : AppCompatActivity() — this declares MainActivity as a subclass of AppCompatActivity, the standard base class Android provides for a single screen with UI. The colon (:) is how Kotlin expresses inheritance, and the parentheses after AppCompatActivity call its constructor, same as you saw with open class inheritance in the previous lesson.
  • override fun onCreate(...) — onCreate() is a lifecycle method that the Android system calls automatically when this screen is first created. We're overriding it here to run our own setup code. This function returns in much more depth in the upcoming Activity Lifecycle lesson — for now, just know it's where a screen's initial setup belongs.
  • super.onCreate(savedInstanceState) — calls the parent class's own onCreate() first, which handles important internal setup. Always call this before your own logic.
  • setContentView(R.layout.activity_main) — tells this Activity which XML layout file to inflate and display as its UI. R.layout.activity_main is a generated reference to the layout file we'll look at next.

The activity_main.xml Layout

The layout referenced by setContentView() lives at app/src/main/res/layout/activity_main.xml. Here's a minimal version containing exactly one TextView showing our welcome message:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/welcomeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to TrailLog"
        android:textSize="20sp" />

</LinearLayout>

The outer LinearLayout is a container that stacks its children in one direction — here, vertical — and android:gravity="center" centers its content both horizontally and vertically on screen. The TextView inside it is the actual visible label, with its text set directly via android:text.

How MainActivity.kt and activity_main.xml Connect

The connection between these two files is exactly the single line setContentView(R.layout.activity_main) from onCreate(). When the Activity is created, this line inflates the XML layout — turning the markup into actual on-screen views — and displays it as this screen's content. Every property we set in the XML (the text, the size, the layout dimensions) shows up immediately once this line runs.

Running It on the Emulator

With both files in place (they're already there if you used the Empty Views Activity template — you don't need to type them from scratch), select your AVD from the device dropdown and click Run. Android Studio builds TrailLog, installs it on the emulator, and launches it. Within a few seconds you should see a plain white screen with "Welcome to TrailLog" centered on it.

Note: This site's own project doesn't run a live Android Studio build, so this code is a correct, ready-to-use static example rather than something executing on this page — copy it into your own TrailLog project in Android Studio to see it run.

If Something Goes Wrong: Logcat

If the app crashes on launch or doesn't behave as expected, Android Studio's Logcat panel (usually docked at the bottom of the IDE) shows a live stream of everything the app and the system are logging, including full stack traces for crashes. We'll cover reading and filtering Logcat properly in the Logging & Debugging Tools lesson later in this track — for now, just know it's the first place to look if this simple screen doesn't show up.

You just built and ran a real Android app. It's a simple one, but every piece — an Activity subclass, a layout file, and the line connecting them — is exactly the same pattern every screen in TrailLog will use, no matter how complex it eventually gets. From here, it's about adding more views, more logic, and more screens on top of a foundation you now understand.
Next up: Continue to Project Structure to understand exactly what the rest of the generated project folders and files are for.