Overview

In this section, we will introduce you to Android development using Kotlin. Kotlin is now the preferred language for Android development due to its concise syntax, safety features, and full interoperability with Java. By the end of this module, you will have a solid understanding of the basics of Android development and be ready to build your first Android app.

Key Concepts

  1. Android Studio: The official Integrated Development Environment (IDE) for Android development.
  2. Android Project Structure: Understanding the different components of an Android project.
  3. Activity Lifecycle: The different states an activity goes through during its lifecycle.
  4. Layouts and Views: Designing user interfaces using XML and Kotlin.
  5. Intents: Navigating between different screens and passing data.

Setting Up Android Studio

Step-by-Step Guide

  1. Download and Install Android Studio:

  2. Configure Android Studio:

    • Open Android Studio.
    • Follow the setup wizard to install the necessary SDK components.
  3. Create a New Project:

    • Open Android Studio and select "Start a new Android Studio project".
    • Choose a project template (e.g., "Empty Activity").
    • Configure your project (name, package name, save location, language as Kotlin, and minimum API level).
    • Click "Finish" to create the project.

Understanding the Android Project Structure

Key Components

  • Manifest File (AndroidManifest.xml): Declares essential information about your app, such as its components and permissions.
  • Java/Kotlin Directory: Contains your Kotlin source files.
  • Res Directory: Contains resources like layouts, strings, and images.
  • Gradle Scripts: Build configuration files.

Example Project Structure

MyApplication/
├── app/
│   ├── src/
│   │   ├── main/
│   │   │   ├── java/com/example/myapplication/
│   │   │   │   └── MainActivity.kt
│   │   │   ├── res/
│   │   │   │   ├── layout/
│   │   │   │   │   └── activity_main.xml
│   │   │   │   ├── values/
│   │   │   │   │   └── strings.xml
│   │   │   └── AndroidManifest.xml
│   └── build.gradle
└── build.gradle

Activity Lifecycle

Key Lifecycle Methods

  • onCreate(): Called when the activity is first created.
  • onStart(): Called when the activity becomes visible to the user.
  • onResume(): Called when the activity starts interacting with the user.
  • onPause(): Called when the activity is partially obscured by another activity.
  • onStop(): Called when the activity is no longer visible to the user.
  • onDestroy(): Called before the activity is destroyed.

Example Code

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("MainActivity", "onCreate called")
    }

    override fun onStart() {
        super.onStart()
        Log.d("MainActivity", "onStart called")
    }

    override fun onResume() {
        super.onResume()
        Log.d("MainActivity", "onResume called")
    }

    override fun onPause() {
        super.onPause()
        Log.d("MainActivity", "onPause called")
    }

    override fun onStop() {
        super.onStop()
        Log.d("MainActivity", "onStop called")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("MainActivity", "onDestroy called")
    }
}

Layouts and Views

Designing User Interfaces

  • XML Layouts: Define the UI structure using XML.
  • Views: UI components like TextView, Button, etc.
  • ViewGroups: Containers that hold other views (e.g., LinearLayout, RelativeLayout).

Example XML Layout

<!-- res/layout/activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />
</LinearLayout>

Example Kotlin Code to Interact with Views

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textView: TextView = findViewById(R.id.textView)
        val button: Button = findViewById(R.id.button)

        button.setOnClickListener {
            textView.text = "Button Clicked!"
        }
    }
}

Intents

Navigating Between Activities

  • Explicit Intents: Directly specify the target activity.
  • Implicit Intents: Specify the action to be performed, and the system determines the appropriate activity.

Example of Explicit Intent

// MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button: Button = findViewById(R.id.button)
        button.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
    }
}

// SecondActivity.kt
class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
    }
}

Practical Exercise

Task

Create a simple Android app with two activities. The first activity should have a button that navigates to the second activity when clicked. The second activity should display a message.

Solution

  1. Create the Project:

    • Follow the steps to create a new project in Android Studio.
  2. Design the Layouts:

    • activity_main.xml:
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="16dp">
      
          <Button
              android:id="@+id/button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Go to Second Activity" />
      </LinearLayout>
      
    • activity_second.xml:
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="16dp">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Welcome to the Second Activity!" />
      </LinearLayout>
      
  3. Implement the Activities:

    • MainActivity.kt:
      class MainActivity : AppCompatActivity() {
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_main)
      
              val button: Button = findViewById(R.id.button)
              button.setOnClickListener {
                  val intent = Intent(this, SecondActivity::class.java)
                  startActivity(intent)
              }
          }
      
    • SecondActivity.kt:
      class SecondActivity : AppCompatActivity() {
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_second)
          }
      }
      

Conclusion

In this section, you learned the basics of Android development with Kotlin, including setting up Android Studio, understanding the project structure, the activity lifecycle, designing layouts, and navigating between activities using intents. With this foundation, you are now ready to dive deeper into building more complex Android applications. In the next section, we will explore building user interfaces in more detail.

© Copyright 2024. All rights reserved