Skip to content

StatusBar AlertView

kevadiya krunal edited this page Mar 30, 2019 · 3 revisions

What is this?

This is a small library inspired by Telegram X status bar new alert written in Kotlin. It can show custom message with optional indeterminate progress in status bar area. Optional autohide feature can be tweaked with custom duration. When showing, status bar alert kindly hides status bar's icon with SYSTEM_UI_FLAG_LOW_PROFILE flag mode, if available from os.

Supported devices

This lib is supported by every device with a SDK level of at least 14 (Android 4+).

Quick walkthrough

Quick Sample

Show a new status bar alert:

val statusBarAlertview: StatusBarAlertView = StatusBarAlert.Builder(this)
            .autoHide(true)
            .withDuration(5000)
            .showProgress(true)
            .showTextAnimation(false)
            .withText("Please wait")
            .withAlertColor(R.color.color_type_default)
            .build()
                       
                       
//update status bar alert view text at any time:
statusBarAlertView.updateText("UPDATED!!")
//or:
statusBarAlertView.updateText(R.string.updated)
 
 
// show indeterminate progress:
statusBarAlertView.showIndeterminateProgress()

Hide all status bar alerts for current activity with a "on hidden" callback:

StatusBarAlert.hide(this@MainActivity, Runnable {})
  • Here is example to use
class StatusBarAlertViewActivity : AppCompatActivity() {

    private var statusBarProgress: StatusBarAlertView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_statusbar_alertview)

        findViewById<Button>(R.id.btn_1).setOnClickListener {
            showAlertView(progressBar())
        }
        findViewById<Button>(R.id.btn_2).setOnClickListener {
            showAlertView(redAlertBar())
        }
        findViewById<Button>(R.id.btn_3).setOnClickListener {
            showAlertView(blinkBar())
        }
    }

    override fun onDestroy() {
        StatusBarAlert.hide(this, Runnable{})
        super.onDestroy()
    }

    private fun showAlertView(alertView: StatusBarAlertView?) {
        if(statusBarProgress != null) {
            StatusBarAlert.hide(this, Runnable {})
        }
        statusBarProgress = alertView
        statusBarProgress?.showIndeterminateProgress()
    }

    private fun progressBar(): StatusBarAlertView? {
        //return progressMessage(msg = "Please wait") available extention
        return StatusBarAlert.Builder(this)
            .autoHide(true)
            .withDuration(5000)
            .showProgress(true)
            .showTextAnimation(false)
            .withText("Please wait")
            .withAlertColor(R.color.color_type_default)
            .build()
    }

    private fun redAlertBar(): StatusBarAlertView? {
        return StatusBarAlert.Builder(this)
            .autoHide(true)
            .withDuration(500)
            .showProgress(false)
            .withText("RED ALERT!")
            .withTypeface(Typeface.createFromAsset(assets, "BeautifulAndOpenHearted.ttf"))
            .withAlertColor(R.color.red)
            .build()
    }

    private fun blinkBar(): StatusBarAlertView? {
        return ResourcesCompat.getFont(this, R.font.montserrat_light)?.let {
            StatusBarAlert.Builder(this)
                .autoHide(true)
                .withDuration(500)
                .showProgress(false)
                .withText("BLINK!")
                .withTypeface(it)
                .showTextAnimation(true)
                .withAlertColor(R.color.green)
                .build()
        }
    }
}

Clone this wiki locally