user1778855
user1778855

Reputation: 669

VideoView orientation change to full screen (show/hide actionbar and notification)

Hi I have this problem on playing orientation change. On portrait, I have the following XML in my layout directory.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- Player Header -->
    <LinearLayout 
        android:id="@+id/player_header_bg"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:background="@layout/bg_player_header"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">

        <!-- Song Title -->
        <TextView 
            android:id="@+id/videoTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#04b3d2"
            android:textSize="16sp"
            android:paddingLeft="10dp"
            android:textStyle="bold"
            android:text="@string/song_def_title"
            android:layout_marginTop="10dp"/>

        <!-- Full Screen button -->
        <ImageButton 
            android:id="@+id/btnFullScreen"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/ic_action_refresh"
            android:background="@null" />

        <!-- Playlist button -->
        <ImageButton 
            android:id="@+id/btnPlaylist"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/btn_playlist"
            android:background="@null"/>
    </LinearLayout>

    <VideoView
        android:id="@+id/surface_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/player_header_bg" >

    </VideoView>

</RelativeLayout>

Which when the orientation change to landscape, I have this XML in layout-land folder that has the same XML filename but with the following XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <VideoView
        android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >
    </VideoView>

</RelativeLayout>

The orientation changes without any issue, but I wanted to get ride of my actionbar and notificationbar on landscape mode. and have it come out on portrait mode.

I have tried to manually override the onConfigurationChanged method but it's not working well.

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            ActionBar actionBar = getActionBar();
            actionBar.hide();

            WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
            attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
            getWindow().setAttributes(attrs); 

            // setContentView(R.layout.player_video_fs);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            ActionBar actionBar = getActionBar();
            actionBar.show();

            WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
            attrs.flags |= WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN; 
            getWindow().setAttributes(attrs); 

            // setContentView(R.layout.player_video);
        }
    }

If I were to do the above, when I changed to landscape it will remove actionbar and notificationbar but when i change back to portrait, the actionbar get slightly weird. See how it get cut off? the actionbar

actiobar

And changing to landscape, it seem like it's going to blackscreen instead of continuing to play the video.

I have tried a few other method but still unable to resolve. Any idea?

Upvotes: 0

Views: 3321

Answers (1)

Ertyguy
Ertyguy

Reputation: 666

I was also having this issue and stumbled upon this question. I know it's dated but to help future developers who come here.

After searching around I found a simple solution provided by Praveen where instead of using a compound or operator (|=) to set the window attribute flags you can simply call the getWindow().clearFlags(flag) or getWindow().addFlags(flag) functions

So my revised version of hiding the action bar when in landscape looks like this:

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    getActionBar().hide();
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
    getActionBar().show();
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

Upvotes: 1

Related Questions