Articles → Android → Create an empty activity in android studio

Create an empty activity in android studio






Activity





Create an empty activity






  1. Right click on your package and click on ‘New’ → ‘Activity’ → ‘Empty Activity’.
  2. Picture showing the Empty Activity option in the New menu of the android studio

    Click to Enlarge

  3. A popup window will appear to enter the activity name. Enter the activity name and click on ‘Finish’ button.
  4. Picture showing the popup window to enter the activity name

    Click to Enlarge

Explaining files created in empty activity




  1. Activity_main2.xml – This is the file where you can create UI elements. For now this file only contains root tag and namespaces.
  2. <?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>


  3. Main2activity.java – In this file you can write the validations, business logic and database layer.
  4. 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);
      }
    }




  1. The class is inherited from AppCompatActivity class. This is the base class of all activities.
  2. onCreate method is overridden.
  3. setContentView specifies the associated XML file.



Posted By  -  Karan Gupta
 
Posted On  -  Monday, September 2, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250