I'm developing a video player application and I've faced to a problem. I have a custom video controller which contains a fullscreen button and I want hide the System UI (navigation and status bars) when the user enters to fullscreen mode. I've tried to do something like this, but it doesn't work properly. My application is like the screenshot below:
When I click the fullscreen button, I change orientation to landscape and I hide the System UI, but my player doesn't get fullscreen. It looks like the screen below:
Also, when I tap the screen I want to show my video controller, but the System UI shows the below content, instead:
So, how can I implement this behavior? Below are my methods for fullscreen and hiding/showing the System UI:
@Override
public void toggleFullscreen() {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mVideoContainer.getLayoutParams();
if (!mFullscreen) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
hideSystemUI();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.setMargins(0, 0, 0, 0);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
showSystemUI();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
params.width = metrics.widthPixels;
params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, metrics);
params.setMargins(0, 0, 0, 0);
}
mVideoContainer.setLayoutParams(params);
mFullscreen = !mFullscreen;
}
private void hideSystemUI() {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
} else {
uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
decorView.setSystemUiVisibility(uiOptions);
}
private void showSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(0);
}
Here's my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/video_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/videoSurfaceContainer"
android:layout_width="match_parent"
android:layout_height="300dp" >
<SurfaceView
android:id="@+id/videoSurface"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>