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+. On Android 4.x the alert will be drawn below the status).
Android X required for v1.1.0 and above, otherwise use old version v1.0.2.
Follow these steps:
Step 1. Add the JitPack repository to your build file
gradle: add it in your root build.gradle
at the end of repositories:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Step 2. Add this line to your app's dependencies:
implementation 'com.github.fede87:StatusBarAlert:2.0.0'
Step 3: Write Code
Create a new StatusBarAlert in activities and fragments.
Using Kotlin DSL:
statusBarAlert {
autoHide()
duration(2, TimeUnit.SECONDS)
showProgress()
text("Kotlin DSL")
alertColor(android.R.color.red)
textColor(android.R.color.white)
progressBarColor(android.R.color.white)
}.show()
Using Java way:
val statusBarAlertview: StatusBarAlert = StatusBarAlert.Builder([email protected])
.autoHide()
.withDuration(100)
.showProgress()
.text("autohide!")
.alertColor(R.color.colorPrimaryDark)
.textColor(R.color.colorAccent)
.progressBarColor(R.color.colorAccent)
.typeface(typeface)
.build().show()
//update status bar alert view text at any time:
statusBarAlertView.setText("UPDATED!!")
//or:
statusBarAlertView.setText(R.string.updated)
// show indeterminate progress:
statusBarAlertView.showProgress()
// hide indeterminate progress:
statusBarAlertView.hideProgress()
Full Example
Here is a full example:
(a). activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#515151"
android:layout_height="match_parent">
<LinearLayout
android:padding="20dp"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_margin="16dp"
android:text="@string/auto_hide_progress"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:text="@string/red_alert"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:text="@string/blink"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:text="@string/transparent_alert"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/dark_status_checkbox"
android:text="@string/enable_dark_status_bar_icons"
android:layout_marginTop="56dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
(b). MainActivity.kt
import android.graphics.Color
import android.graphics.Typeface
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.fede987.statusbaralert.StatusBarAlert
import com.fede987.statusbaralert.utils.statusBarAlert
import kotlinx.android.synthetic.main.activity_main.button1
import kotlinx.android.synthetic.main.activity_main.button2
import kotlinx.android.synthetic.main.activity_main.button3
import kotlinx.android.synthetic.main.activity_main.button4
import kotlinx.android.synthetic.main.activity_main.dark_status_checkbox
import java.util.concurrent.TimeUnit
class MainActivity : AppCompatActivity() {
var typeface: Typeface? = null
val handler: Handler = Handler()
var alert1: StatusBarAlert? = null
var alert2: StatusBarAlert? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createCustomTypeface()
setupButtons()
}
override fun onDestroy() {
alert1 = null
alert2 = null
handler.removeCallbacksAndMessages(null)
super.onDestroy()
}
private fun createCustomTypeface() {
typeface = Typeface.createFromAsset(assets, "font/Lato-Regular.ttf")
}
private fun setupButtons() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
dark_status_checkbox.setOnCheckedChangeListener { button, checked ->
if (checked) window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
else window.decorView.systemUiVisibility = window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
}
} else dark_status_checkbox.visibility = View.GONE
button1.setOnClickListener {
alert1 = StatusBarAlert.Builder([email protected])
.autoHide(true)
.showProgress(true)
.duration(10000)
.text("autohide!")
.typeface(typeface)
.alertColor(R.color.colorAccent)
.progressBarColor(R.color.colorPrimary)
.textColor(R.color.colorPrimary)
.build().apply { show() }
handler.postDelayed({
alert1?.setText("Phase 1!")
alert1?.showProgress()
}, 2000)
handler.postDelayed({
alert1?.setText("Phase 2!")
alert1?.showProgress()
}, 4000)
handler.postDelayed({
alert1?.setText("Completed!")
alert1?.hideProgress()
}, 7500)
}
button2.setOnClickListener {
alert2 = statusBarAlert {
autoHide(false)
showProgress(false)
text("RED ALERT!")
typeface(typeface)
alertColor(R.color.red)
textColor(R.color.colorPrimaryDark)
}.show()
handler.postDelayed({
if (alert2?.parent != null)
alert2?.setText("INFO UPDATED!!")
}, 2000)
}
button3.setOnClickListener {
statusBarAlert {
autoHide()
duration(400)
showProgress(false)
text("BLINK!")
typeface(typeface)
alertColor(R.color.green)
textColor(R.color.colorAccent)
progressBarColor(R.color.colorAccent)
}.show()
}
button4.setOnClickListener {
statusBarAlert {
autoHide()
duration(2, TimeUnit.SECONDS)
showProgress(false)
text("transparent alert!")
alertColor(Color.TRANSPARENT)
textColor(R.color.colorAccent)
progressBarColor(R.color.colorAccent)
typeface(typeface)
}.show()
}
}
}
Reference
Read more here.
Download code here.
Follow code author here.