Articles → Android → Create An Empty Activity In Android Studio
Create An Empty Activity In Android Studio
Activity
Create An Empty Activity
- Right click on your package and click on ‘New’ → ‘Activity’ → ‘Empty Activity’.
Click to Enlarge
- A popup window will appear to enter the activity name. Enter the activity name and click on ‘Finish’ button.
Click to Enlarge
Explaining Files Created In Empty Activity
- Activity_main2.xml – This is the file where you can create UI elements. For now this file only contains root tag and namespaces.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
</androidx.constraintlayout.widget.ConstraintLayout>
- Main2activity.java – In this file you can write the validations, business logic and database layer.
package com.example.insuranceapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
- The class is inherited from AppCompatActivity class. This is the base class of all activities.
- onCreate method is overridden.
- setContentView specifies the associated XML file.