Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Android videoView not showing inside fragment

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Android videoView not showing inside fragment

    Hey all I am having a little bit of an issue with trying to call a fragment that has the videoview within it into my current fragment page.

    When I click on the test button I set up it goes through the code without error but never shows the video fragment - I even gave it a background color of red so I could see it load into my current view.

    My goal here is just to send a parameter of the video path to the video fragment videoview player and start playing it.

    videoPlay.java
    public class videoPlay extends Fragment {
        public videoPlay(){
        //constructor
        }
     
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
     
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout._fragvideoplay, container, false);
     
            MediaController mc= new MediaController(getActivity());
            VideoView view = (VideoView)rootView.findViewById(R.id.videoView);
            String path = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/sddir/Bubble Guppies.mp4";
            view.setVideoURI(Uri.parse(path));
            view.setMediaController(mc);
            view.start();
     
            return rootView;
        }
     
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
     
        }
    }

    _fragvideoplayer.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
     
        <VideoView
            android:id="@+id/videoView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    FragMovies.java:
    public class FragMovies extends Fragment {
     
        public FragMovies(){
        //constructor
        }
     
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
     
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout._fragmovies, container, false);
            WebView view = (WebView) rootView.findViewById(R.id.webView);
     
            view.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
     
                    container.findViewById(R.id.webView).setVisibility(View.GONE);
                    container.findViewById(R.id.pBar1).setVisibility(View.VISIBLE);
                }
     
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    container.findViewById(R.id.pBar1).setVisibility(View.GONE);
                    container.findViewById(R.id.webView).setVisibility(View.VISIBLE);
                }
            });
     
            view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            view.getSettings().setJavaScriptEnabled(true);
            view.getSettings().setAllowContentAccess(true);
            view.getSettings().setAllowFileAccess(true);
            view.getSettings().setLoadsImagesAutomatically(true);
            view.getSettings().setAllowFileAccess(true);
            view.getSettings().setBuiltInZoomControls(false);
            view.getSettings().setDomStorageEnabled(true);
            view.getSettings().setAppCacheEnabled(true);
            view.getSettings().setDisplayZoomControls(false);
            view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            view.getSettings().setSupportZoom(false);
     
            view.setHorizontalScrollBarEnabled(false);
            view.setVerticalScrollBarEnabled(false);
            view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
     
            view.loadUrl("https://www.bing.com/");
     
            return rootView ;
        }
    }

    _fragmovies.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainLayout">
     
        <ProgressBar
            android:id="@+id/pBar1"
            android:layout_width="93dp"
            android:layout_height="match_parent"
            android:layout_alignParentTop="false"
            android:layout_centerHorizontal="true"
            android:layout_marginStart="950px"
            android:layout_marginLeft="950px" />
     
        <WebView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/webView"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true" />    
    </LinearLayout>

    Main_activity.java:
    public class MainActivity extends AppCompatActivity {
        TabLayout tabLayout;
        ViewPager viewPager;
        PagerAdapter pagerAdapter;
        Button button;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            getViews();
     
            //adapter setup
            pagerAdapter = new com.example.telluridetainment.PagerAdapter(getSupportFragmentManager());
     
            //attaching fragments to adapter
            pagerAdapter.addFragment(new FragMovies(),"Movies");
     
            viewPager.setOffscreenPageLimit(1);
            viewPager.setAdapter(pagerAdapter);
            tabLayout.setupWithViewPager(viewPager);
     
            //setting icons
            tabLayout.getTabAt(0).setIcon(R.drawable.ic_movie_white_24dp);
     
            button = (Button) findViewById(R.id.button0);
            button.setOnClickListener(new MyClass());
        }
     
        public class MyClass implements View.OnClickListener {
     
            @Override
            public void onClick(View v) {
                videoPlay myfragment = new videoPlay();
                //pass data
                Bundle bundle = new Bundle();
                bundle.putString("KEY","DATA");
                myfragment.setArguments(bundle);
     
                FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
                fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
     
                fragmentManager.addToBackStack(null);
                fragmentManager.replace(R.id.viewPager, myfragment).commit();
            }
     
        }
     
        private void getViews() {
            tabLayout = findViewById(R.id.mTabLayout);
            viewPager = findViewById(R.id.viewPager);
        }
    }

    activity_main.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:layout_width="2000px"
        android:layout_height="1200px"
        tools:context=".MainActivity">
     
        <Button
            android:id="@+id/button0"
            android:layout_width="200dp"
            android:layout_height="152dp"
            android:text="Button"
            android:textColor="#B71C1C" />
     
        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/appbarLayout"
            tools:ignore="SpeakableTextPresentCheck">
        </android.support.v4.view.ViewPager>
    </RelativeLayout>

    The path to the movie is correct. And the movie is there in the "external" SD card path. Clicking around on the new fragment (videoPlay) yields no video menu (play, remind, pause...) so its not there.

    Any help would be great!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Android videoView not showing inside fragment

    Also posted at: https://coderanch.com/t/749900/mobil...owing-fragment
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Insert Esri feature layer inside Java Android application
    By Icer182 in forum Android Development
    Replies: 1
    Last Post: December 30th, 2020, 04:04 PM
  2. JPanel not showing up dynamically inside JLayeredPane
    By javaAllDay in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 15th, 2019, 09:23 AM
  3. Replies: 6
    Last Post: March 14th, 2014, 11:55 AM
  4. Replies: 0
    Last Post: July 11th, 2013, 07:00 AM