How to Change Application’s Starting Activity in Android?
Many times while building an android application. We have to change the default activity of our application to another activity that we have created within our android studio project. So that we can set that custom activity to the starting activity of our application. In this article, we will take a look at the How to Change Applications starting activity in our android studio project.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/idRLContainer" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!--on below line we are creating a text view for heading--> < TextView android:id = "@+id/idTVHeading" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:layout_margin = "20dp" android:gravity = "center" android:padding = "10dp" android:text = "Main Activity" android:textAlignment = "center" android:textColor = "@color/black" android:textSize = "20sp" android:textStyle = "bold" /> </ RelativeLayout > |
Step 3: Create a new empty activity.
Navigate to app>java>your app’s package name>Right click on it>New>Empty activity and name it as MainActivity2.
Step 4: Working with activity_main2.xml
Navigate to app>res>layout>activity_main2.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity2" > <!--on below line we are creating a text for our app--> < TextView android:id = "@+id/idTVHeading" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:layout_margin = "20dp" android:gravity = "center" android:padding = "10dp" android:text = "New Activity\n Main 2 Activity" android:textAlignment = "center" android:textColor = "@color/black" android:textSize = "20sp" android:textStyle = "bold" /> </ RelativeLayout > |
Step 5: Updating our AndroidManifest.xml
Navigate to app>AndroidManifest.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.gtappdevelopers.kotlingfgproject" > < 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.KotlinGFGProject" android:usesCleartextTraffic = "true" > <!--on below line we are specifying our main activity--> < activity android:name = ".MainActivity" /> <!--on below line we are specifying our main 2 activity--> < activity android:name = ".MainActivity2" > <!--on below line we are setting main activity 2 as a launcher activity on below line--> < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > </ manifest > |
Now run your application to see the output of it.
Output:
Contact Us