Introduction
This code demonstrates how to handle screen orientation changes in an Android application written in Kotlin. By default, when a device is rotated, the activity restarts, potentially causing data loss and a disjointed user experience. This example shows how to prevent the activity from restarting and instead update the UI to reflect the new orientation.
Breakdown of the Code
MainActivity.kt:
onCreate: This lifecycle method is called when the activity is first created. Here, the layout (
activity_main.xml
) is inflated and a random number is generated and displayed ontextView
.onConfigurationChanged: This method is called whenever the device configuration changes, including screen orientation.
- It updates
textView2
to indicate a configuration change. - It retrieves the current orientation using
newConfig.orientation
and displays a corresponding message ("Portrait", "Landscape", etc.) ontextView2
.
- It updates
activity_main.xml:
This file defines the layout of the activity. It contains two TextViews:
textView
: Displays a randomly generated number.textView2
: Used to display messages about the configuration change and the current orientation.
AndroidManifest.xml (partial):
The relevant part of the manifest file specifies that the activity should handle orientation and screen size changes. This is achieved by adding the line android:configChanges="orientation|screenSize"
within the <activity>
tag for MainActivity
.
Summary
By combining the information from the layout file, activity code, and manifest, this example demonstrates how to gracefully handle screen orientation changes in an Android application. The activity avoids restarting by using the onConfigurationChanged
method to update the UI based on the new orientation. This approach helps maintain a smooth and responsive user experience regardless of device rotation.
package com.example.jetpack
import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val random = Random.nextInt(50)
textView.text = "\n\nRandom Number : $random"
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
textView2.text = "Configuration Changed."
val orientation = when(newConfig.orientation){
Configuration.ORIENTATION_PORTRAIT -> "Portrait"
Configuration.ORIENTATION_LANDSCAPE -> "Landscape"
Configuration.ORIENTATION_UNDEFINED -> "Undefined"
else -> "Error"
}
textView2.append("\nOrientation : $orientation")
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#E5E4E2">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
tools:text="TextView"
android:gravity="center"
android:fontFamily="sans-serif-condensed"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:textSize="30sp"
android:textColor="#1C05B3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
<!--
android:configChanges="orientation|screenSize"
this prevent restarting the activity when screen orientation change.
-->
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
- android kotlin - EditText space validation
- android kotlin - Show soft keyboard when EditText is focused
- android kotlin - EditText hide error
- android kotlin - EditText remove underline while typing
- android kotlin - EditText change cursor color programmatically
- android kotlin - Create ImageView programmatically
- android kotlin - ImageView rounded corners transparent
- android kotlin - ImageView set image from Uri
- android kotlin - Get battery capacity programmatically
- android kotlin - Repeat a task periodically
- android kotlin - Do a task after a delay
- android kotlin - Get app first install time
- android kotlin - Get app version name and code
- android kotlin - Enable disable bluetooth programmatically
- android kotlin - Change screen orientation programmatically