MainActivity.kt
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.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
textView.text = "Configuration Changed."
val orientation = when(newConfig.orientation){
Configuration.ORIENTATION_PORTRAIT -> "Portrait"
Configuration.ORIENTATION_LANDSCAPE -> "Landscape"
Configuration.ORIENTATION_UNDEFINED -> "Undefined"
else -> "Error"
}
textView.append("\n\nOrientation : $orientation")
}
}
activity_main.xml
<?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" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidMenifest.xml [part]
<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 - Get screen orientation programmatically
- android kotlin - Get screen width and height in dp
- android kotlin - Get screen density programmatically
- android kotlin - Convert pixels to dp programmatically
- android kotlin - Get screen size programmatically
- android kotlin - RecyclerView animation
- android kotlin - RecyclerView smooth scroll
- android kotlin - RecyclerView add remove item
- android kotlin - RecyclerView GridLayoutManager example
- android kotlin - RecyclerView StaggeredGridLayoutManager example
- android kotlin - GridView selected item background color
- android kotlin - Border/divider between GridView items
- android kotlin - GridView OnItemClickListener
- android kotlin - Set CardView corner radius
- android kotlin - RadioButton with image and text