android tv search focus

This commit is contained in:
LagradOst 2021-10-30 20:42:58 +02:00
parent 3734871608
commit 40fd2fd49c
3 changed files with 18 additions and 30 deletions

View file

@ -9,10 +9,8 @@ import android.content.pm.PackageManager
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.content.res.Configuration import android.content.res.Configuration
import android.content.res.Resources import android.content.res.Resources
import android.graphics.Rect
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.TypedValue
import android.view.* import android.view.*
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
@ -52,7 +50,6 @@ import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_result.* import kotlinx.android.synthetic.main.fragment_result.*
import java.util.* import java.util.*
import kotlin.concurrent.thread import kotlin.concurrent.thread
import kotlin.math.roundToInt
const val VLC_PACKAGE = "org.videolan.vlc" const val VLC_PACKAGE = "org.videolan.vlc"
@ -140,28 +137,8 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
} }
} }
private fun Activity.getRootView(): View {
return findViewById(android.R.id.content)
}
private fun Context.convertDpToPx(dp: Float): Float {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
this.resources.displayMetrics
)
}
private fun Activity.isKeyboardOpen(): Boolean {
val visibleBounds = Rect()
this.getRootView().getWindowVisibleDisplayFrame(visibleBounds)
val heightDiff = getRootView().height - visibleBounds.height()
val marginOfError = this.convertDpToPx(50F).roundToInt()
return heightDiff > marginOfError
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
println("Keycode: $keyCode") //println("Keycode: $keyCode")
//showToast( //showToast(
// this, // this,
// "Got Keycode $keyCode | ${KeyEvent.keyCodeToString(keyCode)} \n ${event?.action}", // "Got Keycode $keyCode | ${KeyEvent.keyCodeToString(keyCode)} \n ${event?.action}",
@ -222,11 +199,11 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
playerEventListener?.invoke(playerEvent) playerEventListener?.invoke(playerEvent)
} }
when (keyCode) { //when (keyCode) {
KeyEvent.KEYCODE_DPAD_CENTER -> { // KeyEvent.KEYCODE_DPAD_CENTER -> {
println("DPAD PRESSED ${this.isKeyboardOpen()}") // println("DPAD PRESSED ${this.isKeyboardOpen()}")
} // }
} //}
return super.onKeyDown(keyCode, event) return super.onKeyDown(keyCode, event)
} }

View file

@ -300,6 +300,8 @@ class SearchFragment : Fragment() {
main_search.setOnQueryTextListener(object : SearchView.OnQueryTextListener { main_search.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean { override fun onQueryTextSubmit(query: String): Boolean {
searchViewModel.searchAndCancel(query) searchViewModel.searchAndCancel(query)
main_search?.clearFocus()
search_filter?.requestFocus()
return true return true
} }

View file

@ -13,6 +13,9 @@
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/toggle1" android:id="@+id/toggle1"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:paddingStart="20dp" android:paddingStart="20dp"
android:paddingEnd="20dp" android:paddingEnd="20dp"
@ -24,12 +27,15 @@
tools:text="Search" tools:text="Search"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<requestFocus/>
</com.google.android.material.switchmaterial.SwitchMaterial> </com.google.android.material.switchmaterial.SwitchMaterial>
<ListView <ListView
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusUp="@id/toggle1"
android:nextFocusDown="@id/apply_btt"
android:id="@+id/listview1" android:id="@+id/listview1"
android:layout_marginTop="-10dp" android:layout_marginTop="-10dp"
android:layout_marginBottom="60dp" android:layout_marginBottom="60dp"
@ -43,6 +49,9 @@
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusUp="@id/toggle1"
android:nextFocusDown="@id/apply_btt"
android:id="@+id/listview2" android:id="@+id/listview2"
android:layout_marginTop="-10dp" android:layout_marginTop="-10dp"
android:layout_marginBottom="60dp" android:layout_marginBottom="60dp"