enter image description here I have fragments, and when I click on any fragment in the upper part of the panel (where the clock is) the color should change. How to implement?

public class MainPagerAdapter extends FragmentStatePagerAdapter { private final static int COUNT = 3; private final static int HORIZONTAL = 0; private final static int TWO_WAY = 1; private final static int TWO_WAY1 = 2; public MainPagerAdapter(final FragmentManager fm) { super(fm); } @Override public Fragment getItem(final int position) { switch (position) { case TWO_WAY: return new HorizontalPagerFragment_bookmarks(); case TWO_WAY1: return new HorizontalPagerFragment_exit(); case HORIZONTAL: return new HorizontalPagerFragment(); default: } return getItem(position); } @Override public int getCount() { return COUNT; } 

}

public class HorizontalPagerFragment_bookmarks extends Fragment {

 @Nullable @Override public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) { setStatusBarColor(R.color.colorAccent); return inflater.inflate(R.layout.fragment_horizontal1, container, false); } public void setStatusBarColor(int color) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { return; } Window window = getActivity().getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(ContextCompat.getColor(getActivity(), color)); } 

}

    1 answer 1

    1. Place method in activity

     public void setStatusBarColor(int color) { if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { return; } Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(ContextCompat.getColor(this, color)); } 

    2. In onCreate activity, hang the listener on the tabs.

     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){ @Override public void onTabSelected(TabLayout.Tab tab){ if(tab.getPosition() == 0){ //красим статусбар цветом tab1 при клике на первый таб setStatusBarColor(R.color.tab1); } else{ //и цветом tab2 при клике на второй таб setStatusBarColor(R.color.tab2); } } }); 
    • Where do I need to register it? - fcbarcafc
    • In the class of the fragment. And to call a method in the listener for a fragment - Flippy
    • you can write the entire code, with the addition. At the top my code is attached - fcbarcafc
    • Em. You have to do it yourself - Flippy
    • See the code, like this? - fcbarcafc