title: How to display video in VideoView in the center of the screen?
body: <p>I am creating <code>ViewPager</code> with two <code>VideoViews</code>. On sliding pages, video on current page start playing. One problem: how to display video in VideoView in the center of the screen ? </p>
  
  <p>Here is my code,</p>
  
  <h2><strong>MainActivity.java:</strong></h2>
  
  <pre><code>public class MainActivity extends Activity{
  
      private class MyViewPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener
      {
           private Vector&lt;View&gt; pages;         
           private ViewGroup myContainer;
           private int currentPageId;
           private boolean flag;
  
           public MyViewPagerAdapter(Context context, Vector&lt;View&gt; pages) 
           {
               this.pages=pages;
           }
  
           @Override
           public int getCount() 
           {
               return pages.size();
           }
  
           @Override
           public Object instantiateItem(ViewGroup container, int position)
           {
               flag = true;
               currentPageId = 0;
               int positionNow = myPager.getCurrentItem();
  
               View page = pages.get(position);
               container.addView(page);
               myContainer = container;
  
               if(positionNow == position)
                  StartVideo(container, positionNow);
  
               return page;
           }
  
           @Override
           public void destroyItem(ViewGroup container, int position, Object object)
           {
               container.removeView((View) object);
               myContainer = container;
           }
  
           @Override
           public boolean isViewFromObject(View view, Object object)
           {
               return view.equals(object);
           }
  
          @Override
          public void onPageScrollStateChanged(int arg0) 
          {
              if(flag)
              {
                  MyVideoView currVideo = (MyVideoView) myContainer.getChildAt(currentPageId+1);
                  if(currVideo != null)
                      if(currVideo.getVideoPath() != null)
                          currVideo.pause();
              }
              else
                  flag = !flag;
          }
  
          @Override
          public void onPageSelected(int arg0) 
          {
              flag = false;
              currentPageId = myPager.getCurrentItem();
              StartVideo(myContainer, myPager.getCurrentItem());
          }
  
          private void StartVideo(ViewGroup container, int position)
          {
              View page = container.getChildAt(position+1);
              MyVideoView curVideo = (MyVideoView) page;
              Log.d("Andrew", curVideo.getVideoPath());
              curVideo.start();
          }
      }
  
      private ViewPager myPager;
      private MyViewPagerAdapter myAdapter;
      private Vector&lt;View&gt; pages;
      private MyVideoView myVideoView;
      int currentPage, previousPage;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          pages = new Vector&lt;View&gt;();
  
          for(int i=1; i &lt;= 2; i++)
          {
              myVideoView = new MyVideoView(this);
              myVideoView.setVideoPath("videoPath");
              pages.add(myVideoView);
          }
  
          this.myAdapter = new MyViewPagerAdapter(this, pages);
          this.myPager = (ViewPager) this.findViewById(R.id.viewpageronecard);
          this.myPager.setAdapter(myAdapter);
  
          myPager.setOnPageChangeListener(myAdapter);
          myPager.setOffscreenPageLimit(1);
          myPager.setCurrentItem(0);
      }
  }
  </code></pre>
  
  <h2><strong>activity_main.xml:</strong></h2>
  
  <pre><code>    &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity" &gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/viewpageronecard"
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:background="#222222" &gt;
  
       &lt;/android.support.v4.view.ViewPager&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>