MainActivity.kt
package com.example.jetpack
import android.content.pm.ActivityInfo
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)
btnPortrait.setOnClickListener {
// set screen orientation to portrait
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
btnLandscape.setOnClickListener {
// set screen orientation to landscape
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
}
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("\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">
<Button
android:id="@+id/btnPortrait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Portrait"
android:backgroundTint="#545AA7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnLandscape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Landscape"
android:backgroundTint="#C32148"
app:layout_constraintBottom_toBottomOf="@+id/btnPortrait"
app:layout_constraintStart_toEndOf="@+id/btnPortrait" />
<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"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:textSize="30sp"
android:textColor="#191970"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnPortrait"
tools:text="TextView" />
</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 - Create ImageView programmatically
- android kotlin - ImageView set image from assets
- android kotlin - ImageView border shadow
- android kotlin - ImageView tint programmatically
- android kotlin - ImageView rounded top corners programmatically
- android kotlin - ImageView set image from Uri
- android kotlin - Get battery capacity programmatically
- android kotlin - Get battery temperature programmatically
- android kotlin - Get battery health 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 orientation without restarting activity