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 declaresMainActivityas a subclass ofAppCompatActivity, the standard base class Android provides for a single screen with UI. The colon (:) is how Kotlin expresses inheritance, and the parentheses afterAppCompatActivitycall its constructor, same as you saw withopen classinheritance 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 ownonCreate()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_mainis 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.
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.