0

I have a single ViewPager in my activity. The xml code of my activity is -

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
         android:id="@+id/pager"     
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

I want to know it's Height and Width (which in this case should be the entire area covered by my application). Problem is I want to know these sizes before I send data to viewpager.

I tried to use as following -

pager= (ViewPager) findViewById(R.id.pager);
int width = pager.getWidth();
int height = pager.getHeight();

but both of them always returns 0.

Why is returning to Zero, when I have set properties to "match_parent"?

and How to get the dimensions?

Edit 1 - At present activity xml has viewpager alone, should I place it in linear/relative layout to find dimensions?

Thanks. Regards,

Dr. Atul Tiwari
  • 1,085
  • 5
  • 22
  • 46
  • Sorry my answer didnt help. Have you tried looking at http://stackoverflow.com/questions/9313554/measuring-a-viewpager – AmmarCSE Jul 20 '14 at 22:06
  • 1
    @AmmarCSE I have looked at that. Sorry, being a medical doctor (no programming background), it's hard for me to grasp that code. Instead I just tried `ViewTreeObserver viewtreeobserver = pageView.getViewTreeObserver();` and then checking dimensions in `if (viewtreeobserver.isAlive())` works for me. Dont know why or how. but it's working so far. – Dr. Atul Tiwari Jul 20 '14 at 22:12

1 Answers1

0

Even though you use getWidth() and getHeight() of ViewPager, you can't get any dimension. So I tried getting height of ViewPager by using child height with any margin and padding that you used.

kimkevin
  • 2,202
  • 1
  • 26
  • 48