I have the following view:
when I click on one of the edit texts a soft keyboard appears. After closing this keyboard my navigation back button doesn't react when i click it. Before I click on one of the edit texts my back button works fine.
The code of my "verification" fragment:
class VerifyPhoneFragment : Fragment() {
lateinit var appContext: Context
private var editTexts: ArrayList<EditText> = ArrayList()
private var focusedElement: Int = 0
private var verifyButton: TextView? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_verify_phone, container, false)
val backButton = view.findViewById<TextView>(R.id.backButtonAbout)
backButton.setOnClickListener {
activity!!.onBackPressed()
}
verifyButton = view.findViewById(R.id.btn_verify)
verifyButton?.setOnClickListener {
val dataConnection = DataConnection()
var code: String = ""
editTexts.forEach {
code += it.text.toString()
}
dataConnection.doGet("/phone/verification",
hashMapOf("token" to code),
{ result: Any?, message: String ->
activity!!.runOnUiThread {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
if(result != null && result is Boolean && result)
activity!!.onBackPressed()
}
}, Boolean::class.java)
}
editTexts.add(view.findViewById(R.id.editText1))
editTexts.add(view.findViewById(R.id.editText2))
editTexts.add(view.findViewById(R.id.editText3))
editTexts.add(view.findViewById(R.id.editText4))
editTexts.add(view.findViewById(R.id.editText5))
editTexts.add(view.findViewById(R.id.editText6))
editTexts.forEach { et ->
et.isEnabled = false
et.setOnKeyListener(object : View.OnKeyListener {
override fun onKey(v: View, keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_DEL && event.action !=KeyEvent.ACTION_DOWN) {
if((focusedElement > 0 && focusedElement != 5) || (focusedElement == 5 && editTexts[focusedElement].text.toString().length == 0)) {
focusedElement--
editTexts[focusedElement].isEnabled = true
editTexts[focusedElement].requestFocus()
editTexts[focusedElement].setText("")
editTexts[focusedElement+1].isEnabled = false
}
else if(focusedElement == 5 && editTexts[focusedElement].text.toString().length != 0){
editTexts[focusedElement].setText("")
}
}
else if(keyCode >= 7 && keyCode <= 16 && event.action !=KeyEvent.ACTION_DOWN){
if(focusedElement < 5)
{
editTexts[focusedElement].setText((keyCode - 7).toString())
focusedElement++
editTexts[focusedElement].isEnabled = true
editTexts[focusedElement].requestFocus()
editTexts[focusedElement-1].isEnabled = false
}
else {
editTexts[focusedElement].setText((keyCode - 7).toString())
editTexts[focusedElement].clearFocus()
hideKeyboard(editTexts[focusedElement])
}
}
checkIfVerifyShouldBeEnabled()
return true
}
})
}
editTexts[focusedElement].isEnabled = true
return view
}
private fun hideKeyboard(view: View) {
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
private fun checkIfVerifyShouldBeEnabled(){
if(editTexts[5].text.toString().length > 0) {
verifyButton?.isEnabled = true
verifyButton?.setTextColor(resources.getColor(R.color.white))
}
else{
verifyButton?.isEnabled = false
verifyButton?.setTextColor(resources.getColor(R.color.grey))
}
}
override fun onAttach(context: Context?) {
super.onAttach(context)
appContext = context!!
}
}
The code of my layout for this fragment:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background_payment_1">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar6"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetStart="0dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/backButtonAbout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:includeFontPadding="false"
android:text="@string/cancel"
android:textColor="@color/white"
android:textSize="19sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<TextView
android:id="@+id/btn_verify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:includeFontPadding="false"
android:text="@string/verify"
android:textColor="@color/grey"
android:textSize="19sp"
android:enabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="80dp"
android:text="@string/verification_code"
android:textColor="@color/white"
style="@style/ToolBarHeaderText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.49"
app:layout_constraintVertical_chainStyle="spread" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/message_phone_verification"
android:textAlignment="center"
android:textSize="18sp"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="@+id/linearLayout"
app:layout_constraintStart_toStartOf="@+id/linearLayout"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<android.support.constraint.ConstraintLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="45dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="45dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4">
<android.support.v7.widget.CardView
android:id="@+id/cv1"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv2"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv2"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv3"
app:layout_constraintStart_toEndOf="@+id/cv1"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv3"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv4"
app:layout_constraintStart_toEndOf="@+id/cv2"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv4"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv5"
app:layout_constraintStart_toEndOf="@+id/cv3"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv5"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv6"
app:layout_constraintStart_toEndOf="@+id/cv4"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp"
android:inputType="number">
<EditText
android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cv6"
android:layout_width="40dp"
android:layout_height="50dp"
android:backgroundTint="#76FFFFFF"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/cv5"
app:layout_constraintTop_toTopOf="parent"
app:cardCornerRadius="5dp"
app:cardElevation="0dp">
<EditText
android:id="@+id/editText6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:background="@color/transparent"
android:textAlignment="center"
android:inputType="number"/>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
So my question is, how do I get the focus of my navigation back button? Or is it a focus problem at all? Btw, the cancel button always works fine!
I found the problem. When the user manually closes the keyboard the focus is still on the current edit text. So when he then presses the navigation back button the onKey event is fired, but i didn't handle the back key event. So i just added following code and everything works fine:
else if(keyCode == KeyEvent.KEYCODE_BACK && event.action != KeyEvent.ACTION_DOWN){
activity?.onBackPressed()
}