Method in Custom layout not working when calling from fragment, but same method works (as xml attribute) for custom styleable

15 views Asked by At

I'm trying to create a wrapper class for a layout file which is a FrameLayout and inside of it, a MaterialButton and a ProgressBar. I have created my own Styleables for 'button text', 'click disable/enable', and 'show progress bar'. On the click of this layout, I'm calling 'showProgress(true)'. Inside 'showProgress()' setButtonText("") is working but ProgressBar is not showing. But in XML, it is working perfectly when setting 'app:showProgress="true"'. Please save my day.

class ProgressButton @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null) : FrameLayout(context, attrs) {

private var binding: ButtonLayoutBinding? = null
private var mButtonText: String? = null
private var mButtonClickable: Boolean? = null
private var mOnClickListener: OnClickListener? = null
private var mShowProgress: Boolean? = null

init {
    binding =
        ButtonLayoutBinding.inflate(LayoutInflater.from(context), this, true)

    context.theme.obtainStyledAttributes(attrs, R.styleable.ProgressButton, 0, 0).apply {
        try {
            mButtonText = getString(R.styleable.ProgressButton_buttonText).also {
                setButtonText(it)
            }
            mButtonClickable = getBoolean(R.styleable.ProgressButton_buttonClickable, true).also {
                setButtonClickable(it)
            }
            mShowProgress = getBoolean(R.styleable.ProgressButton_showProgress, false).also {
                showProgress(it)
            }
        } finally {
            recycle()
        }
    }
}

fun setButtonText(buttonText: String?) {
    mButtonText = buttonText
    binding?.btnContinue?.text = buttonText
}

fun getButtonText() = mButtonText

fun setButtonClickable(buttonClickable: Boolean) {
    mButtonClickable = buttonClickable
    alpha = if (buttonClickable) 1f else 0.24f
}

fun isButtonClickable() = mButtonClickable

fun showProgress(showProgress: Boolean) {
    mShowProgress = showProgress
    if(showProgress){
        binding?.btnContinue?.isEnabled = false
        setButtonText("")
        binding?.progressBar?.visibility = VISIBLE
    }else{
        binding?.btnContinue?.isEnabled = true
        setButtonText(mButtonText)
        binding?.progressBar?.visibility = GONE
    }
}

fun isProgressShowing() = mShowProgress
override fun setOnClickListener(l: OnClickListener?) {
    super.setOnClickListener(l)
    mOnClickListener = l
}

override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
    when (event?.action) {
        MotionEvent.ACTION_DOWN -> {
            alpha = .78f
            postDelayed({
                alpha = 1f
            }, 200)
        }
    }
    return mOnClickListener != null && isButtonClickable() == true
}

override fun onFinishInflate() {
    super.onFinishInflate()
}

}

0

There are 0 answers