Theory
Two files for one screen
Your FestConnect welcome screen needs to look a certain way (a title, a Register button) and do things (react when the button is tapped). Android deliberately splits those two jobs into two files.
The Activity (a Java class) is the screen's brain: its behaviour. The Layout (an XML file) is the screen's face: its appearance. One screen, two files, joined by a single line of code. This split is the first big idea of Android UI.
Theory
The stage and the stage manager
Think of a play. The set (painted backdrop, furniture placement) is designed and described separately: that is the XML layout. The stage manager who cues the action, responds to events, and runs the show is the Activity in Java. The set does not act; the manager does not paint scenery. Keeping them apart lets each be changed without disturbing the other.
Theory
Activity and Layout, formally
- An Activity is one screen of the app, a Java class extending
AppCompatActivity. - Its lifecycle begins at `onCreate()`, called when the screen is created.
- Inside onCreate you call `setContentView(R.layout.activity_main)` to attach a layout.
- A Layout is an XML file in
res/layoutdescribing the UI: containers (LinearLayout) and widgets (TextView,Button).
Look lives in XML; behaviour lives in Java. That is separation of concerns.
Practical
activity_main.xml (the look)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to FestConnect" />
</LinearLayout>Practical
MainActivity.java (the behaviour)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// attach the XML layout to THIS screen
setContentView(R.layout.activity_main);
}
}Think first
What joins the two files?
You have activity_main.xml (the layout) and MainActivity.java (the Activity). Which single line makes the Java screen actually display that XML, and where does it go?
Show the answer
The line setContentView(R.layout.activity_main);, placed inside onCreate() after super.onCreate(...). R.layout.activity_main is a generated reference to your XML file, and setContentView inflates it onto the screen. Without that line, the Activity runs but shows a blank screen, because nothing told it which layout to display. onCreate is where a screen wires up its appearance.
Quiz
In Android, which statement about Activity and Layout is correct?
- An Activity is a screen written in Java; a Layout is its UI written in XML
- An Activity is the XML file; a Layout is the Java class
- Both the Activity and Layout are written in XML
- One Activity must contain many unrelated screens at once
Show the answer
An Activity is a screen written in Java; a Layout is its UI written in XML
An Activity is a single screen implemented as a Java class, and a Layout is that screen's UI described in XML (A). Option B swaps them. The Activity is Java, not XML (C). And an Activity represents one focused screen, not many unrelated ones crammed together (D). The clean split, Java behaviour vs XML appearance, is exactly what this lesson establishes.
Watch out
Activity/Layout traps
1. Forgetting setContentView: the app runs but the screen is blank. Always attach a layout in onCreate.
2. Mixing up which is which: Activity = Java (behaviour), Layout = XML (appearance).
3. Putting logic in the XML: XML only describes the UI. Button clicks and data handling belong in the Java Activity (or via an onClick binding), never as logic inside the layout file.
Theory
Next: the whole project, and the glue
You have seen the two files of one screen. The next lesson creates the full FestConnect project in Android Studio, runs 'Welcome' on the emulator, and tours the other components that make it an app, including the AndroidManifest.xml and the auto-generated R.java that turns @+id/title into that R.layout reference you just used.
Summary
Key takeaways
- An Activity is one screen of the app, written as a Java class (extends AppCompatActivity).
- A Layout is that screen's UI, written in XML in res/layout (containers + widgets).
- onCreate() is the Activity's entry point; setContentView(R.layout.activity_main) attaches the layout.
- Appearance lives in XML, behaviour in Java: separation of concerns.
- Forgetting setContentView leaves the screen blank.
- Memory hook: the set (XML layout) and the stage manager (Java Activity).