Advertisement
aidarsvd

Untitled

Aug 13th, 2021
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.40 KB | None | 0 0
  1. package com.custom.views_library
  2.  
  3. import android.content.Context
  4. import android.util.AttributeSet
  5. import android.view.Gravity
  6. import android.view.View
  7. import android.widget.FrameLayout
  8. import android.widget.ProgressBar
  9. import android.widget.TextView
  10.  
  11. class LoadingButton
  12. @JvmOverloads constructor(
  13.     context: Context,
  14.     attrs: AttributeSet? = null,
  15.     defStyleAttr: Int = 0
  16. ) : FrameLayout(context, attrs, defStyleAttr), View.OnClickListener {
  17.     var buttonText: String
  18.         get() = textView.text.toString()
  19.         set(value) {
  20.             textView.text = value
  21.         }
  22.  
  23.     var isButtonLoading = false
  24.         set(value) {
  25.             if (value) startLoading()
  26.             else finishLoading()
  27.             field = value
  28.         }
  29.  
  30.     private var progressBar: ProgressBar = ProgressBar(context)
  31.     private var textView = TextView(context)
  32.     private lateinit var wrappedOnClickListener: OnClickListener
  33.  
  34.     init {
  35.         setOnClickListener(this)
  36.         attrs?.let {
  37.             val typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingButton)
  38.             buttonText = typedArray.getString(R.styleable.LoadingButton_buttonText)!!
  39.             isButtonLoading = typedArray.getBoolean(R.styleable.LoadingButton_buttonLoading, false)
  40.             typedArray.recycle()
  41.         }
  42.         textView.text = buttonText
  43.         addView(
  44.             progressBar,
  45.             LayoutParams(
  46.                 LayoutParams.WRAP_CONTENT,
  47.                 LayoutParams.WRAP_CONTENT,
  48.             ).apply {
  49.                 gravity = Gravity.CENTER
  50.             }
  51.         )
  52.         addView(
  53.             textView,
  54.             LayoutParams(
  55.                 LayoutParams.WRAP_CONTENT,
  56.                 LayoutParams.WRAP_CONTENT,
  57.             ).apply {
  58.                 gravity = Gravity.CENTER
  59.             }
  60.         )
  61.     }
  62.  
  63.     private fun finishLoading() {
  64.         progressBar.visibility = View.GONE
  65.         textView.visibility = View.VISIBLE
  66.     }
  67.  
  68.     private fun startLoading() {
  69.         progressBar.visibility = View.VISIBLE
  70.         textView.visibility = View.GONE
  71.     }
  72.  
  73.     override fun onClick(v: View?) {
  74.         if (!isButtonLoading) {
  75.             wrappedOnClickListener.onClick(v)
  76.             startLoading()
  77.         }
  78.     }
  79.  
  80.     override fun setOnClickListener(l: OnClickListener?) {
  81.         super.setOnClickListener(l)
  82.         wrappedOnClickListener = l!!
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement