2

I need to draw UI which is exactly twice the screen width, scrolled within HorizontalScrollView.

Pseudocode is like that:

<HorizontalScrollView width=device_screen>
  <myUI width=device_screen*2>...</myUI>
</HorizontalScrollView>

I do not want to set exact width in px/dp for each device on the market, but I did not find a way to set view width declaratively for ANY device relatively to its parent.

It there any way for that?

Mix
  • 3,081
  • 2
  • 17
  • 14

2 Answers2

1

You can use WindowManager class to get current display info, multiply by 2 and assign it to the width of the view. Something like this:

WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();

int view_width = 2*d.getWidth();
Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • Although this is not pure XML based solution, but it seems the only way. Thanks – Mix Sep 04 '12 at 22:05
0

This is the same question as this one. There are two open source libraries which implements what you need included in the best answer there. It will allow you to add as many horizontal page views as you need.

However, if you don't need views and can get away with using Fragments, you should use ViewFliper, etc. with native implementations.

Community
  • 1
  • 1
Edison
  • 5,961
  • 4
  • 23
  • 39
  • My app is simple and I had an idea to rely on Android XML mark up, seems it's not feasible and I need to resize views in code. Thanks for advice anyway! – Mix Sep 04 '12 at 22:03
  • 1
    Using HorizontalPager, it is pure XML markup, you can just mark it the same way as a LinearLayout with horizontal orientation. – Edison Sep 04 '12 at 22:54