Difference Between Basic Activity And Empty Activity In Android Studio

When you create android project in android studio, there are a lot of activity templates you can choose which provided by android studio. For android beginners, basic activity and empty activity always make them confused. So what the difference between them, let me tell you now.

1. Create Basic Activity In Android Project.

  1. First you should create an android project. You can read article How To Create New Android Studio Project to learn how to do it if you do not know. You can also input the new project information like below picture.
    create android project
  2. During the project creation process, in Add an Activity to Mobile wizard select Basic Activity template. Then click Next button.
    android blank activity and empty activity
  3. Just click next button in all after wizard and use all default values until complete. Then you can see the basic activity template wizard created files and folders listed in left Project panel.
    basic activity template created files

2. Basic Activity Template Files Introduction.

From above picture we can see that basic activity template create a lot of template files, now we will introduce the most important files below.

2.1 Main Activity Java File.

Double click the MainActivity java class under app / java folder to open it. Please note below java code in onCreate method.

setContentView(R.layout.activity_main);

Above code means this activity will use the layout xml file which name is activity_main. You can find the xml file under app / res / layout folder.

2.2 Main Activity Layout Xml File.

Also double click app / res / layout / activity_main.xml file to open it. You can see that it has already contains some android components definition xml.

<!-- AppBarLayout is used to wrap Toolbar to avoid Toolbar is overlapped by other view components.-->
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <!-- This is the Toolbar-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<!-- Include the main content layout xml file of this application. -->
<include layout="@layout/content_main" />

<!-- This is the floating action button which float over the screen. -->
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

At the top of the layout xml is a Toolbar wrapped by AppBarLayout. AppBarLayout is used to wrap Toolbar to avoid Toolbar is overlapped by other view components when screen scroll. Please refer article Android AppBarLayout And Toolbar ExampleAndroid Material Design Toolbar Example.

READ :   Android ActionBar Tab Navigation Example

After that is a include xml tag. This tag will include the activity content layout xml file which is named content_main.xml. This xml file is also saved in app / res / layout folder.

At the layout file bottom is a floating action button. This button is introduced in android material design. It is a button floating over the screen. You can read How To Avoid Snackbar Overlap Floating Action Button In Android for detail introduction.

2.3 Activity Content Layout Xml File.

Now open content_main.xml file under app / res / layout folder, this file is the place where you design your app UI. The template content is too simple, there is only one TextView component in it.

Click Design tab under it, you can drag and drop a lot of android ui components into it visually. You can also click the Text tab at bottom to see the layout xml code.

In the xml designer Palette area, all android ui components is classified by type, click each type will list the components under it. Under the Palette is Component Tree, that list all this app’s ui components in a tree, you can easily adjust the ui components order and relationship in it.

android layout xml file designer

There is also a toolbar in the android layout xml file designer. Use this toolbar you can look and feel your design instantly with following actions.

android layout designer toolbar

  1. Change the screen direction to Portrait or Landscape.
  2. Show your design effect in multiple devices.
    change layout xml display device
  3. Check ui effect by changing android API version.
    change android api version to check layout effect
  4. Change the activity theme. The activity theme decide how this activity looks and feel. It is used in the application AndroidManifest.xml file, we will introduce it later.
    change android app theme to see layout effect
  5. Change app language.
READ :   How To Fix Unable To Access Android Sdk Add-on List When Run Android Studio For The First Time

3. Create Empty Activity.

  1. Right click the java package, click New —> Activity —> Empty Activity menu item in popup menu list.
    create new android activity
  2. Just click Finish button and use all the default values. When the process complete, you can see that there is only one activity java file and one layout xml file.

Now we can tell that the difference between basic activity and empty activity is basic activity create a best practiced standard code skeleton for android developer but empty activity just create an activity java and layout xml file.

4. The Android Application Manifest File.

All the activities is just act as components in android app, one android app can contains multiple activities. But how to assemble all these activities together to make a complete android app. We use AndroidManifest.xml file to do this.

This file is so important for android app, it is saved in app / manifests folder.

android manifest xml
Open this file you can see below xml code. From the xml code, we can get below results.
  1. This android application contains two activity.
  2. Application and activity can use different theme. The theme are defined in app / res / values / styles.xml file.
  3. Application and activity label is string variable defined in app / res / values / strings.xml file. This is a good habit than hard coding string value in source code, this can make your app internationalization.
  4. App icon value is also reference a variable defined in app / res / mipmap folder.
  5. Specify MainActivity as the launcher activity by intent-filter category value.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dev2qa.android.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity"></activity>
    </application>

</manifest>

 

2 Comments


  1. So, what does an empty Activity look like?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.