1

I am creating ViewPager with two VideoViews. On sliding pages, video on current page start playing. One problem: how to display video in VideoView in the center of the screen ?

Here is my code,

MainActivity.java:

public class MainActivity extends Activity{

    private class MyViewPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener
    {
         private Vector<View> pages;         
         private ViewGroup myContainer;
         private int currentPageId;
         private boolean flag;

         public MyViewPagerAdapter(Context context, Vector<View> 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<View> pages;
    private MyVideoView myVideoView;
    int currentPage, previousPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pages = new Vector<View>();

        for(int i=1; i <= 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);
    }
}

activity_main.xml:

    <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" >

    <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" >

     </android.support.v4.view.ViewPager>

</RelativeLayout>
user2060383
  • 979
  • 1
  • 14
  • 32
Andrew
  • 133
  • 3
  • 13
  • Just Refer this. it little usefull..[enter link description here][1] [enter link description here][2] [1]: http://stackoverflow.com/questions/12211837/full-screen-videoview-without-stretching-the-video [2]: http://stackoverflow.com/questions/8471618/center-videoview-in-landscape-mode – harikrishnan Feb 21 '13 at 12:45
  • Thank's for links. In my case, is the parent of VideoView - ViewGroup, not Relative or Linear layouts. – Andrew Feb 21 '13 at 12:55
  • Try to set ViewGroup to center of the View. Like we do in xml. View.centerInParent = true; try something like this – Umer Abid Feb 21 '13 at 13:01
  • I set LayoutParams to container, but no reaction.`@Override public Object instantiateItem(ViewGroup container, int position) { RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.CENTER_HORIZONTAL, 1); params.addRule(RelativeLayout.CENTER_IN_PARENT, 1); params.addRule(RelativeLayout.CENTER_VERTICAL, 1); container.setLayoutParams(params); .... return page; }` – Andrew Feb 21 '13 at 13:43

1 Answers1

1

Although it is an old question, perhaps someone else is struggling with it, too, as I had the same problem. I solved it by changing

 android:layout_centerInParent="true"
 android:layout_centerVertical="true"
 android:layout_centerHorizontal="true"

to

 android:layout_gravity="center"

in the xml for the VideoView I used to populate the ViewPager.

Dominikus K.
  • 273
  • 3
  • 14