I have a ViewPager and FragmentPagerAdapter set up in one activity with 3 fragments. All fragments are loaded simultaneously and are kept in memory using
mPa
First, in the activity/fragment you want to hide the status-bar: Make a public static of the fragment.
public static Fragment QSFrag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
QSFrag = this;
}
Then in your onPageSelected:
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position == 1){
mActionBar.hide();
YourActivityWithNoStatusOrActionBar.QSFrag.getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}else{
mActionBar.show();
// mActionBar.setSelectedNavigationItem(position); I use this, not sure u need/want it.
}
}
I've just tested it, and it works. But it doesn't look awesome when you leave the fragment with no status-bar. :p
You can use this code in onCreate
:
setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
I figured out how to do this, though it's definitely a hack.
The trick is not to use the Android action bar, it's to make your own in your layout files.
I defined two more RelativeLayouts in my code:
<RelativeLayout
android:id="@+id/padding"
android:layout_width="fill_parent"
android:layout_height="25dip"
android:layout_alignParentTop="true"
android:background="@color/abs__background_holo_dark" />
<RelativeLayout
android:id="@+id/fake_action_bar"
android:layout_width="fill_parent"
android:layout_height="48.0dip"
android:layout_below="@id/padding" >
<!-- stuff here -->
</RelativeLayout>
The top RelativeLayout is padding for the Android status bar. I set it to 25dip, but it may vary - it would probably be best to set this value programmatically. Also you need to set it to a black color so that it matches the status bar color and makes the UI look more natural.
The bottom RelativeLayout is for your fake action bar. You need to stuff everything your action bar needs (title text, buttons, etc.) in this RelativeLayout. I set it to 48dip, but again, this will vary and you should probably set it programmatically.
In the Activity hosting your Fragments, ViewPager and FragmentPagerAdapter, you need to set the flags telling Android to make the status bar an overlay:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
Now we need to make the status bar hide and show when we switch fragments. Have your activity implement ViewPager.OnPageChangeListener and then use the following code:
@Override
public void onPageScrollStateChanged(int state) {
switch(state) {
case ViewPager.SCROLL_STATE_IDLE:
if(mPosition == 1) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mPosition = position;
}
We keep track of the page number using onPageSelected and use it in onPageScrollStateChanged to dynamically hide/show the status bar depending on which Fragment is currently being displayed. You can't do this in onPageSelected because this will cause the status bar to redisplay halfway through a swipe.
You also need to make sure to add appropriate code for your fake action bar - for buttons, this would be OnClickListener.