Create an Activity with Layout

## Introduction

In this tutorial, you will learn how to create a basic activity with a layout. In the process, you will create an layout file and add a TextView. Later, you will create a Java Class file, make it extend the AppCompatActivity Class, and then override its onCreate() method.

## Creating a layout

Choose a name for your activity. For the sake of this tutorial, it will be called Main, but you can name it whatever you want. Until 2016 or further, you can't add subdirectories to the layout package, so you should name your layout based on the naming conventions to make it easier to be distinguished from other files. The name format should be activity\_"ClassName".xml. For this tutorial, it will be called activity\_main.xml.

Right click your layout package, and add a new Layout Resource File. You can choose any Root element, but for this tutorial, we will use the LinearLayout, as it's simpler to use. The directory name should be "layout".

Your activity\_main.xml should look like this:

```
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout\_width="match\_parent"
 android:layout\_height="match\_parent">
 </LinearLayout>

```

We will add a TextView, so our layout won't be blank when we run our application. Create a new TextView inside the LinearLayout, set its layout\_width and layout\_height to "wrap\_content" and set it's text to "Hello World!", or anything you'd like.

```
 <TextView
 android:layout\_width="wrap\_content"
 android:layout\_height="wrap\_content"
 android:text="Hello World!" />

```

When you are finished, your layout file should look like this:

```
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout\_width="match\_parent"
 android:layout\_height="match\_parent">
 <TextView
 android:layout\_width="wrap\_content"
 android:layout\_height="wrap\_content"
 android:text="Hello World!" />
 </LinearLayout>

```

## Creating a new class

Right click a package of your application, and create a new Java Class. By naming conventions, you should call it "ClassName"Activity.java. So, for this example, it will be called MainActivity.java.

Your MainActivity.java file should look like this by now:

```
 package br.com.jeanknapp.helloworld;
 /\*\*
 \* Created by Jean on 08/10/2016.
 \*/
 public class MainActivity {

 }

```

## Importing

Import the following classes:

```
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;

```

Bundle is used to pass the savedInstanceState to the constructor. AppCompatActivity is the base class which will be extended to create our new activity. There are other base classes, but for this tutorial we will use this one.

Your MainActivity.java file should look like this by now:

```
 package br.com.jeanknapp.helloworld;

 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;

 /\*\*
 \* Created by Jean on 08/10/2016.
 \*/
 public class MainActivity {

 }

```

## Overriding the onCreate() method

We don't need much code to create a new activity, as the AppCompatActivity class already does all the job. But to be able to customize the activity and add the content we want, we must override some methods to have access to the lifecycle of the activity. In this tutorial, we will override the onCreate() method, that is called right after the activity starts. We will use this method to make the activity render our activity\_main.xml layout.

First, we need to extend the class from the base class AppCompatActivity

```
 public class BasicActivity extends AppCompatActivity {

```

Next, will override the onCreate() method. onCreate() takes Bundle savedInstanceState as a parameter. When overriding, we should call the base class method, so it will execute the standard code to create the activity, and then it will execute our code. That way, we won't need to code everything from scratch.

```
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 }

```

Finally, after the super.onCreate() call, we will set the content view to our activity\_main.xml layout.

```
 setContentView(R.layout.activity\_main);

```

After you are finished, your java class file should look like this:

```
package br.com.jeanknapp.helloworld;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

/\*\*
 \* Created by Jean on 08/10/2016.
 \*/

public class BasicActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity\_main);
 }
}

```

## Conclusion

You just create the most basic activity with layout an Android application can support. If you followed the steps correctly, you should have a blank activity with a text.

For the next steps, you should add a ToolBar, even if you are not adding any tools, as it will display the title of your activity.

You now know how to create a basic layout and make it be rendered by a basic activity.

## Next steps

* Create a Toolbar
* Create a Floating Action Button
* Create a Toast
* Create a Snackbar