Skip to content

Commit 93e653b

Browse files
committed
bugfix : #9 show toggle for password edittext
1 parent 6c60555 commit 93e653b

File tree

6 files changed

+55
-0
lines changed

6 files changed

+55
-0
lines changed

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
app:custom_component_error_text_bg_color="@android:color/white"
9494
app:custom_component_maxLength="15"
9595
app:custom_component_title="@string/edt_title_password"
96+
app:isToggleEnable = "true"
9697
app:custom_component_title_bg_color="@android:color/white"
9798
app:custom_component_title_color="@color/colorAccent"
9899
app:custom_component_title_error_color="@color/color_error"

customedittextoutlinedborder/src/main/java/com/simform/customcomponent/SSCustomEdittextOutlinedBorder.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import android.graphics.drawable.DrawableContainer.DrawableContainerState
66
import android.graphics.drawable.GradientDrawable
77
import android.graphics.drawable.StateListDrawable
88
import android.text.InputFilter
9+
import android.text.method.HideReturnsTransformationMethod
10+
import android.text.method.PasswordTransformationMethod
911
import android.util.AttributeSet
1012
import android.view.Gravity
1113
import android.view.LayoutInflater
@@ -15,6 +17,7 @@ import android.widget.LinearLayout
1517
import androidx.annotation.ColorInt
1618
import androidx.core.content.ContextCompat
1719
import androidx.core.content.res.ResourcesCompat
20+
import androidx.core.view.isVisible
1821
import androidx.databinding.InverseBindingMethod
1922
import androidx.databinding.InverseBindingMethods
2023
import kotlinx.android.synthetic.main.layout_custom_edittext.view.*
@@ -56,11 +59,14 @@ class SSCustomEdittextOutlinedBorder @JvmOverloads constructor(context: Context,
5659
borderColor = typedArray.getColor(R.styleable.custom_component_attributes_custom_component_border_color, ContextCompat.getColor(context, R.color.color_warm_grey))
5760
borderErrorColor = typedArray.getColor(R.styleable.custom_component_attributes_custom_component_border_error_color, ContextCompat.getColor(context, R.color.color_error))
5861
borderWidth = typedArray.getInt(R.styleable.custom_component_attributes_custom_component_border_width, 1)
62+
val isToggleEnable = typedArray.getBoolean(R.styleable.custom_component_attributes_isToggleEnable, false)
5963

6064
setTitle(title as String)
6165
setEditTextHint(editTextHint as String)
6266
setTextStyle(ResourcesCompat.getFont(context, R.font.graphik_regular))
6367
setIsErrorEnable(isErrorEnable)
68+
setIsToggleEnable(isToggleEnable)
69+
setPasswordToggleClick()
6470
setStyle(inputType, maxLine, minLine, maxLength)
6571
setTitleBackGroundColor(titleBgColor)
6672
setEditTextBackGroundColor(editTextBgColor)
@@ -89,6 +95,33 @@ class SSCustomEdittextOutlinedBorder @JvmOverloads constructor(context: Context,
8995
}
9096
}
9197

98+
private fun setIsToggleEnable(isToggleEnable: Boolean) {
99+
imagePasswordToggle.isVisible = isToggleEnable
100+
}
101+
102+
private fun setPasswordToggleClick() {
103+
imagePasswordToggle.setOnClickListener {
104+
if (editText.transformationMethod.equals(PasswordTransformationMethod.getInstance())) {
105+
imagePasswordToggle.setImageDrawable(
106+
ContextCompat.getDrawable(
107+
context,
108+
R.drawable.ic_baseline_visibility_24
109+
)
110+
)
111+
editText.transformationMethod = HideReturnsTransformationMethod.getInstance()
112+
} else {
113+
imagePasswordToggle.setImageDrawable(
114+
ContextCompat.getDrawable(
115+
context,
116+
R.drawable.ic_baseline_visibility_off_24
117+
)
118+
)
119+
editText.transformationMethod = PasswordTransformationMethod.getInstance()
120+
}
121+
editText.text?.let { text -> editText.setSelection(text.length) }
122+
}
123+
}
124+
92125
private fun setTitleColor(@ColorInt colorID: Int) {
93126
lableTitle.setTextColor(colorID)
94127
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M12,4.5C7,4.5 2.73,7.61 1,12c1.73,4.39 6,7.5 11,7.5s9.27,-3.11 11,-7.5c-1.73,-4.39 -6,-7.5 -11,-7.5zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5zM12,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3 3,-1.34 3,-3 -1.34,-3 -3,-3z"/>
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M12,7c2.76,0 5,2.24 5,5 0,0.65 -0.13,1.26 -0.36,1.83l2.92,2.92c1.51,-1.26 2.7,-2.89 3.43,-4.75 -1.73,-4.39 -6,-7.5 -11,-7.5 -1.4,0 -2.74,0.25 -3.98,0.7l2.16,2.16C10.74,7.13 11.35,7 12,7zM2,4.27l2.28,2.28 0.46,0.46C3.08,8.3 1.78,10.02 1,12c1.73,4.39 6,7.5 11,7.5 1.55,0 3.03,-0.3 4.38,-0.84l0.42,0.42L19.73,22 21,20.73 3.27,3 2,4.27zM7.53,9.8l1.55,1.55c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.66 1.34,3 3,3 0.22,0 0.44,-0.03 0.65,-0.08l1.55,1.55c-0.67,0.33 -1.41,0.53 -2.2,0.53 -2.76,0 -5,-2.24 -5,-5 0,-0.79 0.2,-1.53 0.53,-2.2zM11.84,9.02l3.15,3.15 0.02,-0.16c0,-1.66 -1.34,-3 -3,-3l-0.17,0.01z"/>
5+
</vector>

customedittextoutlinedborder/src/main/res/layout/layout_custom_edittext.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@
5959
app:layout_constraintStart_toStartOf="@+id/editText"
6060
app:layout_constraintTop_toBottomOf="@+id/editText" />
6161

62+
<androidx.appcompat.widget.AppCompatImageView
63+
android:id="@+id/imagePasswordToggle"
64+
android:layout_width="wrap_content"
65+
android:layout_height="wrap_content"
66+
android:layout_marginEnd="@dimen/_8sdp"
67+
app:layout_constraintBottom_toBottomOf="@id/editText"
68+
app:layout_constraintEnd_toEndOf="@id/editText"
69+
app:layout_constraintTop_toTopOf="@id/editText"
70+
app:srcCompat="@drawable/ic_baseline_visibility_off_24" />
71+
6272
</androidx.constraintlayout.widget.ConstraintLayout>
6373
</merge>
6474
</layout>

customedittextoutlinedborder/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<resources>
33
<declare-styleable name="custom_component_attributes">
44
<attr name="isErrorEnable" format="boolean" />
5+
<attr name="isToggleEnable" format="boolean" />
56
<attr name="custom_component_title" format="reference" />
67
<attr name="custom_component_error" format="reference" />
78
<attr name="custom_component_title_color" format="color" />

0 commit comments

Comments
 (0)