How to Create an Activity in Android: Step-by-Step Guide
To create an activity in Android, define a new class that extends
AppCompatActivity and override the onCreate() method to set the UI layout with setContentView(). Then, register this activity in the AndroidManifest.xml file to make it part of your app.Syntax
An Android activity is a single screen in your app. To create one, you write a class that extends AppCompatActivity. Inside, override the onCreate() method to set the layout using setContentView(R.layout.your_layout). Finally, declare the activity in AndroidManifest.xml so the system knows about it.
- class YourActivity extends AppCompatActivity: Defines the activity class.
- onCreate(Bundle savedInstanceState): Called when activity starts.
- setContentView(): Sets the UI layout.
- AndroidManifest.xml: Registers the activity.
java
public class YourActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_your); } } <!-- In AndroidManifest.xml --> <activity android:name=".YourActivity" />
Example
This example shows a simple activity named MainActivity that displays a layout called activity_main.xml. It also shows how to register it in the manifest file.
java
package com.example.myapp; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } <!-- AndroidManifest.xml --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <application android:allowBackup="true" android:label="MyApp" android:supportsRtl="true"> <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>
Output
App launches showing the UI defined in activity_main.xml layout.
Common Pitfalls
Common mistakes when creating activities include:
- Forgetting to register the activity in
AndroidManifest.xml, causing the app to crash when starting it. - Not calling
super.onCreate(savedInstanceState)insideonCreate(), which can break the activity lifecycle. - Using the wrong layout resource ID in
setContentView(), leading to blank screens or errors.
java
/* Wrong: Missing super call */ @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); } /* Right: Include super call */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
Quick Reference
Remember these key steps to create an activity:
- Create a class extending
AppCompatActivity. - Override
onCreate()and callsuper.onCreate(). - Set the UI with
setContentView(). - Register the activity in
AndroidManifest.xml. - Use correct layout resource IDs.
Key Takeaways
Create an activity by extending AppCompatActivity and overriding onCreate().
Always call super.onCreate() before setting the content view.
Register your activity in AndroidManifest.xml to avoid runtime errors.
Use setContentView() with the correct layout resource to display UI.
Check for common mistakes like missing manifest entries or super calls.