getDisplay и getOrientation не обнаруживают TextView

#android #kotlin

Вопрос:

Проблема моего кода в том, что я пытаюсь заменить устаревший код windowManager.defaultDisplay.orientation и использую предлагаемый код, который будет отображаться здесь getDisplay() и getRotation(), я внимательно прочитал, что он хотел, чтобы я сделал, и чтобы быть уверенным, что это работает, я создал TextView в XML, чтобы показатьесли вращение правильное, для отображения строки из TextView. Однако он не отображает строку, как было предложено, и я действительно не уверен или не уверен, что я правильно создал формат и не знаю, как сделать это правильно, чтобы заставить его работать. Если у кого-нибудь есть какие-либо идеи, как это должно быть сделано, пожалуйста, дайте мне знать, и если я не очень хорошо это объяснил, пожалуйста, дайте мне знать. Заранее благодарю вас.

XML-файл:

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <androidx.camera.view.PreviewView
        android:id="@ id/previewView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@ id/captureButton"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_marginBottom="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:importantForAccessibility="no" />

 <TextView
        android:id="@ id/orientationLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/orientation"
        android:textColor="@color/red"
        android:layout_marginStart="15dp"
        app:layout_constraintTop_toBottomOf="@id/rollLabel"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@ id/orientationText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/orientation_goes_here"
        android:textColor="@color/red"
        android:layout_marginStart="4dp"
        app:layout_constraintTop_toBottomOf="@id/rollText"
        app:layout_constraintStart_toEndOf="@id/orientationLabel"/>

</androidx.constraintlayout.widget.ConstraintLayout>
 

В onCreate() я использую следующие методы:

 val displayManager = applicationContext.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
                        val rotation = getRotation()

                        val orientationWM = when (displayManager.getDisplay(rotation)) {
                            getDisplay(Surface.ROTATION_0) -> orientationView.text = "Portrait"
                            getDisplay(Surface.ROTATION_90) -> orientationView.text = "Landscape"
                            getDisplay(Surface.ROTATION_180) -> orientationView.text = "Rev.Portrait"
                            getDisplay(Surface.ROTATION_270) -> orientationView.text = "Rev.Landscape"
                            else -> "Error"
                        }
 

Методы:

 fun getDisplay(displayID: Int): Display? {
        val displayManager = applicationContext.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
     return displayManager.getDisplay(displayID)
}

fun getRotation(): Int {
    val source = 0

    when (source) {
        0 -> Surface.ROTATION_0
        1 -> Surface.ROTATION_90
        2 -> Surface.ROTATION_180
        3 -> Surface.ROTATION_270
        }
    return source
}