Search code examples
androidandroid-studiocolorsandroid-tablayout

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

  • I suppose you have to use tabLayout.setStatusBarColor(ContextCompat.getColor(MainActivity.this,R.color.YellowDark))