How to add a new activity in android, new activity android, android create new activity

How to add a new activity in android

An activity is a screen in android that allows applications to have its user interface and also add functions of the defined interface. In android, an activity contains an XML file that holds the user interface and a java or kotlin file where functionalities are added.

By default, when you create a project in the android studio an activity called Main Activity is created which has activity_main XML file and MainActivity.kt or MainActivity.java depending on the language you have selected to use.

Unless your application is a test application and does not involve any interactions, all the applications contain more than one activity. To know the number of activities that are present in an app, you can easily identify them in the android manifest file since all of them are added there

For example, in the below android manifest file there is only 1 activity. An activity in manifest file starts with <activity> tag and ends with </activity> tag

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.checkinternet">

 

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-permission android:name="android.permission.INTERNET"/>

    <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/Theme.CheckInternet">

        <!-- activity 1 starts here       -->

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

     <!--        activity 1 ends here-->

    </application>

 

</manifest>

The reason why you need to add a new activity in android is determined by the purpose the activity will serve. For example, in a login application when the user is authenticated, you want the user to be directed to another page which will require creating a new activity

In this article, we shall demonstrate the steps followed when adding a new activity in android using the kotlin language.

  • To add a new activity in android, there must be an existing project created that is already in use
  • On the top right of the current project, right-click app. A new floating interface will appear, hover on New and another floating interface will appear, scroll down to the new floating interface and hover on activity
  • On the new floating interface that appears, click on Empty Activity

How to add a new activity in android

  • A modal will appear where you will need to configure the name of the activity and the language you want to use. The name, in this case, is MainActivity2, we will leave it like that for now but you can rename it to what is easier for you to remember.

How to add a new activity in android

  • Click on finish
  • Wait for the activity to be set up and Gradle to finish on sync
  • At the project left sidebar, the activity you have created will be visible as shown below

How to add a new activity in android

  • Two new files will be created, activity_main2.xml which is the layout file and MainActivity2.kt since we are using kotlin as our development language

In the android manifest file, you will also note the activity we have created is has also been added

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.checkinternet">

 

    <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/Theme.CheckInternet">

        <!--        activity 2 starts here-->

        <activity android:name=".MainActivity2">

 

        </activity>

        <!--        activity 2 ends here-->

        <!-- activity 1 starts here -->

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

    <uses-permission android:name="android.permission.INTERNET" />

 

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 

</manifest>

Now you can see that there are 2 activities in our project

You can also change the launcher activity in android that is, changing which activity will be loaded by default when you open your app. This is common when for example you have created a splash screen in android later after adding the main functionalities to the first activities.

To change the launcher activity for your app, you will need to cut the code between <intent filter> tags and transfer or paste it to the activity you want to set as the starting activity. The full demonstration is found in this article of how to change launcher activity in android studio

 

How to connect two activities in android

To move from one activity to another in android, we use intents as shown below. Changing of activities is mostly triggered by a click either button click on link click

To move from activity 1 to activity 2 after clicking a button, we use the following code

button1.setOnClickListener {

            val intent= Intent(this, MainActivity2::class.java)

            startActivity(intent);

        }

How to use open a new activity is explained in detail in this article how to open a new activity in android using intents

Conclusion

In the above discussion, we have defined what an activity is and what it does, we have also shown how to create a new activity in android from an existing project.

Also, we have highlighted how you can view the number of activities that are there in a project using the android manifest file

We have also shown how you can change the launcher activity in android from the current default launcher activity to another activity of choice

Finally, we have demonstrated how you can navigate from one activity to another by use of a button click using intents.

That’s it for this article, hope you have learned and know how to add a new activity in android using the steps we have outlined.