Skip to content

Commit 7d2157a

Browse files
committed
Text Color & Custom Font
1 parent 4f8c79b commit 7d2157a

File tree

7 files changed

+170
-15
lines changed

7 files changed

+170
-15
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

app/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ dependencies {
3030
testImplementation 'junit:junit:4.12'
3131
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
3232
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
33-
implementation project(':blockedittext')
33+
debugImplementation project(':blockedittext')
34+
releaseImplementation 'com.infideap.blockedittext:block-edittext:0.3.1'
3435
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3536
implementation 'com.app.infideap.stylishwidget:stylish-widget:1.4.2-alpha', {
3637
exclude group: 'com.android.support', module: 'support-annotations'

app/src/main/java/com/infideap/blockedittextexample/MainActivity.kt

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.infideap.blockedittextexample
22

3+
import android.content.res.ColorStateList
34
import android.graphics.Color
5+
import android.graphics.Typeface
46
import android.os.Bundle
57
import androidx.appcompat.app.AppCompatActivity
68
import android.text.Editable
@@ -48,5 +50,18 @@ class MainActivity : AppCompatActivity() {
4850
icNumberEditText.setLengthAt(2, 4)
4951

5052

53+
val customFontEditText: BlockEditText = findViewById(R.id.blockEditText_custom_font)
54+
// customFontEditText.setTypeface(Typeface.createFromAsset(resources.assets, "Quicksand-Light.ttf"))
55+
56+
val customTextColorEditText: BlockEditText = findViewById(R.id.blockEditText_custom_text_color)
57+
// customTextColorEditText.setHintTextColor(Color.BLACK)
58+
customTextColorEditText.setInputTextColor(ColorStateList(
59+
arrayOf(
60+
intArrayOf(android.R.attr.state_focused),
61+
intArrayOf()),
62+
intArrayOf(ContextCompat.getColor(this,R.color.colorPrimary), Color.BLACK),
63+
))
64+
65+
5166
}
5267
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,41 @@
102102
app:inputType="Integer"
103103
app:numberOfBlock="4"
104104
app:shiftPosition="false" />
105+
106+
<View
107+
android:layout_width="match_parent"
108+
android:layout_height="1px"
109+
android:layout_marginBottom="8dp"
110+
android:layout_marginTop="16dp"
111+
android:background="#aaa" />
112+
113+
<com.infideap.blockedittext.BlockEditText
114+
android:id="@+id/blockEditText_custom_font"
115+
android:layout_width="match_parent"
116+
android:layout_height="wrap_content"
117+
app:cardIconSize="48dp"
118+
app:cardPrefix="Mastercard|Visa"
119+
app:defaultLength="4"
120+
app:separatorCharacter="/"
121+
app:typefaceFromAsset="Quicksand-Medium.ttf"
122+
app:editTextBackground="@drawable/selector_edittext_round_border"
123+
app:hint="Custom Font"
124+
app:inputType="Integer"
125+
app:numberOfBlock="4"
126+
app:shiftPosition="false" />
127+
<com.infideap.blockedittext.BlockEditText
128+
android:id="@+id/blockEditText_custom_text_color"
129+
android:layout_width="match_parent"
130+
android:layout_height="wrap_content"
131+
app:cardIconSize="48dp"
132+
app:cardPrefix="Mastercard|Visa"
133+
app:defaultLength="4"
134+
app:hintTextColor="@color/colorPrimary"
135+
app:editTextBackground="@drawable/selector_edittext_round_border"
136+
app:hint="Custom Text Color"
137+
app:inputType="Integer"
138+
app:numberOfBlock="4"
139+
app:shiftPosition="false" />
105140
</LinearLayout>
106141

107142

blockedittext/build.gradle

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
apply plugin: 'com.android.library'
22
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-kapt'
34
apply plugin: 'maven-publish'
45
android {
56
compileSdkVersion 30
@@ -14,7 +15,6 @@ android {
1415
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1516

1617
}
17-
1818
buildTypes {
1919
release {
2020
minifyEnabled false
@@ -26,7 +26,7 @@ android {
2626
afterEvaluate {
2727
def publish_groupId = 'com.infideap.blockedittext'
2828
def publish_artifactId = 'block-edittext'
29-
def publish_version = '0.2.2'
29+
def publish_version = '0.3.1'
3030
publishing {
3131
publications {
3232
// Creates a Maven publication called "release".
@@ -53,19 +53,18 @@ afterEvaluate {
5353
}
5454
dependencies {
5555
implementation fileTree(dir: 'libs', include: ['*.jar'])
56-
56+
implementation "androidx.core:core-ktx:1.3.2"
5757
implementation 'androidx.appcompat:appcompat:1.2.0'
58+
5859
implementation 'com.app.infideap.stylishwidget:stylish-widget:1.3.9', {
5960
exclude group: 'com.android.support', module: 'support-annotations'
6061
exclude group: 'com.android.support', module: 'support-v4'
6162
exclude group: 'com.android.support', module: 'design'
6263
exclude group: 'com.android.support', module: 'recyclerview-v7'
6364
}
64-
6565
testImplementation 'junit:junit:4.12'
6666
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
6767
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
68-
implementation "androidx.core:core-ktx:1.3.2"
6968
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
7069
}
7170
repositories {

blockedittext/src/main/java/com/infideap/blockedittext/BlockEditText.kt

+111-9
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package com.infideap.blockedittext
22

33
import android.content.Context
44
import android.content.res.ColorStateList
5+
import android.graphics.Typeface
56
import android.graphics.drawable.Drawable
67
import android.os.Build
78
import android.text.*
89
import android.util.AttributeSet
10+
import android.util.Log
911
import android.util.SparseArray
1012
import android.util.SparseIntArray
1113
import android.view.*
@@ -17,6 +19,7 @@ import android.widget.ImageView
1719
import android.widget.LinearLayout
1820
import androidx.annotation.RequiresApi
1921
import androidx.core.content.ContextCompat
22+
import androidx.core.util.keyIterator
2023
import androidx.core.view.ViewCompat
2124
import com.app.infideap.stylishwidget.util.Utils
2225
import com.app.infideap.stylishwidget.view.AEditText
@@ -32,6 +35,8 @@ class BlockEditText : FrameLayout {
3235
private var defaultLength = 1
3336
private var hintTextView: ATextView? = null
3437
private var inputType = InputType.TYPE_CLASS_TEXT
38+
private var inputTextColor: ColorStateList? = null
39+
private var typeface: Typeface? = null
3540
private var watcher: TextWatcher? = null
3641
private var callback: ActionMode.Callback? = null
3742
private val editTexts = SparseArray<AEditText?>()
@@ -87,6 +92,8 @@ class BlockEditText : FrameLayout {
8792
editTextBackground = a.getDrawable(R.styleable.BlockEditText_editTextBackground)
8893
hint = a.getString(R.styleable.BlockEditText_hint)
8994
setHint(hint)
95+
96+
9097
var tempStr = a.getString(R.styleable.BlockEditText_separatorCharacter)
9198
if (!TextUtils.isEmpty(tempStr)) {
9299
separator = tempStr!![0]
@@ -167,6 +174,23 @@ class BlockEditText : FrameLayout {
167174
initLayout()
168175
tempStr = a.getString(R.styleable.BlockEditText_text)
169176
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)
170194
a.recycle()
171195
}
172196

@@ -196,17 +220,25 @@ class BlockEditText : FrameLayout {
196220
editText.addTextChangedListener(createTextChangeListener(editText, i))
197221
editTexts.put(i, editText)
198222
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+
}
203232
editText.setSupportTextAppearance(textAppearance)
204233
setTextSize(editText, textSize)
205234
editText.setOnKeyListener(createKeyListener(editText, i))
206235
} else {
207236
editText = editTexts[i]
208237
}
209238
editText!!.inputType = inputType
239+
editText.typeface = typeface
240+
if (inputTextColor != null)
241+
editText.setTextColor(inputTextColor);
210242
val filters = arrayOfNulls<InputFilter>(1)
211243
filters[0] = LengthFilter(editText, i)
212244
editText.filters = filters
@@ -220,6 +252,7 @@ class BlockEditText : FrameLayout {
220252
if (i + 1 < noOfBlock && separator != null) {
221253
val textView = ATextView(context)
222254
textView.text = separator.toString()
255+
textView.typeface = typeface
223256
val textViewParams = LinearLayout.LayoutParams(
224257
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT
225258
)
@@ -241,6 +274,12 @@ class BlockEditText : FrameLayout {
241274
hideOrShowCardIcon()
242275
}
243276

277+
private fun updateHintTextColor(hasFocus: Boolean) {
278+
hintTextView!!.setHintTextColor(
279+
if (hasFocus) hintColorFocus else hintColorDefault
280+
)
281+
}
282+
244283
private fun setEditTextEnable(editText: AEditText?, i: Int) {
245284
if (shiftPosition && i > 0) editText!!.isEnabled = false else editText!!.isEnabled = isEnabled
246285
}
@@ -316,7 +355,7 @@ class BlockEditText : FrameLayout {
316355
var i = 0
317356
while (i < editTexts.size()) {
318357
val editText = editTexts[i]
319-
editText?.text= null
358+
editText?.text = null
320359
i++
321360
}
322361
if (sequence != null) {
@@ -366,6 +405,47 @@ class BlockEditText : FrameLayout {
366405
}
367406
}
368407

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+
369449
fun setNumberOfBlock(block: Int) {
370450
noOfBlock = block
371451
initLayout()
@@ -395,15 +475,18 @@ class BlockEditText : FrameLayout {
395475
fun setHint(hint: String?) {
396476
if (hintTextView == null) {
397477
hintTextView = ATextView(context)
478+
hintTextView!!.typeface = typeface
398479
hintTextView!!.setPadding(16, 0, 16, 0)
399480
setHintTextAppearance(hintTextAppearance)
400481
setHintTextSize(hintTextAppearance.toFloat())
401482
linearLayout!!.addView(hintTextView, 0)
402483
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+
407490
}
408491
hintTextView!!.visibility = if (hint == null) GONE else VISIBLE
409492
hintTextView!!.hint = hint
@@ -423,6 +506,25 @@ class BlockEditText : FrameLayout {
423506
}
424507
}
425508

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+
426528
fun setSeparatorTextAppearance(textAppearance: Int) {
427529
separatorTextAppearance = textAppearance
428530
initLayout()

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

+3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
<attr name="hint" format="string" />
1212
<attr name="text" format="string" />
1313
<attr name="separatorCharacter" format="string" />
14+
<attr name="typefaceFromAsset" format="string" />
1415
<attr name="textAppearance" format="reference" />
1516
<attr name="hintTextAppearance" format="reference" />
1617
<attr name="separatorTextAppearance" format="reference" />
1718
<attr name="editTextBackground" format="reference" />
1819
<attr name="showCardIcon" format="boolean" />
1920
<attr name="shiftPosition" format="boolean" />
2021
<attr name="style" format="reference" />
22+
<attr name="hintTextColor" format="color" />
23+
<attr name="inputTextColor" format="color" />
2124

2225
<attr name="inputType" format="enum">
2326
<enum name="Text" value="1" />

0 commit comments

Comments
 (0)