I am trying to set the image background based on the height and width of the viewport in android. I am using windowManager and receiving this error on a non-emulator device. I don't know if that has anything to do with it. The image is a splashscreen which I am trying to make responsive based on different viewport sizes. I found the windowManager.maximumWindowMetrics.bounds.height() to work well on the emulator devices but on a couple physical phones, I got this error below. Here is the error
2021-10-29 15:17:56.184 30160-30160/com.example.appName E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.appName, PID: 30160
java.lang.NoSuchMethodError: No interface method getMaximumWindowMetrics()Landroid/view/WindowMetrics; in class Landroid/view/WindowManager; or its super classes (declaration of 'android.view.WindowManager' appears in /system/framework/framework.jar!classes2.dex)
at com.example.appName.MainActivity.onCreate(MainActivity.kt:21)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7078)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Here is the code for the activity:
package com.example.appName
import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
class MainActivity : AppCompatActivity() {
private lateinit var constraintLayout: ConstraintLayout
private lateinit var displayMetrics: DisplayMetrics
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()
constraintLayout = findViewById(R.id.constraintLayout)
val height = windowManager.maximumWindowMetrics.bounds.height()
var width = windowManager.maximumWindowMetrics.bounds.width()
Log.i("MainActivity", "$height\n")
Log.i("MainActivity", "$width\n")
when{
width in 1075..1085 && height in 2155..2165-> {
constraintLayout.setBackgroundResource(R.drawable.splashscreen_1080x2160)
}
width in 1075..1085 && height in 2215..2225 -> {
constraintLayout.setBackgroundResource(R.drawable.splashscreen_1080x2220)
}
width in 1075..1085 && height in 2275..2285-> {
constraintLayout.setBackgroundResource(R.drawable.splashscreen_1080x2280)
}
width in 1275..1285 && height in 1915..1925-> {
constraintLayout.setBackgroundResource(R.drawable.splashscreen_1280x1920)
}
width in 1435..1445 && height in 2555..2565 -> {
constraintLayout.setBackgroundResource(R.drawable.splashscreen_1440x2560)
}
width in 1435..1445 && height in 2875..2885 -> {
constraintLayout.setBackgroundResource(R.drawable.splashscreen_1440x2880)
}
width in 1435..1445 && height in 2955..2965 -> {
constraintLayout.setBackgroundResource(R.drawable.splashscreen_1440x2960)
}
width in 1435..1445 && height in 3035..3045 -> {
constraintLayout.setBackgroundResource(R.drawable.splashscreen_1440x3040)
}
else -> {
constraintLayout.setBackgroundResource(R.drawable.splashscreen_1280x1920)
}
}
android.os.Handler().postDelayed({
val intent = Intent(this@MainActivity, HomeActivity::class.java)
startActivity(intent)
finish()
}, 3500)
}
}