0

I have a Device with 540 pix screensize(width). I try this layout:

<LinearLayout 
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="540dp"
>

But the LinearLayout is bigger than the screen. It ranges out of the screen.

I read out the screensize by code:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;

and Tools, so the screen is 540 pix width, definitely.

Whats wrong?

  • can you change the dp to px and try it out again? dp is different from px – ucsunil Feb 17 '14 at 21:39
  • For info, take a look at: http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-in-android – NigelK Feb 17 '14 at 21:40

2 Answers2

0

The unit you are using in your layout file is 'dp' while the actual unit is 'px' - there is a difference between the two. Values given in 'dp' are scaled by the device. Setting your value to 540px should solve your issue.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
0

The issue is that dp (formerly dip as an acronym) stands for "density independent pixels" so it scales to the screen size. You can either set the unit to 'px' or if you're trying to fill the screen, more accurately would be using "match_parent".

There's an extremely helpful article here

Wes Winn
  • 551
  • 5
  • 15