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:

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.

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

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:

Importing
Import the following classes:

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:

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

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.

Finally, after the super.onCreate call, we will set the content view to our activity_main.xml layout.

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

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