[Kotlin] Application Resources and Building User Interfaces
Categories: Kotlin
Tags: User Interface Resources
📋 Here are the notes summarizing what I learned from the course!
Application Resources and Building User Interfaces
Objectives:
- Explain Android activities, fragments, and intents.
- Understand application, activity, and fragment life cycles.
- Create and use activities.
- Apply intents to call built-in applications and pass information to other activities.
- Create and use fragments.
Activities
Definition
- Activity: An application component that provides a screen for user interaction to do things like dialing the phone, taking a photo, sending an email, or viewing a map. Each activity is given a window to draw its user interface.
Creating an Activity
- To create an activity, create a subclass of
Activity
orAppCompatActivity
for compatibility support.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Registering an Activity in the Manifest
- Activities need to be registered in the Android manifest with necessary metadata like labels and themes.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Using AppCompatActivity
AppCompatActivity
provides backward compatibility for features added in newer releases of the Android platform.
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
Activity States and Lifecycle Callbacks
Lifecycle States
- Nonexistent
- Created
- Started
- Resumed
- Destroyed
Example: Lifecycle Callbacks
- Lifecycle methods like
onCreate
,onStart
,onResume
,onPause
,onStop
, andonDestroy
handle transitions between states.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(TAG, "onCreate(Bundle?) called")
}
override fun onStart() {
super.onStart()
Log.d(TAG, "onStart() called")
}
override fun onResume() {
super.onResume()
Log.d(TAG, "onResume() called")
}
override fun onPause() {
super.onPause()
Log.d(TAG, "onPause() called")
}
override fun onStop() {
super.onStop()
Log.d(TAG, "onStop() called")
}
override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "onDestroy() called")
}
}
Managing Activity Transitions with Intents
Intents
- Intents are asynchronous messages used to request an action from another app component.
Example: Starting an Activity
val intent = Intent(this, ActivityB::class.java)
startActivity(intent)
Passing Data with Intents
val intent = Intent(this, DisplayMessageActivity::class.java)
intent.putExtra("EXTRA_MESSAGE", "Hello World!")
startActivity(intent)
Fragments
Definition
- Fragment: A modular section of an activity, which has its own lifecycle and can be added or removed while the activity is running.
Example: Creating a Fragment
class AboutFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_about, container, false)
}
}
Fragment Lifecycle
States
- Fragments transition through various states in their lifecycle, similar to activities.
Example: Fragment Lifecycle Callbacks
class AboutFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(TAG, "onCreate(Bundle?) called")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.d(TAG, "onCreateView() called")
return inflater.inflate(R.layout.fragment_about, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
Log.d(TAG, "onActivityCreated() called")
}
override fun onStart() {
super.onStart()
Log.d(TAG, "onStart() called")
}
override fun onResume() {
super.onResume()
Log.d(TAG, "onResume() called")
}
override fun onPause() {
super.onPause()
Log.d(TAG, "onPause() called")
}
override fun onStop() {
super.onStop()
Log.d(TAG, "onStop() called")
}
override fun onDestroyView() {
super.onDestroyView()
Log.d(TAG, "onDestroyView() called")
}
override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "onDestroy() called")
}
override fun onDetach() {
super.onDetach()
Log.d(TAG, "onDetach() called")
}
}
Adding and Managing Fragments
Example: Adding a Fragment to an Activity
<FragmentContainerView
android:id="@+id/fragment_container_view"
android:name="com.example.android.fragments.AboutFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Android Manifest File
Definition
- The Android Manifest File contains application configuration information such as the application’s package name, components (activities, services, broadcast receivers, content providers), permissions, and hardware/software features.
Example: Registering Activities in the Manifest
<activity android:name=".MyActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Example: Using Features and Permissions
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Configuring the Gradle Build
Definition
- Gradle files define your build configuration. They include project-scoped and module-scoped build files.
Example: Adding Dependencies
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
Android UI Toolkits
Toolkits
- Android View System: Resources defined in XML.
- Jetpack Compose: Declarative API to describe the UI programmatically.
Example: Creating Layouts Using XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!" />
</LinearLayout>
RecyclerView
Definition
- RecyclerView is a ViewGroup for displaying large sets of data efficiently.
Example: Using RecyclerView
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
Example: Creating a RecyclerView Adapter
class MyRecyclerViewAdapter(private val myDataset: Array<String>) :
RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder>() {
class MyViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val textView = LayoutInflater.from(parent.context)
.inflate(R.layout.text_view, parent, false) as TextView
return MyViewHolder(textView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = myDataset[position]
}
override fun getItemCount() = myDataset.size
}
Example: Setting up RecyclerView in an Activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = MyRecyclerViewAdapter(arrayOf("Item 1", "Item 2", "Item 3"))
}
}
Declarative Data Binding
Definition
- The Data Binding library allows you to bind UI components in your layouts to data sources in your app using a declarative format.
Example: Enabling Data Binding
android {
buildFeatures {
dataBinding true
}
}
Example: Using Data Binding in a Layout
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.example.User" />
</data>
<LinearLayout
android:orientation="vertical"
android:
layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}" />
</LinearLayout>
</layout>
Example: Setting up Data Binding in an Activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val user = User("John Doe")
binding.user = user
}
}
References
- Android Developers: Activities
- Android Developers: Fragments
- Kotlin Android Training: Creating and Adding a Fragment
Leave a comment