How to fix issue VideoView overlay Bottom Navigation bar?

466 Views Asked by At

The fragment contains a ViewPager2, each page contains a full-screen VideoView. I want the VideoView is full screen but behind both Status bar and Bottom Navigation bar (Status bar and Bottom Navigation bar are transparent so we still see the video).
The problem is the VideoView overlay the Bottom Navigation bar, can't see the Bottom Bar but still can interact with it.
How to set the VideoView behind the Bottom Navigation bar or any solution that has the same result?

enter image description here

Edit
res/menu/bottom_nav_menu.xml`

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/navigation_home"
            android:icon="@drawable/ic_home_white_24dp"
            android:title="@string/title_home" />
    
        <item
            android:id="@+id/navigation_discover"
            android:icon="@drawable/ic_baseline_search_24"
            android:title="@string/title_discover" />
    
        <item
            android:id="@+id/navigation_settings"
            android:icon="@drawable/ic_baseline_settings_24"
            android:title="@string/title_settings" />
    
    </menu>

/res/navigation/mobile_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.android.ui.home.VideoCategoriesFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_video_category_pager" />

    <fragment
        android:id="@+id/navigation_discover"
        android:name="com.android.ui.discover.DiscoverFragment"
        android:label="@string/title_discover"
        tools:layout="@layout/fragment_discovering" />

    <fragment
        android:id="@+id/navigation_settings"
        android:name="com.android.ui.settings.SettingsFragment"
        android:label="@string/title_settings"
        tools:layout="@layout/fragment_settings" />
</navigation>

video_preview.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

fragment_list_video_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager_videos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:orientation="vertical">
    </androidx.viewpager2.widget.ViewPager2>

</androidx.constraintlayout.widget.ConstraintLayout>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:foreground="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

ListVideoPagerAdapter.java

public class ListVideoPagerAdapter extends FragmentStateAdapter {

    List<Video> videos = new ArrayList<>();
    private VideoPlayer player;
    long categoryId = -1;

    @Override
    public Fragment createFragment(int position) {
        Fragment fragment;
        if (videos != null && videos.size() > 0) {
            fragment = new VideoPreviewFragment();
            Bundle args = new Bundle();
            args.putString(EXTRA_VIDEO_URI, videos.get(position).getVideoPath());
            args.putLong(EXTRA_VIDEO_ID, videos.get(position).getId());
            args.putLong(EXTRA_CATEGORY_ID, categoryId);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public int getItemCount() {
        return Math.max(videos.size(), 0);
    }

    public void addVideos(List<Video> videos) {
        this.videos.addAll(videos);
        notifyDataSetChanged();
    }

    public Video getItem(int position) {
        return  (videos != null && videos.size() > 0) ? videos.get(position) : null;
    }
}

ListVideoFragment.java

public class ListVideoFragment extends Fragment{

    public static final String EXTRA_CATEGORY_ID = "extra_category_id";

    private ListVideoPagerAdapter mPagerAdapter;
    private ViewPager2 viewPager;
    private ViewPager2.OnPageChangeCallback pageChangeCallback;
    private static ListVideoFragment fragment;
    private VideoViewModel mVideoViewModel;
    private long categoryId = -1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list_video_pager, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Bundle args = getArguments();
        if (args != null) {
            categoryId = args.getLong(EXTRA_CATEGORY_ID);
        }
        mVideoViewModel = new ViewModelProvider(this).get(VideoViewModel.class);

        viewPager = view.findViewById(R.id.pager_videos);
        if (categoryId >= 0) {
            mPagerAdapter = new ListVideoPagerAdapter(this);

            mPagerAdapter.setCategoryId(categoryId);
            try {
                List<CategoryWithVideos> data = mVideoViewModel.getVideosByCategory(categoryId);
                mPagerAdapter.addVideos(data.get(0).videos);             
            } catch (ExecutionException | InterruptedException e) {
                e.printStackTrace();
            }
            initPager();
        }
    }

    private void initPager() {
        viewPager.setCurrentItem(0);
        viewPager.setAdapter(mPagerAdapter);
        pageChangeCallback = new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                Video video = mPagerAdapter.getItem(position);
            }
        };
        viewPager.registerOnPageChangeCallback(pageChangeCallback);
    }

    @Override
    public void onDestroyView() {
        if (viewPager != null) {
            viewPager.unregisterOnPageChangeCallback(pageChangeCallback);
        }
        super.onDestroyView();
    }
}

VideoPreviewFragment.java

public class VideoPreviewFragment extends Fragment {

    public static final String EXTRA_VIDEO_URI = "extra_video_uri";
    public static final String EXTRA_VIDEO_ID = "extra_video_id";
    public static final String EXTRA_CATEGORY_ID = "extra_category_id";

    private VideoPlayer player;
    private VideoView videoView;
    private String videoURI;
    private long videoId;
    private long categoryId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            videoURI = args.getString(EXTRA_VIDEO_URI);
            videoId = args.getLong(EXTRA_VIDEO_ID);
            categoryId = args.getLong(EXTRA_CATEGORY_ID);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.video_view, container, false);
        videoView = view.findViewById(R.id.video_preview);
        videoView.requestFocus();       
        player = new VideoPlayer();
        initializePlayer();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        player.play();
    }

    @Override
    public void onStop() {
        super.onStop();
        player.pause();
    }
    
    private void initializePlayer() {
        player.setVideoUri(videoURI);
        player.setVideoView(videoView);
        player.setCategoryId(categoryId);
        player.setVideoId(videoId);
        player.init();
    }
}
0

There are 0 best solutions below