@@ -6,10 +6,11 @@ import android.graphics.drawable.ColorDrawable
66import android.os.Build
77import android.util.AttributeSet
88import android.util.TypedValue
9+ import android.view.Gravity
910import android.view.LayoutInflater
1011import android.view.View
1112import android.widget.RelativeLayout
12- import androidx.annotation.RestrictTo
13+ import androidx.annotation.*
1314import androidx.appcompat.app.AppCompatDelegate
1415import androidx.core.content.ContextCompat
1516import kotlinx.android.synthetic.main.layout_checkable_text.view.*
@@ -49,33 +50,37 @@ class CheckableTextView : RelativeLayout {
4950 this , true )
5051 attributeSet.let {
5152 val array: TypedArray = context.obtainStyledAttributes(attributeSet, R .styleable.CheckableTextView )
52- val iconTint = array.getColor(
53- R .styleable.CheckableTextView_ctv_IconTint ,
54- ContextCompat .getColor(context, defaultIconTintColor)
55- )
56- val textColor = array.getColor(
57- R .styleable.CheckableTextView_ctv_TextColor ,
58- ContextCompat .getColor(context, defaultTextColor)
59- )
60- val text = array.getString(R .styleable.CheckableTextView_ctv_Text )
61- isChecked = array.getBoolean(R .styleable.CheckableTextView_ctv_IconChecked , false )
62- val textSize = array.getDimensionPixelSize(R .styleable.CheckableTextView_ctv_TextSize , 0 )
63- val textStyle = array.getResourceId(R .styleable.CheckableTextView_ctv_TextStyle , 0 )
64- checkIcon = array.getResourceId(R .styleable.CheckableTextView_ctv_Icon , 0 )
65-
66- // giving applied style attrs least preference (colors n text size will be override by ctv_TextColor & ctv_TextSize as applied later)
67- applyTextStyle(textStyle, context)
68- validateCheckIcon(context)
69- checkedTextTV.text = text
70- checkedTextTV.isSelected = true
71- checkedTextTV.setTextColor(textColor)
72- checkedIV.setImageResource(checkIcon)
73-
74- if (isValidRes(textSize))
75- checkedTextTV.setTextSize(TypedValue .COMPLEX_UNIT_PX , textSize.toFloat())
76- if (isValidRes(iconTint))
77- checkedIV.setColorFilter(iconTint)
53+ if (array.length() > 0 ) {
54+ val iconTint = array.getColor(
55+ R .styleable.CheckableTextView_ctv_IconTint ,
56+ ContextCompat .getColor(context, defaultIconTintColor)
57+ )
58+ val textColor = array.getColor(
59+ R .styleable.CheckableTextView_ctv_TextColor ,
60+ ContextCompat .getColor(context, defaultTextColor)
61+ )
62+ val text = array.getString(R .styleable.CheckableTextView_ctv_Text )
63+ isChecked = array.getBoolean(R .styleable.CheckableTextView_ctv_IconChecked , false )
64+ val textSize = array.getDimensionPixelSize(R .styleable.CheckableTextView_ctv_TextSize , 0 )
65+ val textStyle = array.getResourceId(R .styleable.CheckableTextView_ctv_TextStyle , 0 )
66+ checkIcon = array.getResourceId(R .styleable.CheckableTextView_ctv_Icon , 0 )
67+ val gravity = array.getInt(R .styleable.CheckableTextView_ctv_TextGravity , Gravity .CENTER )
68+
69+ // giving applied style attrs least preference (colors n text size will be override by ctv_TextColor & ctv_TextSize as applied later)
70+ applyTextStyle(textStyle, context)
71+ validateCheckIcon(context)
72+ checkedTextTV.text = text
73+ checkedTextTV.isSelected = true
74+ checkedTextTV.gravity = gravity
75+ checkedTextTV.setTextColor(textColor)
76+ checkedIV.setImageResource(checkIcon)
77+
78+ if (isValidRes(textSize))
79+ checkedTextTV.setTextSize(TypedValue .COMPLEX_UNIT_PX , textSize.toFloat())
80+ if (isValidRes(iconTint))
81+ checkedIV.setColorFilter(iconTint)
7882
83+ }
7984 array.recycle()
8085 }
8186
@@ -124,6 +129,7 @@ class CheckableTextView : RelativeLayout {
124129
125130
126131 private fun isValidRes (res : Int ) = res != 0
132+ private fun emptyNullCheck (text : String? ) = text != null && ! text.isBlank();
127133
128134 private fun notifyListener (isChecked : Boolean ) {
129135 listener?.onCheckChange(this , isChecked)
@@ -171,7 +177,56 @@ class CheckableTextView : RelativeLayout {
171177 return this .isChecked
172178 }
173179
174- interface CheckedListener {
175- fun onCheckChange (view : View , isChecked : Boolean )
180+
181+ // //---------------------------setters------------------------------------------------------------------------------------------////
182+
183+ fun setIconTint (@ColorRes resId : Int ) {
184+ if (isValidRes(resId)) {
185+ val color = ContextCompat .getColor(context, resId)
186+ checkedIV.setColorFilter(color)
187+ }
188+ }
189+
190+ fun setTextSize (@DimenRes resId : Int ) {
191+ if (isValidRes(resId)) {
192+ val dimension = resources.getDimensionPixelSize(resId)
193+ checkedTextTV.setTextSize(TypedValue .COMPLEX_UNIT_PX , dimension.toFloat())
194+ }
195+ }
196+
197+ fun setTextColor (@ColorRes resId : Int ) {
198+ if (isValidRes(resId)) {
199+ val color = ContextCompat .getColor(context, resId)
200+ checkedTextTV.setTextColor(color)
201+ }
202+ }
203+
204+ fun setText (@StringRes resId : Int ) {
205+ if (isValidRes(resId)) {
206+ val string = context.getString(resId)
207+ setText(string)
208+ }
209+ }
210+
211+ fun setText (text : String ) {
212+ if (emptyNullCheck(text))
213+ checkedTextTV.text = text
214+ }
215+
216+ fun setTextGravity (gravity : Int ) {
217+ checkedTextTV.gravity = gravity
218+ }
219+
220+ fun setIcon (@DrawableRes resId : Int ) {
221+ if (isValidRes(resId)) {
222+ checkIcon = resId
223+ validateCheckIcon(context)
224+ checkedIV.setImageResource(checkIcon)
225+ }
226+ }
227+
228+ fun setTextStyle (@StyleRes resId : Int ) {
229+ if (isValidRes(resId))
230+ applyTextStyle(resId, context)
176231 }
177232}
0 commit comments