Press enter to see results or esc to cancel.

Android Activity: Elements of Activity and Manifest file

Share this
()

The Android Activity we created in the previous lesson: Android Studio: The First Step in Creating Android App with our Android Studio can now be analyzed in detail. The purpose we set ourselves is to closely observe how an Android Activity is Created.

In our project, we have one Activity and that is the user interface that shows the message “Hello World“. Despite its simplicity, it highlights a fundamental aspect. To create an Activity you need to do two things:

  • Extend AppCompatActivity class, belonging to the Android framework;
  • Register the Activity in the AndroidManifest.xml using the appropriate XML tag <activity>. Among other things, this dictum applies to all four fundamental components of an application.

Lesson 7 – Android Activity – Java Code and Manifest file

MainActivity.java

The Java code that creates Android Activity resides in the src folder, as explained below. The content of a typical “Hello world” could be this:

package com.robort.coding180androidpractise1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

Below are all the elements of Android Activity file:

Android Activity elements overview.
Android Activity elements overview.
  1. Package Name: This is the unique id that we specified when we created our App. This is what identifies our app anywhere. For example, This ID is being added to our app URL on play store – https://play.google.com/store/apps/details?id=com.coding180.KotlinLanguage where “com.coding180.KotlinLanguage” is the package name to the app.
  2. Imports the needed Classes for the Activity. the example above imports Bundle and AppCompatactivity Classes.
  3. MainActivity extends AppCompatActivity. This will allow you to inherit all the attributes and methods of AppCompatActivity, Where MainActivity is the subclass and AppCompatActivity is the superclass.
  4. @Override – This annotation will override the functionality of an existing method called onCreate. It provides a specific implementation of onCreate method (which is inside our AppCompatActivity class)
  5. protected void onCreate(Bundle savedInstanceState): This onCreate method is invoked to create the Activity. In lesson 5, where we learned about android life cycle methods, you discover that this is a fundamental step in the life cycle of this type of component. Lesson 5 – Android Activity life cycle. Bundle savedInstanceState is used to access our saved bundle object which is passed into our onCreate method. For Example, Bundle can be used to save the state of our activity so we won’t lose the data when the orientation of our device is changed while working on the Activity.
  6. super.onCreate(savedInstanceState): invokes the homonymous method of the base class. This operation is absolutely mandatory;
  7. setContentView(R.layout.activity_main): Specifies what will be the “face” of the Activity, its called layout. At the moment  R.layout.activity_main may appear somewhat mysterious but it will still be for a while, until the moment in which the use of resources will be illustrated. Its function is to impose the content of the activity_main.xml file in the res/layout folder as the graphic structure of the Activity.

Assign Activity in the Manifest file

The AndroidManifest.xml file that configures this application looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.robort.coding180androidpractise1">

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>  Below are the elements of the Android manifest file.
android manifest file elements
android manifest file elements
  1. The first line specifies the XML file version and encoding type.
  2. <manifest> This is the root element that helps identify the file as a manifest file. It has “Package” attribute which describes our app package name and xmlns:android which describes the android namespace.
  3. The <application> node contains the components used in the application. Some of the commonly found attributes are:android:icon which specifies the app icon, android:label for the app name,android:theme for app theme, etc
  4. <activity> Element is the component used in our app, it has the attribute android:name which specifies the name of the Java class that embodies the Activity.
  5. The intent-filter inside Activity node serves to indicate that this activity is the main Activity of the project, in practice the interface that will welcome the user when launching the application. It helps in identifying the type of intent which activity, service or broadcast receiver can respond to. in our case, we have. Action and Category
  6. The <action android:name="android.intent.action.MAIN" /> node indicates that this Activity is the entry point of the Application, It adds an action for the intent-filter. it is also combines with <category android:name=”android.intent.category.LAUNCHER” /> in an <intent-filter> to indicate an activity that should appear in the home screen’s launcher, or in anything else that considers itself to be a launcher.

The important thing is that every activity is Created in Android Studio with the following  two steps:

  1. Create a Java class that extends AppCompatActivity.
  2. Define a <activity> node in AndroidManifest.xml that reports in the attribute android:name specifying the name of the corresponding class.

How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this