Created an extension function that can be used to hide the keyboard from any view and used that extension function to add the behavior of hiding the keyboard when a EditText loses focus which was included in a custom view MyTextInputEditText. Then used that class in all EditText fields in SendTransactionFragment, ReceiveTransactionFragment, ImportBrainkeyFragment and CreateAccountFragment to introduce the described behavior.

This commit is contained in:
Severiano Jaramillo 2019-01-16 10:30:11 -06:00
parent 12d17c54d3
commit ec57bea829
7 changed files with 83 additions and 82 deletions

View file

@ -1,7 +1,10 @@
package cy.agorise.bitsybitshareswallet.utils
import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.google.android.material.floatingactionbutton.FloatingActionButton
@ -42,4 +45,12 @@ fun String.containsDigits(): Boolean {
*/
fun String.containsVowels(): Boolean {
return Pattern.matches("[aeiou]", this)
}
/**
* Allows to hide the Keyboard from any view
*/
fun View.hideKeyboard(){
val inputMethodManager = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(this.windowToken, 0)
}

View file

@ -1,43 +0,0 @@
package cy.agorise.bitsybitshareswallet.views;
import android.content.Context;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import com.google.android.material.textfield.TextInputEditText;
// An EditText that lets you use actions ("Done", "Go", etc.) on multi-line edits.
public class MyTextInputEditText extends TextInputEditText
{
public MyTextInputEditText(Context context)
{
super(context);
}
public MyTextInputEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyTextInputEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) {
// clear the existing action
outAttrs.imeOptions ^= imeActions;
// set the DONE action
outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
}
if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
return connection;
}
}

View file

@ -0,0 +1,37 @@
package cy.agorise.bitsybitshareswallet.views
import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import com.google.android.material.textfield.TextInputEditText
import cy.agorise.bitsybitshareswallet.utils.hideKeyboard
/**
* A TextInputEditText that hides the keyboard when the focus is removed from it and also lets you
* use actions ("Done", "Go", etc.) on multi-line edits.
*/
class MyTextInputEditText(context: Context?, attrs: AttributeSet?) : TextInputEditText(context, attrs){
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
val connection = super.onCreateInputConnection(outAttrs)
val imeActions = outAttrs.imeOptions and EditorInfo.IME_MASK_ACTION
if (imeActions and EditorInfo.IME_ACTION_DONE != 0) {
// clear the existing action
outAttrs.imeOptions = outAttrs.imeOptions xor imeActions
// set the DONE action
outAttrs.imeOptions = outAttrs.imeOptions or EditorInfo.IME_ACTION_DONE
}
if (outAttrs.imeOptions and EditorInfo.IME_FLAG_NO_ENTER_ACTION != 0) {
outAttrs.imeOptions = outAttrs.imeOptions and EditorInfo.IME_FLAG_NO_ENTER_ACTION.inv()
}
return connection
}
override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
super.onFocusChanged(focused, direction, previouslyFocusedRect)
if (!focused) this.hideKeyboard()
}
}

View file

@ -9,39 +9,26 @@
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
android:paddingBottom="@dimen/activity_vertical_margin"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true">
<RelativeLayout
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilAccountName"
style="@style/Widget.Bitsy.TextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilAccountName"
style="@style/Widget.Bitsy.TextInputLayout"
android:hint="@string/text__bitshares_account_name">
<cy.agorise.bitsybitshareswallet.views.MyTextInputEditText
android:id="@+id/tietAccountName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/text__bitshares_account_name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tietAccountName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:digits="abcdefghijklmnopqrstuvwxyz0123456789-"
android:singleLine="true"/>
</com.google.android.material.textfield.TextInputLayout>
<!--<com.github.ybq.android.spinkit.SpinKitView-->
<!--android:id="@+id/spin_kit"-->
<!--android:layout_width="20dp"-->
<!--android:layout_height="20dp"-->
<!--android:layout_marginEnd="15dp"-->
<!--android:layout_alignParentEnd="true"-->
<!--android:layout_centerVertical="true"-->
<!--android:visibility="invisible"-->
<!--app:SpinKit_Color="@color/colorAccent"-->
<!--style="@style/SpinKitView.Small.ThreeBounce" />-->
</RelativeLayout>
android:inputType="text"
android:digits="abcdefghijklmnopqrstuvwxyz0123456789-"
android:singleLine="true"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilPin"
@ -52,7 +39,7 @@
android:hint="@string/text_field__6_digit_pin"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
<cy.agorise.bitsybitshareswallet.views.MyTextInputEditText
android:id="@+id/tietPin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -69,7 +56,7 @@
android:hint="@string/text_field__confirm_pin"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
<cy.agorise.bitsybitshareswallet.views.MyTextInputEditText
android:id="@+id/tietPinConfirmation"
android:layout_width="match_parent"
android:layout_height="wrap_content"

View file

@ -18,7 +18,10 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilPin"
@ -31,7 +34,7 @@
android:hint="@string/text_field__6_digit_pin"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
<cy.agorise.bitsybitshareswallet.views.MyTextInputEditText
android:id="@+id/tietPin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -51,7 +54,7 @@
android:hint="@string/text_field__confirm_pin"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
<cy.agorise.bitsybitshareswallet.views.MyTextInputEditText
android:id="@+id/tietPinConfirmation"
android:layout_width="match_parent"
android:layout_height="wrap_content"

View file

@ -5,6 +5,9 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
tools:context=".fragments.ReceiveTransactionFragment">
<androidx.constraintlayout.widget.Guideline
@ -27,7 +30,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/centeredVerticalGuideline">
<com.google.android.material.textfield.TextInputEditText
<cy.agorise.bitsybitshareswallet.views.MyTextInputEditText
android:id="@+id/tietAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"

View file

@ -4,14 +4,17 @@
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
tools:context=".fragments.SendTransactionFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".fragments.SendTransactionFragment">
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/centeredVerticalGuideline"
@ -37,7 +40,7 @@
android:hint="@string/text__to"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
<cy.agorise.bitsybitshareswallet.views.MyTextInputEditText
android:id="@+id/tietTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -61,7 +64,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/centeredVerticalGuideline">
<com.google.android.material.textfield.TextInputEditText
<cy.agorise.bitsybitshareswallet.views.MyTextInputEditText
android:id="@+id/tietAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -113,7 +116,7 @@
android:hint="@string/text__memo"
app:layout_constraintTop_toBottomOf="@id/tilAmount">
<com.google.android.material.textfield.TextInputEditText
<cy.agorise.bitsybitshareswallet.views.MyTextInputEditText
android:id="@+id/tietMemo"
android:layout_width="match_parent"
android:layout_height="wrap_content"