Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.custom.views_library
- import android.content.Context
- import android.util.AttributeSet
- import android.view.Gravity
- import android.view.View
- import android.widget.FrameLayout
- import android.widget.ProgressBar
- import android.widget.TextView
- class LoadingButton
- @JvmOverloads constructor(
- context: Context,
- attrs: AttributeSet? = null,
- defStyleAttr: Int = 0
- ) : FrameLayout(context, attrs, defStyleAttr), View.OnClickListener {
- var buttonText: String
- get() = textView.text.toString()
- set(value) {
- textView.text = value
- }
- var isButtonLoading = false
- set(value) {
- if (value) startLoading()
- else finishLoading()
- field = value
- }
- private var progressBar: ProgressBar = ProgressBar(context)
- private var textView = TextView(context)
- private lateinit var wrappedOnClickListener: OnClickListener
- init {
- setOnClickListener(this)
- attrs?.let {
- val typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingButton)
- buttonText = typedArray.getString(R.styleable.LoadingButton_buttonText)!!
- isButtonLoading = typedArray.getBoolean(R.styleable.LoadingButton_buttonLoading, false)
- typedArray.recycle()
- }
- textView.text = buttonText
- addView(
- progressBar,
- LayoutParams(
- LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT,
- ).apply {
- gravity = Gravity.CENTER
- }
- )
- addView(
- textView,
- LayoutParams(
- LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT,
- ).apply {
- gravity = Gravity.CENTER
- }
- )
- }
- private fun finishLoading() {
- progressBar.visibility = View.GONE
- textView.visibility = View.VISIBLE
- }
- private fun startLoading() {
- progressBar.visibility = View.VISIBLE
- textView.visibility = View.GONE
- }
- override fun onClick(v: View?) {
- if (!isButtonLoading) {
- wrappedOnClickListener.onClick(v)
- startLoading()
- }
- }
- override fun setOnClickListener(l: OnClickListener?) {
- super.setOnClickListener(l)
- wrappedOnClickListener = l!!
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement