Show/hide soft input, i.e. keyboard

hh
Laurent 6 years ago
parent 67155d382a
commit 763fb331f9
  1. 39
      app/src/main/java/net/pokeranalytics/android/ui/adapter/HandHistoryAdapter.kt
  2. 14
      app/src/main/java/net/pokeranalytics/android/ui/extensions/UIExtensions.kt
  3. 19
      app/src/main/java/net/pokeranalytics/android/ui/fragment/HandHistoryFragment.kt
  4. 4
      app/src/main/java/net/pokeranalytics/android/ui/view/handhistory/KeyboardAmountView.kt
  5. 23
      app/src/main/java/net/pokeranalytics/android/ui/view/handhistory/KeyboardContainer.kt
  6. 5
      app/src/main/res/layout/fragment_hand_history.xml
  7. 1
      app/src/main/res/layout/row_hand_action.xml

@ -1,5 +1,7 @@
package net.pokeranalytics.android.ui.adapter package net.pokeranalytics.android.ui.adapter
import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.text.Editable import android.text.Editable
import android.text.InputType import android.text.InputType
@ -8,6 +10,7 @@ import android.view.LayoutInflater
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.Button import android.widget.Button
import android.widget.EditText import android.widget.EditText
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -15,6 +18,7 @@ import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.handhistory.ComputedAction import net.pokeranalytics.android.model.handhistory.ComputedAction
import net.pokeranalytics.android.model.handhistory.HHKeyboard import net.pokeranalytics.android.model.handhistory.HHKeyboard
import net.pokeranalytics.android.ui.extensions.hideKeyboard
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.holder.RowViewHolder import net.pokeranalytics.android.ui.view.holder.RowViewHolder
import net.pokeranalytics.android.ui.view.rowrepresentable.ViewIdentifier import net.pokeranalytics.android.ui.view.rowrepresentable.ViewIdentifier
@ -114,14 +118,16 @@ class HandHistoryAdapter(
amountEditText.isFocusableInTouchMode = true amountEditText.isFocusableInTouchMode = true
amountEditText.setOnTouchListener { v, event -> amountEditText.setOnTouchListener { v, event ->
Timber.d("touch action = ${event.action}")
if (this.amountCanBeEdited) { if (this.amountCanBeEdited) {
// Both are required, otherwise requestFocus() fails if (event.action == MotionEvent.ACTION_UP) {
amountEditText.isFocusable = true // Both are required, otherwise requestFocus() fails
amountEditText.isFocusableInTouchMode = true amountEditText.isFocusable = true
amountEditText.requestFocus() amountEditText.isFocusableInTouchMode = true
requestFocusAndShowKeyboard(amountEditText)
if (event.action == MotionEvent.ACTION_DOWN) {
editTextSelected(amountEditText, true, HHKeyboard.AMOUNT.ordinal) editTextSelected(amountEditText, true, HHKeyboard.AMOUNT.ordinal)
} }
} }
@ -146,6 +152,7 @@ class HandHistoryAdapter(
private fun buttonEdited(button: Button, selected: Boolean, tag: Int) { private fun buttonEdited(button: Button, selected: Boolean, tag: Int) {
button.backgroundTintList = ColorStateList.valueOf(color(selected)) button.backgroundTintList = ColorStateList.valueOf(color(selected))
(itemView.context as Activity).hideKeyboard()
if (selected) { if (selected) {
val row = dataSource.rowRepresentableForPosition(currentPosition) val row = dataSource.rowRepresentableForPosition(currentPosition)
@ -161,7 +168,7 @@ class HandHistoryAdapter(
override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) { override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) {
Timber.d("onbind @position = $position") // Timber.d("onbind @position = $position")
this.currentPosition = position this.currentPosition = position
@ -181,6 +188,9 @@ class HandHistoryAdapter(
val selected = adapter.dataSource.isSelected(position, row, tag) val selected = adapter.dataSource.isSelected(position, row, tag)
// Timber.d("Action at $position is selected: $selected") // Timber.d("Action at $position is selected: $selected")
actionButton.backgroundTintList = ColorStateList.valueOf(color(selected)) actionButton.backgroundTintList = ColorStateList.valueOf(color(selected))
if (selected) {
(itemView.context as Activity).hideKeyboard()
}
computedAction.action.type?.resId?.let { computedAction.action.type?.resId?.let {
actionButton.setText(it) actionButton.setText(it)
@ -205,17 +215,26 @@ class HandHistoryAdapter(
amountEditText.setText(computedAction.action.displayedFormattedAmount) amountEditText.setText(computedAction.action.displayedFormattedAmount)
val tookFocus = if (selected) { if (selected) {
amountEditText.requestFocus() requestFocusAndShowKeyboard(amountEditText)
} else { } else {
amountEditText.clearFocus() amountEditText.clearFocus()
false
} }
Timber.d("tookFocus: $tookFocus")
} }
} }
private fun requestFocusAndShowKeyboard(editText: EditText) {
editText.requestFocus()
if (editText.requestFocus()) {
val imm = itemView.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0)
// imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}
}
} }
/** /**

@ -11,6 +11,7 @@ import android.graphics.Color
import android.net.Uri import android.net.Uri
import android.util.TypedValue import android.util.TypedValue
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.Toast import android.widget.Toast
@ -205,3 +206,16 @@ fun Bitmap.toByteArray() : ByteArray {
this.compress(Bitmap.CompressFormat.PNG, 100, baos) this.compress(Bitmap.CompressFormat.PNG, 100, baos)
return baos.toByteArray() return baos.toByteArray()
} }
fun Activity.hideKeyboard() {
this.currentFocus?.let {
val imm = getSystemService(InputMethodManager::class.java)
// imm.showSoftInput()
imm.hideSoftInputFromWindow(it.windowToken, 0)
}
}
//fun Context.showKeyboard(view: View) {
// val imm = getSystemService(InputMethodManager::class.java)
// imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
//}

@ -5,7 +5,6 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_hand_history.* import kotlinx.android.synthetic.main.fragment_hand_history.*
import kotlinx.android.synthetic.main.fragment_settings.recyclerView import kotlinx.android.synthetic.main.fragment_settings.recyclerView
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
@ -19,6 +18,7 @@ import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.fragment.components.RealmFragment import net.pokeranalytics.android.ui.fragment.components.RealmFragment
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager
import net.pokeranalytics.android.ui.view.handhistory.KeyboardListener import net.pokeranalytics.android.ui.view.handhistory.KeyboardListener
import net.pokeranalytics.android.ui.view.handhistory.StreetCardHeader import net.pokeranalytics.android.ui.view.handhistory.StreetCardHeader
import net.pokeranalytics.android.ui.viewmodel.HandHistoryViewModel import net.pokeranalytics.android.ui.viewmodel.HandHistoryViewModel
@ -84,12 +84,13 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
setDisplayHomeAsUpEnabled(true) setDisplayHomeAsUpEnabled(true)
val viewManager = LinearLayoutManager(requireContext()) // SmoothScrollLinearLayoutManager(requireContext())
// val viewManager = LinearLayoutManager(requireContext())
handHistoryAdapter = HandHistoryAdapter(this, this) handHistoryAdapter = HandHistoryAdapter(this, this)
recyclerView.apply { recyclerView.apply {
setHasFixedSize(true) setHasFixedSize(true)
layoutManager = viewManager layoutManager = SmoothScrollLinearLayoutManager(requireContext())
adapter = handHistoryAdapter adapter = handHistoryAdapter
} }
@ -117,7 +118,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
private fun findNextActionToEdit(startIndex: Int, keyboard: HHKeyboard? = null) { private fun findNextActionToEdit(startIndex: Int, keyboard: HHKeyboard? = null) {
this.model.findIndexForEdition(startIndex, keyboard)?.let { this.model.findIndexForEdition(startIndex, keyboard)?.let {
this.keyboard.show(it) this.keyboard.show(it, this.requireActivity())
this.refreshCells(startIndex) this.refreshCells(startIndex)
} ?: run { } ?: run {
this.keyboard.hide() this.keyboard.hide()
@ -158,7 +159,8 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
this.model.currentSelection = HHSelection(position, HHKeyboard.values()[tag]) this.model.currentSelection = HHSelection(position, HHKeyboard.values()[tag])
// scrolls to selected position // scrolls to selected position
this.recyclerView.scrollToPosition(position) // this.recyclerView.scrollToPosition(position)
this.recyclerView.smoothScrollToPosition(position)
val keyboard = when (row) { val keyboard = when (row) {
is ComputedAction -> { is ComputedAction -> {
@ -174,7 +176,7 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
else -> null else -> null
} }
Timber.d("row $position selected, show keyboard = $keyboard") Timber.d("row $position selected, show keyboard = $keyboard")
keyboard?.let { this.keyboard.show(keyboard) } keyboard?.let { this.keyboard.show(keyboard, requireActivity()) }
} }
@ -231,6 +233,10 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
this.refreshCurrentRow() this.refreshCurrentRow()
} }
override fun closeKeyboard() {
this.keyboard?.hide()
}
// Table refresh // Table refresh
private fun refreshCells(oldIndex: Int) { private fun refreshCells(oldIndex: Int) {
@ -245,4 +251,5 @@ class HandHistoryFragment : RealmFragment(), RowRepresentableDataSource, RowRepr
} }
} }
} }

@ -18,6 +18,10 @@ class KeyboardAmountView(context: Context) : AbstractKeyboardView(context) {
this.clearButton.setOnClickListener { this.clearButton.setOnClickListener {
this.keyboardListener?.clearAmount() this.keyboardListener?.clearAmount()
} }
this.closeButton.setOnClickListener {
this.keyboardListener?.closeKeyboard()
}
} }
} }

@ -1,22 +1,24 @@
package net.pokeranalytics.android.ui.view.handhistory package net.pokeranalytics.android.ui.view.handhistory
import android.app.Activity
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
import android.widget.FrameLayout import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible import androidx.core.view.isVisible
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.handhistory.HHKeyboard import net.pokeranalytics.android.model.handhistory.HHKeyboard
import net.pokeranalytics.android.model.realm.handhistory.Action import net.pokeranalytics.android.model.realm.handhistory.Action
import net.pokeranalytics.android.model.realm.handhistory.Card import net.pokeranalytics.android.model.realm.handhistory.Card
interface KeyboardListener { interface KeyboardListener {
fun actionSelected(action: Action.Type) fun actionSelected(action: Action.Type)
fun cardValueSelected(value: Int) fun cardValueSelected(value: Int)
fun cardSuitSelected(suit: Card.Suit) fun cardSuitSelected(suit: Card.Suit)
fun amountValidated() fun amountValidated()
fun clearAmount() fun clearAmount()
fun closeKeyboard()
} }
class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) { class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
@ -25,7 +27,7 @@ class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(co
val keyboards = HashMap<HHKeyboard, AbstractKeyboardView>() val keyboards = HashMap<HHKeyboard, AbstractKeyboardView>()
private lateinit var constraintLayout: ConstraintLayout // private lateinit var constraintLayout: ConstraintLayout
init { init {
this.setBackgroundColor(context.getColor(R.color.kaki_darker)) this.setBackgroundColor(context.getColor(R.color.kaki_darker))
@ -39,7 +41,7 @@ class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(co
this.isVisible = false this.isVisible = false
} }
fun show(type: HHKeyboard) { fun show(type: HHKeyboard, activity: Activity) {
// Timber.d("show keyboard : $type") // Timber.d("show keyboard : $type")
show() show()
@ -48,9 +50,18 @@ class KeyboardContainer(context: Context, attrs: AttributeSet?) : FrameLayout(co
if (view == null) { if (view == null) {
view = when(type) { view = when(type) {
HHKeyboard.ACTION -> { KeyboardActionView(context) } HHKeyboard.ACTION -> {
HHKeyboard.AMOUNT -> { KeyboardAmountView(context) } // activity.hideKeyboard()
HHKeyboard.CARD -> { KeyboardCardView(context) } KeyboardActionView(context)
}
HHKeyboard.AMOUNT -> {
// activity.showKeyboard()
KeyboardAmountView(context)
}
HHKeyboard.CARD -> {
// activity.hideKeyboard()
KeyboardCardView(context)
}
} }
view.keyboardListener = this.keyboardListener view.keyboardListener = this.keyboardListener
this.keyboards[type] = view this.keyboards[type] = view

@ -29,7 +29,7 @@
android:id="@+id/recyclerView" android:id="@+id/recyclerView"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toTopOf="@id/keyboard"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBar" /> app:layout_constraintTop_toBottomOf="@+id/appBar" />
@ -37,8 +37,7 @@
<net.pokeranalytics.android.ui.view.handhistory.KeyboardContainer <net.pokeranalytics.android.ui.view.handhistory.KeyboardContainer
android:id="@+id/keyboard" android:id="@+id/keyboard"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="200dp" android:layout_height="100dp"
app:layout_constraintTop_toBottomOf="@id/recyclerView"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" /> app:layout_constraintBottom_toBottomOf="parent" />

@ -38,6 +38,7 @@
<androidx.appcompat.widget.AppCompatEditText <androidx.appcompat.widget.AppCompatEditText
android:id="@+id/amountEditText" android:id="@+id/amountEditText"
style="@style/PokerAnalyticsTheme.EditText" style="@style/PokerAnalyticsTheme.EditText"
android:inputType="numberDecimal"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="2" android:layout_weight="2"
android:layout_height="wrap_content" android:layout_height="wrap_content"

Loading…
Cancel
Save