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.*
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")
}
}
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" />
<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>
AndroidMenifest.xml [part]
<!--
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