> For the complete documentation index, see [llms.txt](https://android-course.cornellappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://android-course.cornellappdev.com/archive/archived-native-android-textbook-pages/2.-views-and-layouts/2.1-file-structure.md).

# 1.1 File Structure and File Types

In general, each Activity (think of it as a unique screen displayed) in an Android app has two files associated with it: a Kotlin / Java file that manages the logic behind the screen, and an XML file which makes the visual display that is shown to the user.&#x20;

## XML Files

The XML files associated with this project are located under `app/res/layout`. At first the code might seem intimidating, but they’re not difficult to write once you understand how they are formatted. XML files contain a `ViewGroup` object that contains `View` objects inside. As you will learn in the next section, view groups set the layout or structure for how views will be placed on the screen.&#x20;

Below, is an example of what a simple XML file might look like. The majority of the code that is displayed below is auto-generated by Android Studio when you create a new XML file. What is important to note is that this file contains a layout that contains one component with a text element inside.&#x20;

{% code title="activity\_main.xml" %}

```markup
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"/>
</FrameLayout>
```

{% endcode %}

Inside this file, we can see that there is a hierarchical 'tree’ of elements. Each element has an opening tag  (`<FrameLayout ...>`) and a closing tag (`</FrameLayout>`), and the code in between these two tags are all the ‘children’ of that element. The lines of code within the opening tag represent a set of **attributes** associated with each element. Each attribute has a name (i.e. `android:text`) that is set equal to a value (i.e. “Hello World”). Attributes can help you customize the placement and styling of an element.&#x20;

![](/files/-LvZIk-voMv230F9P6AV)

Taking a look at the example above, the first two attributes of the `TextView` element set the **width** to be as large as the parent (`FrameLayout`) and the **height** to be as large as the content inside of it (the height and the width must be defined for every XML element). The last attribute sets the text of the `TextView` to be “Hello World!”.  Other attributes for this `TextView` include `android:textColor`, and `android:layout_marginLeft`  which changes how much space is to the left of the element.&#x20;

The `tools:context=".MainActivity"` attribute of the `FrameLayout` defines what class this XML file is connected to. Note the **id** attribute that we added as well—it is a vital attribute that allows us to identify and manipulate the component from the specific class defined for this XML file.&#x20;

## Class File

The complement to `activity_main.xml` is a file called `MainActivity.kt`, and this class will control all the logic associated with the screen generated by `activity_main.xml`. The code below is auto-generated when you create a new "Empty Activity" project.

{% code title="MainActivity.kt" %}

```kotlin
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}
```

{% endcode %}

The line `setContentView()` inside the `onCreate()` method allows for the XML layout we defined above to be shown when the activity is launched in the application. Method `findViewById(R.id.textView)` is a built-in method that allows users to access and add functionality to a component described in the associated XML file with a given `id`. Then, after storing the element in a local variable, we dynamically update the text to display "Hello Friend".

Having a good understanding of Kotlin files, XML files, and their interactions is a core skill necessary for Android development. In this section, we looked at an overview of XML and Kotlin files, and how to manipulate a `TextView` element through XML and through Kotlin code.&#x20;

For the rest of the chapter, we’ll be focusing on more complex elements (buttons, text inputs, etc.) that you have at your disposal.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://android-course.cornellappdev.com/archive/archived-native-android-textbook-pages/2.-views-and-layouts/2.1-file-structure.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
