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.
addded three pics after implementing second answer
[normal screen][1] [after using your code works goods][2] [when came back from full screen][3]
[1]: https://i.stack.imgur.com/KbX4T.jpg [2]: https://i.stack.imgur.com/sSWqf.jpg [3]: https://i.stack.imgur.com/urJaK.jpg
答案 0 :(得分:4)
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);
答案 1 :(得分:0)
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();
}
}
}
答案 2 :(得分:0)
@Lalit Singh Fauzdar回答隐藏statusBar是正确的。
如果你想显示statusBar你可以使用View decorView = getActivity().getWindow().getDecorView(); // Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(uiOptions);