Set Up a Default Activity for an Android Application in Android Studio

Anurag Kandulna
The Startup
Published in
4 min readJul 24, 2020
Credits: Photo by Deyvi Romero from Pexels

Learning Android Application Development from scratch can be tricky sometimes for a newbie like myself. I am assuming that you are equipped with a complete Android Studio setup and have a fair idea about Android Activity and Markup language.

Let us create a Android Project with Empty Activity and follow the process. On completion, we are presented with two files:
1. MainActivity.java (Red box in Fig 1)
2. activity_main.xml (Green box in Fig 1)
Just for curiosity, we delete both the files from the project folder, and now the project is broken.

Through this post, we’ll try to fix what’s broken and set up a new default activity without starting from scratch.

Fig 1: Default Layout before files were deleted

Before we go into the actual process of setting up the default activity, let’s look at how we can create an activity. It can either be created from the File tab section (purple box on the top left of Fig 1) or Project Manager window (blue box, i.e., the leftmost window of Fig 1). Given below are the two ways to create a new activity:

  1. Go to File tab > New > Activity > Empty Activity
  2. Right Click on App module > New > Activity > Empty Activity
Fig 2.1: Create new activity from File tab
Fig 2.2: Create new activity from Project Manager window

We will work with Empty Activity(named New Main Activity) in this post.

Once an Activity is selected, a new window will pop up, asking us to Configure the Activity we have chosen previously. Fill up the details as required and do check the check-boxes saying Generate a Layout File and Launcher Activity (Fig 3).

Fig 3: New Activity configuration window
Fig 4: Build successful

If everything is done correctly, the app will build successfully & run without any errors. We can go back to building our cool application. (Fig 4)

Fig 5: Unchecked check-boxes of Configure Activity window

Otherwise, if the check-boxes are left unchecked (Fig 5), the new activity will be generated. But it won’t run because Default Activity is not set (Fig 6).

Fig 6: Unsuccessful build

We will first create a layout file and then update the Android Manifest file to fix this issue.

Step 1: Create a layout file

To create a layout file go to File > New > XML > Layout XML File (Fig 7.1)

Fig 7.1: Create a new layout file

In the layout configuration window, the Root Tag field can be filled as Linear Layout, Relative Layout, etc. (Root Tag can also be changed later)

Fig 7.2: Layout configuration window

Step 2: Configure Android Manifest file

To open the Android Manifest file go to the Project Manager window (brown box in Fig 1) and follow the file hierarchy (Fig 8):
App > manifests > AndroidManifest.xml

Fig 8: Android Manifest file location

In the XML file you will see activity tag (Fig 9):
<activity android:name= ”.NewMainActivity”></activity>

If there is no code within the activity tag that means that particular activity (.NewMainActivity in our case) is not set as Default Activity.

Fig 9: Android Manifest file code view

Add the following markup code within the activity tag to make it a Default Activity (Fig 10):

<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
Fig 10: Updated Manifest file

Now, build the application and run it. It will run successfully (Fig 11).

Fig 11: Successful build and Run

Similarly, one can edit any activity tag in the AndroidManifest.xml file to make it a Default activity.

--

--