ViewPager viewPager = findViewById(R.id.viewPager); // get reference to ViewPager object viewPager.setVisibility(View.INVISIBLE); // set the ViewPager to be invisible
ViewPager viewPager = findViewById(R.id.viewPager); // get reference to ViewPager object if (shouldDisplayViewPager) { viewPager.setVisibility(View.VISIBLE); // set the ViewPager to be visible } else { viewPager.setVisibility(View.GONE); // set the ViewPager to be gone }This code example checks whether the ViewPager should be displayed based on a boolean flag. If it should be displayed, the visibility is set to visible. If not, the visibility is set to gone, removing the ViewPager from the layout entirely. In both examples, the android.support.v4.view package library is used to access the ViewPager class and its setVisibility method. The setVisibility method takes an integer parameter representing the visibility value to set (either View.VISIBLE, View.INVISIBLE, or View.GONE), allowing developers to show or hide the ViewPager as needed.