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


Here We Go Again : (

if (article == helpful) {
    println("Like and subscribe to blog newsletter.")
} else {
    println("Let me know what i should blog on.")
}

This site uses Akismet to reduce spam. Learn how your comment data is processed.