Skip to content

Android Activity with Example in Kotlin

  • by

Android Activity: is the main component of Android applications. The activity gives user interaction, you can set UI  setContentView(View) in activity to show the user interface. Like when you open mail apps (Gmail etc) you will see the list of emails. That is showing in a list of activities or using other components.

What is Android-Activity-with-Example-Kotlin

There are some methods have to override in Android activity:

  • onCreate(Bundle) for initializing activity and set the layout resource file and Using findViewById(int) you have to interact with UI widgets like TextView, Button, CheckBox, RadioButton etc.
  • onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed.

Not only 2 methods there is many callback method , which handles  life cycle of Activity: onStart() , onResume(), onStop(), onDestroy(), onRestart() etc.

Must Read About Multiple Activity:

Many apps have multiple screens, so every screen required a user interface, where a user can interact with the app. So, in this case, we have to use multiple activity or fragments can be also an option but its totally different concept you can read here: Android Fragment with an example in Kotlin

Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. It should be defined in AndroidManifest.xml with <IntentFilter>Action” and “category” to know android system your launching activity.

<manifest ...>
    <application ...>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />

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


Each activity can start another activity in order to perform different actions or show.

For example, the main activity has mails list (inbox), so on click any mail you have to show another user interface (Activity have UI). That can be performed by Intent to go form on activity to another with some data.

To use activities in your app, you must register information about them in the app’s Android manifest XML, and you must manage activity lifecycles appropriately.

Configuring the manifest: Declare activities

To declare your activity, open your manifest file and add an <activity> element as a child of the  <application> element. For example:

<manifest ... >
  <application ... >
      <activity android:name=".TestActivity" />
  </application ... >
</manifest >

Enough theory, let’s start Practical :

This example demonstrates activity with Kotlin…

Step 1. Create new project “Build Your First Android App in Kotlin
Step 2. Open project structure and see how activity relate in Android app
Android Activity with Example in Kotlin tutorial

You can see the code of MainActivity have set view “activity_main.xml” and MainActivity has declared in AndroidManifest.xml.

Source codes

MainActivity.kt :

activity_main.xml :…/activity_main.xml

AndroidManifest.xml :…/AndroidManifest.xml

Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)



Coding in Kotlin

Leave a Reply

Your email address will not be published. Required fields are marked *