title: How to play Youtube Video in a Fragment in ViewPager, Android
body: <p>Now I am trying to play Youtube video in a fragment of ViewPager using YouTubePlayerSupportFragment.
  I am using SmartFragmentStatePagerAdapter as a viewpager adapter.
  But I got this error :</p>
  
  <p>W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.support.v4.view.ViewPager{41a53898 VFED.... ......I. 0,84-480,730 #7f0c00b8 app:id/viewPager}. The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 480, top: 0, right: -480, bottom: 376 (these should all be positive).</p>
  
  <p>How can I solve this issue.
  Regards</p>
  
  <p>This is FragmentPageAdapter code</p>
  
  <pre><code>public class VideoPagerAdapter extends SmartFragmentStatePagerAdapter 
  
  {
      private UserInfo usrInfo;
  
  public VideoPagerAdapter(FragmentManager fragmentManager, UserInfo userInfo){
      super(fragmentManager);
      usrInfo = userInfo;
  }
  
  @Override
  public Fragment getItem(int position) {
      VideoDetailFragment videoDetailFragment = new VideoDetailFragment();
      videoDetailFragment.video = usrInfo.videoList.get(position);
      return videoDetailFragment;
  }
  
  @Override
  public int getCount() {
      return usrInfo.videoList.size();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and I have implemented code in VideoDetailFragment.</p>
  
  <pre><code>private YouTubePlayer mYoutubePlayer;
  private YouTubePlayerSupportFragment mYoutubeFragment;
  
  private void initYoutubeVideo(){
      mYoutubeFragment = YouTubePlayerSupportFragment.newInstance();
      FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
      transaction.replace(R.id.youtube_layout, mYoutubeFragment).commit();
  
      mYoutubeFragment.initialize(DEVELOPER_KEY, this);
  }
  
  @Override
  public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
      mYoutubePlayer = youTubePlayer;
      if (!b){
          youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
          youTubePlayer.loadVideo(VIDEO_ID);
      }
  }
  
  
  
  @Override
  public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
      Log.d(TAG, "*** Youtube Player error : " + youTubeInitializationResult.toString());
      String errorMessage = String.format(getString(R.string.error_youtube_player), youTubeInitializationResult.toString());
  
      Utils.showAlert(mActivity, errorMessage);
  }
  
  @Override
  public void setUserVisibleHint(boolean isVisibleToUser) {
      super.setUserVisibleHint(isVisibleToUser);
  
      if (!isVisibleToUser &amp;&amp; mYoutubePlayer != null) {
          mYoutubePlayer.release();
      }
      if (isVisibleToUser &amp;&amp; mYoutubeFragment != null) {
          mYoutubeFragment.initialize(DEVELOPER_KEY, this);
      }
  }
  </code></pre>
  
  <p>and I have tested it in real device</p>
  
  <pre><code>06-08 19:02:47.012 2562-2562/com.videofeed.tickit W/YouTubeAndroidPlayerAPI: Cannot load modern controls UI. Upgrade to the latest version of the Android YouTube API.
  06-08 19:02:47.082 2562-2562/com.videofeed.tickit D/dalvikvm: DexOpt: couldn't find field Landroid/view/accessibility/CaptioningManager$CaptionStyle;.windowColor
  06-08 19:02:47.082 2562-2562/com.videofeed.tickit W/dalvikvm: VFY: unable to resolve instance field 10579
  06-08 19:02:47.082 2562-2562/com.videofeed.tickit D/dalvikvm: VFY: replacing opcode 0x52 at 0x0019
  06-08 19:02:47.082 2562-2562/com.videofeed.tickit W/YouTubeAndroidPlayerAPI: Forcefully created overlay:vtd@41fd4d28 helper:Lazy@41fd5898 view:null status: ...... {...}
  06-08 19:02:47.372 2562-2562/com.videofeed.tickit W/YouTubeAndroidPlayerAPI: Cannot load modern controls UI. Upgrade to the latest version of the Android YouTube API.
  06-08 19:02:47.442 2562-2562/com.videofeed.tickit W/YouTubeAndroidPlayerAPI: Forcefully created overlay:vtd@42c9fe20 helper:Lazy@42c9fe98 view:null status: ...... {...}
  06-08 19:02:47.462 2562-2562/com.videofeed.tickit I/Choreographer: Skipped 40 frames!  The application may be doing too much work on its main thread.
  06-08 19:02:47.602 2562-2562/com.videofeed.tickit W/YouTubeAndroidPlayerAPI: Cannot load modern controls UI. Upgrade to the latest version of the Android YouTube API.
  06-08 19:02:47.692 2562-2562/com.videofeed.tickit W/YouTubeAndroidPlayerAPI: Forcefully created overlay:vtd@42d8f2a8 helper:Lazy@42d8f320 view:null status: ...... {...}
  06-08 19:02:51.912 2562-2604/com.videofeed.tickit D/dalvikvm: GC_FOR_ALLOC freed 15971K, 69% free 7899K/25052K, paused 35ms, total 37ms
  06-08 19:02:58.202 2562-2562/com.videofeed.tickit W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.support.v4.view.ViewPager{42c4a610 VFED.... ......ID 0,84-480,730 #7f0c00b8 app:id/viewPager}. The distances between the ancestor's edges and that of the YouTubePlayerView is: left: -480, top: 0, right: 480, bottom: 376 (these should all be positive).
  06-08 19:02:58.222 2562-2562/com.videofeed.tickit W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is not contained inside its ancestor android.support.v4.view.ViewPager{42c4a610 VFED.... ......ID 0,84-480,730 #7f0c00b8 app:id/viewPager}. The distances between the ancestor's edges and that of the YouTubePlayerView is: left: 480, top: 0, right: -480, bottom: 376 (these should all be positive).
  </code></pre>
  
  <p>this is log in Android studio.
  I have attached the screenshots.
  <a href="https://i.stack.imgur.com/1fO7F.png" rel="nofollow noreferrer">This is the screenshot</a></p>