Shows notifications and system icons. Status bar behavior, background, visibility, color or style can be completely customized.
For making changes to the status bar, we have to retrieve Window from the current activity.
For android devices v4.0 and below we need WindowManager.
1. Status bar from activity
Kotlin:
// Hide status bar
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
// Show status bar
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
Java:
// Hide status bar
View windowDecorView = getWindow().getDecorView();
windowDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
// Show status bar
View windowDecorView = getWindow().getDecorView();
windowDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
2. Status bar from fragment
Calling Window API from fragment requires us to use it’s calling activity from current fragment.
Kotlin:
// Hide status bar
requireActivity().window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
// Show status bar
requireActivity().window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
Java:
// Hide status bar
View windowDecorView = requireActivity().getWindow().getDecorView();
windowDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
// Show status bar
View windowDecorView = requireActivity().getWindow().getDecorView();
windowDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
3. Hide status bar on activity start
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Hide status bar before inflating layout
hideStatusBar()
setContentView(R.layout.activity_main)
}
private fun hideStatusBar() {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
}
}
4. Hide status bar on fragment start
class SimpleFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requireActivity().window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_simple, container, false)
}
}
5. Change status bar on button click
mybutton.setOnClickListener {
hideStatusBar()
}
fun hideStatusBar() {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
}
6. Resources
Need Help ?
Got stuck while learning or trying to implement the code in your simple project, I’m here to help. You can join our slack workspace and we will try to assist you in sharing more information.
Coming Soon !
Need More Help ?
Not sure how to start or learn this topic from the documentation? Don’t worry, you can register and join a scheduled free google meet session which lasts for 20 mins and I’ll help you for free : )
Here We Go Again : (
if (article == helpful) {
println("Like and subscribe to blog newsletter.")
} else {
println("Let me know what i should blog on.")
}
You must be logged in to post a comment.