0

I am learning to create extension functions in Kotlin and get success in some. I want to create 2 extension functions 1 for display Progress bar when and where ever I call that function and 2 is for dismiss that progress.

I had created Extension for Toast As below and it is working fine.

fun Context.showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()}

But I don't get idea how to do same wit Progress bar without any system threats.

Anyone having knowledge about this can help me.

Thanks in advance!

1
  • You could show a progress bar in a dialog. I’m sure there are previous questions about that here or tutorials you could search for. If you want to show one that’s directly in your view layout, that is not really suitable for an extension because it would have to rely on the specific layout being used by the current Activity or Fragment. Commented Feb 1, 2022 at 12:39

1 Answer 1

1

You can use this library for proggres;

https://github.com/Kaopiz/KProgressHUD

init progress your BaseActivity and extend from childs after you can.

class LoadingHelper(context: Context) {
private var progress: KProgressHUD = KProgressHUD.create(context)
    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
    .setLabel(context.getString(R.string.loading))
    .setCancellable(false)
    .setAnimationSpeed(2)
    .setDimAmount(0.5f)


fun showDialog() {
    if (!progress.isShowing) progress.show()
}

fun hideDialog() {
    if (progress.isShowing) progress.dismiss()
}

companion object {
    @Volatile
    private var instance: LoadingHelper? = null

    fun getInstance(context: Context) = instance ?: synchronized(this) {
        instance ?: LoadingHelper(context).also { instance = it }
    }
}

}

 @AndroidEntryPoint
open class BaseActivity : AppCompatActivity() {

    private lateinit var loadingHelper: LoadingHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        loadingHelper = LoadingHelper.getInstance(this)
    }


    fun showLoading() {
        loadingHelper.showDialog()
    }

    fun hideLoading() {
        loadingHelper.hideDialog()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.