'Unable to change the color of the status bar in the TabLayout?
I want to change the color of the status bar in the TabLayout but I'm getting an error?
Screenshot of the code with error http://s3.picofile.com/file/8370099418/Screenshot_from_2019_08_20_14_57_39.jpg
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 0) {
toolbar.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
tabLayout.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().getStatusBarColor(ContextCompat.getColor(MainActivity.this , R.color.colorPrimaryDark));
}
} else if (tab.getPosition() == 1) {
toolbar.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.Yellow));
tabLayout.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.Yellow));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().getStatusBarColor(ContextCompat.getColor(MainActivity.this, R.color.YellowDark));
}
}
}
Solution 1:[1]
Java:
if (Build.VERSION.SDK_INT >= 21) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
}
Kotlin:
if (Build.VERSION.SDK_INT >= 21) {
val window = this.window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.statusBarColor = this.resources.getColor(R.color.colorPrimaryDark)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rehan Khan |
