You really don't have to set the layout parameters programmatically just to set the width and height to match_parent: this can easily be done using fundamental xml declarations.
If you need something more advanced that you didn’t specify — for example, adjusting fields or adding views dynamically, consider packing the ViewPager with a layout appropriate for your case. For instance:
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/view_pager_full_photo" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
In this case, to adjust the fields (for example):
ViewPager pager = (ViewPager) findViewById(R.id.view_pager_full_photo); ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) pager.getLayoutParams(); lp.topMargin += TOP_MARGIN_HEIGHT_PX;
and etc.
Note that in this case, the layout options returned by pager.getLayoutParams () are RelativeLayout shell options that can be more easily manipulated to suit your needs.
source share