Search code examples
javaandroiddynamicandroid-youtube-api

Create YouTubePlayerSupportFragment programmatically (Android)


I need create YouTubePlayer dynamically, I can't extends on YoutubeBaseActivity. So I need create only YouTubePlayerSupportFragment. I tried do it like this:

container_more_info_item - it my linearLayout

//create video
        for(int j = 0; j < 5; j++) {
            final YouTubePlayerSupportFragment ysF = new YouTubePlayerSupportFragment();
            ysF.initialize("AIzbSyAT5C-xs5YG1HzG69Fn__PHq9DHXBuKpws", new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                    youTubePlayer.cueVideo("IjUlQU6yifk");
                }

                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

                }
            });

//this line error, because ViewGroup can not be applied com.google.android.youtube.player.YouTubePlayerSupportFragment
            container_more_info_item.addView(ysF);
        }

And now I'm stuck and can't come up with something

UPD:

I use FragmentManager, but I have this message:

Cannot resolve method 'add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment)

 //create video
        for(int j = 0; j < 5; j++) {
            final YouTubePlayerSupportFragment ysF = new YouTubePlayerSupportFragment();
            ysF.initialize("AIzaSyAT5C-xs5YG1HzG69Fn__PHq9DHXBuKpws", new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                    youTubePlayer.cueVideo("IjUlQU6yifk");
                }

                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

                }
            });

            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);

            ll.setId(j);

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
//Cannot resolve method 'add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment)
            fragTransaction.add(ll.getId(), ysF);
            fragTransaction.commit();

            container_more_info_item.addView(ll);
        }

Solution

  • YouTubePlayerSupportFragment is not a View it is a Fragment.

    The process for adding fragments is a bit different to adding a view.

    To add a fragment:

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.your_fragment_container, ysF);
    ft.commit();
    

    https://developer.android.com/guide/components/fragments.html#Transactions