@@ -2,10 +2,12 @@ package com.infideap.blockedittext
2
2
3
3
import android.content.Context
4
4
import android.content.res.ColorStateList
5
+ import android.graphics.Typeface
5
6
import android.graphics.drawable.Drawable
6
7
import android.os.Build
7
8
import android.text.*
8
9
import android.util.AttributeSet
10
+ import android.util.Log
9
11
import android.util.SparseArray
10
12
import android.util.SparseIntArray
11
13
import android.view.*
@@ -17,6 +19,7 @@ import android.widget.ImageView
17
19
import android.widget.LinearLayout
18
20
import androidx.annotation.RequiresApi
19
21
import androidx.core.content.ContextCompat
22
+ import androidx.core.util.keyIterator
20
23
import androidx.core.view.ViewCompat
21
24
import com.app.infideap.stylishwidget.util.Utils
22
25
import com.app.infideap.stylishwidget.view.AEditText
@@ -32,6 +35,8 @@ class BlockEditText : FrameLayout {
32
35
private var defaultLength = 1
33
36
private var hintTextView: ATextView ? = null
34
37
private var inputType = InputType .TYPE_CLASS_TEXT
38
+ private var inputTextColor: ColorStateList ? = null
39
+ private var typeface: Typeface ? = null
35
40
private var watcher: TextWatcher ? = null
36
41
private var callback: ActionMode .Callback ? = null
37
42
private val editTexts = SparseArray <AEditText ?>()
@@ -87,6 +92,8 @@ class BlockEditText : FrameLayout {
87
92
editTextBackground = a.getDrawable(R .styleable.BlockEditText_editTextBackground )
88
93
hint = a.getString(R .styleable.BlockEditText_hint )
89
94
setHint(hint)
95
+
96
+
90
97
var tempStr = a.getString(R .styleable.BlockEditText_separatorCharacter )
91
98
if (! TextUtils .isEmpty(tempStr)) {
92
99
separator = tempStr!! [0 ]
@@ -167,6 +174,23 @@ class BlockEditText : FrameLayout {
167
174
initLayout()
168
175
tempStr = a.getString(R .styleable.BlockEditText_text )
169
176
if (tempStr != null ) text = tempStr
177
+
178
+
179
+ val filename = a.getString(R .styleable.BlockEditText_typefaceFromAsset );
180
+ Log .e(" Filename" , filename ? : " " )
181
+ if (filename != null )
182
+ setTypefaceFromAsset(filename)
183
+
184
+ val color = a.getColorStateList(
185
+ R .styleable.BlockEditText_inputTextColor
186
+ )
187
+ if (color != null )
188
+ setInputTextColor(color)
189
+ val hintTextColor = a.getColorStateList(
190
+ R .styleable.BlockEditText_hintTextColor
191
+ )
192
+ if (hintTextColor != null )
193
+ setHintTextColor(hintTextColor.defaultColor)
170
194
a.recycle()
171
195
}
172
196
@@ -196,17 +220,25 @@ class BlockEditText : FrameLayout {
196
220
editText.addTextChangedListener(createTextChangeListener(editText, i))
197
221
editTexts.put(i, editText)
198
222
editText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
199
- if (hintTextView != null )
200
- hintTextView!! .setHintTextColor(
201
- if (hasFocus) hintColorFocus else hintColorDefault
202
- ) }
223
+ if (hintTextView != null ) {
224
+ val hintColorFocus: ColorStateList = ColorStateList .valueOf(
225
+ editText.textColors.getColorForState(
226
+ intArrayOf(android.R .attr.state_focused),
227
+ ContextCompat .getColor(context, R .color.colorAccent)
228
+ ))
229
+ updateHintTextColor(hasFocus);
230
+ }
231
+ }
203
232
editText.setSupportTextAppearance(textAppearance)
204
233
setTextSize(editText, textSize)
205
234
editText.setOnKeyListener(createKeyListener(editText, i))
206
235
} else {
207
236
editText = editTexts[i]
208
237
}
209
238
editText!! .inputType = inputType
239
+ editText.typeface = typeface
240
+ if (inputTextColor != null )
241
+ editText.setTextColor(inputTextColor);
210
242
val filters = arrayOfNulls<InputFilter >(1 )
211
243
filters[0 ] = LengthFilter (editText, i)
212
244
editText.filters = filters
@@ -220,6 +252,7 @@ class BlockEditText : FrameLayout {
220
252
if (i + 1 < noOfBlock && separator != null ) {
221
253
val textView = ATextView (context)
222
254
textView.text = separator.toString()
255
+ textView.typeface = typeface
223
256
val textViewParams = LinearLayout .LayoutParams (
224
257
LinearLayout .LayoutParams .WRAP_CONTENT , LinearLayout .LayoutParams .WRAP_CONTENT
225
258
)
@@ -241,6 +274,12 @@ class BlockEditText : FrameLayout {
241
274
hideOrShowCardIcon()
242
275
}
243
276
277
+ private fun updateHintTextColor (hasFocus : Boolean ) {
278
+ hintTextView!! .setHintTextColor(
279
+ if (hasFocus) hintColorFocus else hintColorDefault
280
+ )
281
+ }
282
+
244
283
private fun setEditTextEnable (editText : AEditText ? , i : Int ) {
245
284
if (shiftPosition && i > 0 ) editText!! .isEnabled = false else editText!! .isEnabled = isEnabled
246
285
}
@@ -316,7 +355,7 @@ class BlockEditText : FrameLayout {
316
355
var i = 0
317
356
while (i < editTexts.size()) {
318
357
val editText = editTexts[i]
319
- editText?.text= null
358
+ editText?.text = null
320
359
i++
321
360
}
322
361
if (sequence != null ) {
@@ -366,6 +405,47 @@ class BlockEditText : FrameLayout {
366
405
}
367
406
}
368
407
408
+
409
+ fun setInputTextColor (color : ColorStateList ) {
410
+ inputTextColor = color
411
+
412
+ hintColorFocus = ColorStateList .valueOf(color.getColorForState(
413
+ intArrayOf(android.R .attr.state_focused),
414
+ ContextCompat .getColor(context, R .color.colorAccent)))
415
+ for (i in 0 until editTexts.size()) {
416
+ val editText = editTexts[i]
417
+ editText!! .setTextColor(inputTextColor);
418
+
419
+ }
420
+ }
421
+
422
+
423
+ fun setInputTextColor (color : Int ) {
424
+
425
+
426
+ for (i in 0 until editTexts.size()) {
427
+ val editText = editTexts[i]
428
+ editText!! .setTextColor(color);
429
+ inputTextColor = editText.textColors
430
+ }
431
+ }
432
+
433
+ fun setTypefaceFromAsset (filename : String ) {
434
+ setTypeface(Typeface .createFromAsset(context.resources.assets, filename));
435
+ }
436
+
437
+ fun setTypeface (typeface : Typeface ) {
438
+ Log .e(" Typeface" , typeface.toString())
439
+ this .typeface = typeface
440
+ if (hintTextView != null )
441
+ hintTextView!! .typeface = typeface
442
+
443
+ for (i in 0 until editTexts.size()) {
444
+ val editText = editTexts[i]
445
+ editText!! .typeface = typeface
446
+ }
447
+ }
448
+
369
449
fun setNumberOfBlock (block : Int ) {
370
450
noOfBlock = block
371
451
initLayout()
@@ -395,15 +475,18 @@ class BlockEditText : FrameLayout {
395
475
fun setHint (hint : String? ) {
396
476
if (hintTextView == null ) {
397
477
hintTextView = ATextView (context)
478
+ hintTextView!! .typeface = typeface
398
479
hintTextView!! .setPadding(16 , 0 , 16 , 0 )
399
480
setHintTextAppearance(hintTextAppearance)
400
481
setHintTextSize(hintTextAppearance.toFloat())
401
482
linearLayout!! .addView(hintTextView, 0 )
402
483
hintColorDefault = hintTextView!! .hintTextColors
403
- hintColorFocus = ContextCompat .getColorStateList(
404
- context,
405
- R .color.colorAccent
406
- )
484
+ // hintColorFocus = ContextCompat.getColorStateList(
485
+ // context,
486
+ // R.color.colorAccent
487
+ // )
488
+
489
+
407
490
}
408
491
hintTextView!! .visibility = if (hint == null ) GONE else VISIBLE
409
492
hintTextView!! .hint = hint
@@ -423,6 +506,25 @@ class BlockEditText : FrameLayout {
423
506
}
424
507
}
425
508
509
+ fun setHintTextColor (color : Int ) {
510
+ hintColorDefault = ColorStateList .valueOf(color)
511
+ if (hintTextView != null ) {
512
+ var focus = false ;
513
+ for (i in 0 until editTexts.size()) {
514
+ val editText = editTexts[i]
515
+ if (editText?.isFocused == true ) {
516
+ focus = true
517
+ break ;
518
+ }
519
+ }
520
+ updateHintTextColor(focus)
521
+ }
522
+ }
523
+
524
+ // fun setFocusHintTextColor(color: Int) {
525
+ // hintColorFocus = ColorStateList.valueOf(color)
526
+ // }
527
+
426
528
fun setSeparatorTextAppearance (textAppearance : Int ) {
427
529
separatorTextAppearance = textAppearance
428
530
initLayout()
0 commit comments