lizzy sam
lizzy sam

Reputation: 105

Hide Statusbar in fragment

I'm trying to hide status bar in fragment. I have successfully hidden toolbar but I want to hide status bar also only in one fragment..

  @Override
    public void onCreate(Bundle arg0) {
        super.onCreate(arg0);

   // requestWindowFeature(Window.FEATURE_NO_TITLE);

        ((HomeActivity)getActivity()).getSupportActionBar().hide();
    }

Above code working fine for hide toolbar

  requestWindowFeature(Window.FEATURE_NO_TITLE);

If I requestWindowFeature in fragment onCreate (has mentioned above) application was crashed.

Added three screenshots after implementing answer by @Mathan Chinna

  1. Normal Screen

Normal Screen

  1. After implementing the code from @Mathan Chinna

code from mathan chinna

  1. When I come back from full screen

When I come back from full screen

Upvotes: 4

Views: 10259

Answers (3)

altu
altu

Reputation: 357

@Lalit Singh Fauzdar answer to hide statusBar is correct. if u want to show statusBar u can use

View decorView = getActivity().getWindow().getDecorView(); // Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(uiOptions);

Upvotes: 5

Mathan Chinna
Mathan Chinna

Reputation: 597

I hope you this code is help you more..

From Activity...

public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   // YOUR FRAGMENT
   FragmentManager fragmentManager = getSupportFragmentManager();
   FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
   fragmentTransaction.replace(R.id.fragment_container, fragment,fragment.toString());
   fragmentTransaction.commit();

    }
public void HideStatusBar() {
   this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  }
public void ShowStatusBar() {
    this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  }
}

from fragment..

public class FragmentOne extends Fragment{

private Activity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


 ((MainActivity) getActivity()).HideStatusBar();
   }
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.mActivity = activity;

}
@Override
public void onDetach() {
    super.onDetach();
    this.mActivity = null;
}
@Override
public void onDestroyView() {
    super.onDestroyView();
    if (mActivity!=null){
       ((MainActivity) getActivity()).ShowStatusBar();
    }
}
 }

Upvotes: -2

Lalit Fauzdar
Lalit Fauzdar

Reputation: 6363

Just a copy-paste from my project though but this way you can hide the status bar in a fragment.

 private int currentApiVersion;       
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getActivity().getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        currentApiVersion = android.os.Build.VERSION.SDK_INT;
        final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_FULLSCREEN    
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        if(currentApiVersion >= Build.VERSION_CODES.KITKAT) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(flags);
            final View decorView = getActivity().getWindow().getDecorView();
            decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                        decorView.setSystemUiVisibility(flags);
                    }
                }
            });
        }
        return inflater.inflate(R.layout.yourLayout, container, false);
    }

DO remember that this code is to be copied in your Fragment class and not in any method.

And yes, you could have used only the below code:

getActivity().getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_FULLSCREEN);

Upvotes: 8

Related Questions