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.
- 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.
- During the project creation process, in Add an Activity to Mobile wizard select Basic Activity template. Then click Next button.
- 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.
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.
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 Example. Android Material Design Toolbar 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.
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.
- Change the screen direction to Portrait or Landscape.
- Show your design effect in multiple devices.
- Check ui effect by changing android API version.
- 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 app language.
3. Create Empty Activity.
- Right click the java package, click New —> Activity —> Empty Activity menu item in popup menu list.
- 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.
- This android application contains two activity.
- Application and activity can use different theme. The theme are defined in app / res / values / styles.xml file.
- 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.
- App icon value is also reference a variable defined in app / res / mipmap folder.
- 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>