4

I have the following code but I get an exception

java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.question_details_full_photo_view_pager);
    Bundle bundle = getIntent().getExtras();
    ArrayList<String> imageUrls = bundle.getStringArrayList("imageUrls");
    ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(this, imageUrls, true);
    
    android.support.v4.view.ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager_full_photo);
    
    
    android.support.v4.view.ViewPager.LayoutParams layoutParams = new LayoutParams();
    layoutParams.width = LayoutParams.MATCH_PARENT;
    layoutParams.height = LayoutParams.MATCH_PARENT;
    viewPager.setLayoutParams(layoutParams);
    
    
    viewPager.setAdapter(imagePagerAdapter);
}
Vel
  • 9,027
  • 6
  • 34
  • 66
124697
  • 22,097
  • 68
  • 188
  • 315
  • What are the imports you're using? – g00dy Jul 24 '13 at 10:47
  • A quick solution can be (but not necessairly) to set: instead `android.support.v4.view.ViewPager.LayoutParams layoutParams = new LayoutParams();` -> to `android.support.v4.view.ViewGroup.LayoutParams layoutParams = new LayoutParams();` – g00dy Jul 24 '13 at 10:49

3 Answers3

7

You really shouldn't be setting the layout params programmatically just for setting up width and height to "match_parent": this could easily be done using fundamental xml declarations.

If however there's something more advanced you need that you didn't specify - such as adjusting margins or adding views dynamically, consider wrapping the ViewPager with a layout matching your case. For example:

<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 margins (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;

etc.

Note that in this case the layout params returned by the call to pager.getLayoutParams() are those of the wrapping RelativeLayout - which could be more easily manipulated to your fit your needs.

Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
d4vidi
  • 2,407
  • 26
  • 27
3

This is because you misunderstood the LayoutParams in this context : setting the layout params of your ViewPager will layout it out in its parent which is a ViewGroup. You have therefore to pass an instance of ViewGroup.LayoutParams.
If you want rather to lay out a component in your ViewPager, you have to set the layout params of the child with an instance of ViewPager.LayoutParams

Antoine Marques
  • 1,379
  • 1
  • 8
  • 16
  • ViewGroup.layoutParams also throws the same exception. it seems to be expecting android.view.ViewGroup$MarginLayoutParams but there is no such thing – 124697 Jul 24 '13 at 11:10
  • @code578841441 there is [http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html](http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html) You need to use the LayoutParam from `ViewPager`'s parent not the `ViewPager` itself. And I just forgot this.. :) – Zyoo May 06 '14 at 17:37
0

val pager = findViewById(R.id.view_pager);

val params = CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT); params.setMargins(44, 44, 44, 44); pager.layoutParams = params