MainActivity.kt
package com.example.jetpack
import android.content.Context
import android.content.Context.WINDOW_SERVICE
import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.WindowManager
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)
textView.text = "API Level ${Build.VERSION.SDK_INT}"
applicationContext.displayMetrics()?.apply {
textView.append("\n\nUsing Display Metrics")
textView.append("\n$widthPixels px * ")
textView.append(" $heightPixels px")
}
applicationContext.bounds()?.apply {
textView.append("\n\nUsing Bounds")
textView.append("\n${width()} px * ${height()} px")
}
}
}
// extension function to get display metrics instance
fun Context.displayMetrics():DisplayMetrics?{
// display metrics is a structure describing general information
// about a display, such as its size, density, and font scaling
val displayMetrics = DisplayMetrics()
return if (Build.VERSION.SDK_INT <= 29){
// get window manager system service from context
val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
// getMetrics() method was deprecated in api level 30
windowManager.defaultDisplay.getMetrics(displayMetrics)
return displayMetrics
}else{
null
}
}
// extension function to get window bounds
fun Context.bounds():Rect?{
return if (Build.VERSION.SDK_INT >=30){
// get window manager system service from context
val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
// returns the bounds of the area associated with
// this window or visual context
windowManager.currentWindowMetrics.bounds
}else{
null
}
}
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="#FFFAF0">
<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-medium"
android:textColor="#D71868"
tools:text="TextView"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - Convert dp to pixels programmatically
- android kotlin - Get screen size programmatically
- android kotlin - RecyclerView animation
- android kotlin - RecyclerView smooth scroll
- android kotlin - BitmapShader example
- android kotlin - Circular bitmap with border
- android kotlin - Draw rectangle on bitmap
- android kotlin - Bitmap remove transparency
- android kotlin - Bitmap change brightness contrast
- android kotlin - Bitmap change opacity
- android kotlin - Bitmap to base64 string
- android kotlin - Bitmap color filter
- android kotlin - Convert drawable to bitmap
- android kotlin - Display logo on ActionBar
- android kotlin - Get ActionBar height