Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. import android.annotation.SuppressLint
  2. import android.content.Context
  3. import android.content.res.TypedArray
  4. import android.graphics.Typeface
  5. import android.util.AttributeSet
  6. import android.util.TypedValue
  7. import android.view.Gravity
  8. import android.view.View
  9. import android.widget.FrameLayout
  10. import android.widget.TextView
  11. import androidx.core.content.res.ResourcesCompat
  12. import com.example.hijaunesia.R
  13.  
  14. class ItemView : FrameLayout {
  15.  
  16. private var textView: TextView? = null
  17. private var view: View? = null
  18. private var barActiveColor: Int = 0
  19. private var barInactiveColor: Int = 0
  20. private var barErrorColor: Int = 0
  21. private var barSuccessColor: Int = 0
  22. private var boxBackgroundColorActive: Int = 0
  23. private var boxBackgroundColorInactive: Int = 0
  24. private var boxBackgroundColorSuccess: Int = 0
  25. private var boxBackgroundColorError: Int = 0
  26. private var hideOTPDrawable: Int = 0
  27. private var defaultOTPDrawable: Int = 0
  28. private var hideOTP = false
  29.  
  30. constructor(context: Context) : super(context) {
  31. init(null)
  32. }
  33.  
  34. constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
  35. init(attrs)
  36. }
  37.  
  38. constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
  39. init(attrs)
  40. }
  41.  
  42. @SuppressLint("CustomViewStyleable")
  43. private fun init(attrs: AttributeSet?) {
  44. val styles = context.obtainStyledAttributes(attrs, R.styleable.OtpTextView)
  45. generateViews(styles)
  46. styles.recycle()
  47. }
  48.  
  49. private fun generateViews(styles: TypedArray) {
  50. val defaultHeight = Utils.getPixels(context,
  51. DEFAULT_BAR_HEIGHT
  52. ).toFloat()
  53. val defaultOtpTextSize = Utils.getPixels(context,
  54. DEFAULT_OTP_TEXT_SIZE
  55. ).toFloat()
  56. val textColor = styles.getColor(R.styleable.OtpTextView_android_textColor, ResourcesCompat.getColor(context.resources, android.R.color.black, null))
  57. val barHeight = styles.getDimension(R.styleable.OtpTextView_bar_height, defaultHeight)
  58. val barMargin = styles.getDimension(R.styleable.OtpTextView_bar_margin, Utils.getPixels(context, 0).toFloat())
  59. var barMarginBottom = styles.getDimension(R.styleable.OtpTextView_bar_margin_bottom, DEFAULT_BAR_MARGIN.toFloat())
  60. var barMarginRight = styles.getDimension(R.styleable.OtpTextView_bar_margin_right, DEFAULT_BAR_MARGIN.toFloat())
  61. var barMarginLeft = styles.getDimension(R.styleable.OtpTextView_bar_margin_left, DEFAULT_BAR_MARGIN.toFloat())
  62. var barMarginTop = styles.getDimension(R.styleable.OtpTextView_bar_margin_top, DEFAULT_BAR_MARGIN.toFloat())
  63. hideOTP = false //styles.getBoolean(R.styleable.OtpTextView_hide_otp, false)
  64. hideOTPDrawable = styles.getResourceId(R.styleable.OtpTextView_hide_otp_drawable, R.drawable.bg_pin)
  65.  
  66. defaultOTPDrawable = ResourcesCompat.getColor(context.resources, R.color.transparent, null)
  67.  
  68. val barEnabled = styles.getBoolean(R.styleable.OtpTextView_bar_enabled, false)
  69.  
  70. val otpTextSize = styles.getDimension(R.styleable.OtpTextView_otp_text_size, defaultOtpTextSize)
  71.  
  72. val otpTextTypeFace = styles.getString(R.styleable.OtpTextView_text_typeface)
  73. val boxBackgroundColor = styles.getResourceId(R.styleable.OtpTextView_otp_box_background, ResourcesCompat.getColor(context.resources, R.color.transparent, null))
  74. boxBackgroundColorActive = styles.getResourceId(R.styleable.OtpTextView_otp_box_background_active, boxBackgroundColor)
  75. boxBackgroundColorInactive = styles.getResourceId(R.styleable.OtpTextView_otp_box_background_inactive, boxBackgroundColor)
  76. boxBackgroundColorSuccess = styles.getResourceId(R.styleable.OtpTextView_otp_box_background_success, boxBackgroundColor)
  77. boxBackgroundColorError = styles.getResourceId(R.styleable.OtpTextView_otp_box_background_error, boxBackgroundColor)
  78. barActiveColor = styles.getColor(R.styleable.OtpTextView_bar_active_color, ResourcesCompat.getColor(context.resources, android.R.color.black, null))
  79. barInactiveColor = styles.getColor(R.styleable.OtpTextView_bar_inactive_color, ResourcesCompat.getColor(context.resources, android.R.color.darker_gray, null))
  80. barErrorColor = styles.getColor(R.styleable.OtpTextView_bar_error_color, ResourcesCompat.getColor(context.resources, android.R.color.holo_red_light, null))
  81. barSuccessColor = styles.getColor(R.styleable.OtpTextView_bar_success_color, ResourcesCompat.getColor(context.resources, android.R.color.black, null))
  82.  
  83. this.setBackgroundResource(boxBackgroundColor)
  84.  
  85. val textViewParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
  86. textViewParams.gravity = Gravity.CENTER
  87. textView = TextView(context)
  88. textView?.gravity = Gravity.CENTER
  89. if (otpTextTypeFace != null) {
  90. try {
  91. val tf = Typeface.createFromAsset(context.assets, otpTextTypeFace)
  92. textView?.typeface = tf
  93. } catch (e: Exception) {
  94. e.printStackTrace()
  95. }
  96.  
  97. }
  98. textView?.setTextColor(textColor)
  99. textView?.setTextSize(TypedValue.COMPLEX_UNIT_PX, otpTextSize)
  100. this.addView(textView, textViewParams)
  101.  
  102. if (barEnabled) {
  103. val barViewParams = LayoutParams(LayoutParams.MATCH_PARENT, barHeight.toInt())
  104. barViewParams.gravity = Gravity.BOTTOM
  105. if (barMargin != 0f) {
  106. barMarginLeft = barMargin
  107. barMarginRight = barMargin
  108. barMarginBottom = barMargin
  109. barMarginTop = barMargin
  110. }
  111. barViewParams.leftMargin = barMarginLeft.toInt()
  112. barViewParams.rightMargin = barMarginRight.toInt()
  113. barViewParams.bottomMargin = barMarginBottom.toInt()
  114. barViewParams.topMargin = barMarginTop.toInt()
  115. view = View(context)
  116. this.addView(view, barViewParams)
  117. }
  118. }
  119.  
  120. fun setText(value: String) {
  121. if (!hideOTP) {
  122. if (textView != null) {
  123. textView?.text = value
  124. }
  125. } else {
  126. textView?.text = ""
  127. if (value == "") {
  128. textView?.setBackgroundResource(defaultOTPDrawable)
  129. } else {
  130. textView?.setBackgroundResource(hideOTPDrawable)
  131. }
  132. }
  133. }
  134.  
  135. fun setViewState(state: Int) {
  136. when (state) {
  137. ACTIVE -> {
  138. view?.setBackgroundColor(barActiveColor)
  139. this.setBackgroundResource(boxBackgroundColorActive)
  140. }
  141. INACTIVE -> {
  142. view?.setBackgroundColor(barInactiveColor)
  143. this.setBackgroundResource(boxBackgroundColorInactive)
  144. }
  145. ERROR -> {
  146. view?.setBackgroundColor(barErrorColor)
  147. this.setBackgroundResource(boxBackgroundColorError)
  148. }
  149. SUCCESS -> {
  150. view?.setBackgroundColor(barSuccessColor)
  151. this.setBackgroundResource(boxBackgroundColorSuccess)
  152. }
  153. else -> {
  154. }
  155. }
  156. }
  157.  
  158. companion object {
  159. const val ACTIVE = 1
  160. const val INACTIVE = 0
  161. const val ERROR = -1
  162. const val SUCCESS = 2
  163.  
  164. private const val DEFAULT_BAR_HEIGHT = 2f
  165. private const val DEFAULT_OTP_TEXT_SIZE = 24f
  166. private const val DEFAULT_BAR_MARGIN = 2
  167. }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement