MainActivity.kt
package com.example.jetpack
import android.app.Activity
import android.graphics.Point
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.math.roundToInt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// show screen size in dp
textView.text = "Screen size in dp"
screenSizeInDp.apply {
// screen width in dp
textView.append("\n\nWidth : $x dp")
// screen height in dp
textView.append("\nHeight : $y dp")
}
}
}
// extension property to get display metrics instance
val Activity.displayMetrics: DisplayMetrics
get() {
// display metrics is a structure describing general information
// about a display, such as its size, density, and font scaling
val displayMetrics = DisplayMetrics()
if (Build.VERSION.SDK_INT >= 30){
display?.apply {
getRealMetrics(displayMetrics)
}
}else{
// getMetrics() method was deprecated in api level 30
windowManager.defaultDisplay.getMetrics(displayMetrics)
}
return displayMetrics
}
// extension property to get screen width and height in dp
val Activity.screenSizeInDp: Point
get() {
val point = Point()
displayMetrics.apply {
// screen width in dp
point.x = (widthPixels / density).roundToInt()
// screen height in dp
point.y = (heightPixels / density).roundToInt()
}
return point
}
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: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>

- android kotlin - EditText allow only numbers
- android kotlin - EditText allow only characters
- android kotlin - EditText hide keyboard after enter
- android kotlin - EditText hide keyboard click outside
- android kotlin - Create ImageView programmatically
- android kotlin - ImageView rounded corners transparent
- android kotlin - ImageView tint programmatically
- android kotlin - ImageView rounded top corners programmatically
- android kotlin - Detect screen orientation change
- android kotlin - Get screen orientation programmatically
- 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