dip?

aklisiewicz

Active Member
Licensed User
Longtime User
I was looking for this line explanation in HELP

ColLineWidth = 1dip

all I have found about 'dip' is this

; Density As Float Dim DipToCurrent (Length As... length arrays. DipToCurrent (Length As Int ) As Int Scales the value, which... devices. Button1.Width = DipToCurrent(100) Note that a shorthand syntax for this method is available



and I found this confusing,... or I should say, from this description I know pretty much nothing!

Arthur
 

klaus

Expert
Licensed User
Longtime User
A dip is a density independent pixel.
When you set in the code positions and sizes of view it is bette to declare them with dips.
Example:
B4X:
Button1.Width = 60dip
The above code means that the physical width of the butoon is:
60 pixels on a device with the standard density of 160, scale 1.
90 pixels on a device with a density of 240, scale 1.5.
45 pixels on a device with a density of 120, scale 0.75.
That means that the OS mutiplies the dimension by the scale factor.

Best regards.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
A dip is a density independent pixel.
When you set in the code positions and sizes of view it is bette to declare them with dips.
Example:
B4X:
Button1.Width = 60dip
The above code means that the physical width of the butoon is:
60 pixels on a device with the standard density of 160, scale 1.
90 pixels on a device with a density of 240, scale 1.5.
45 pixels on a device with a density of 120, scale 0.75.
That means that the OS mutiplies the dimension by the scale factor.

Klaus -- I'm trying to make sure that I have all this straight for the Wiki.

If we say Button1.Width = 60, then the button would take up 15% of the screen on a 400x240 screen width and 7.5% of the screen width on an 800x480 screen of the same physical size (say 4"). So by using Button1.Width = 60dip, Android will scale the width up from 60 to 90, as you said above; however while 60/400=15%, 90/800 is only 11%, so the scaling will be off some.

So does Android base its scaling strictly on the 1 vs 1.5 density, or does it adjust for each device's actual dpi?

For example, the Galaxy SGS 4" device has a physical screen width of 3.5" and a resolution of 800x480. Dividing 800 by 3.5 = 228 dpi. The Motorola Defy is a 3.6" device with a screen width of about 3.25" and a resolution of 854x480 where 854/3.25 = 263 dpi. So while both devices are considered to have a density of 1.5, it would seem that Android would still need to adjust the sizes and positions of views to get the same layout to fit the same on both devices, or does it not try to get them precisely the same in such a case and that's when we have to use activity.height/width to fine-tune the adjustments as shown in this post?

Also, the Galaxy SGS's height is about 1.875" which divided into 480 gives a vertical dpi of 256, but as calculated above, its horizontonal dpi is 228. So how does Android adjust for this difference so that a 100x100 (dip or not) square still shows up as square?

Also, a layout done on a 4" device with 240 dpi (scale=1.5) and a Button1.Width = 60, will still have a button width of 60 on a 10" device with a resolution of 2100x1200 because such a device still has a dpi of 240 (scale=1.5), but it will appear to be much smaller on the higher resolution screen.

In fact, the entire layout done on a 800x480 Galaxy SGS would fit in the upper left quadrant of a 10" screen with 2100x1200 resolution, and that's the reason we normally have to create different layouts for different size devices, right? Or am I missing something about how this all works?
 
Last edited:
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Android currently defines four scaling values: Supporting Multiple Screens | Android Developers
0.75, 1, 1.5 and 2. You are right that the real "dots per inch value" can differ a bit than those values. However the scale reported by each device will match one of these four values.

So does Android adjust the layout to each device's real dots per inch value, or just to the scale? If just the scale, then the layout will not be exactly the same on two different devices with the same scale but different real dpi's, and we may have to adjust for that difference on crowded layouts to make everything fit.

Since I only have one actual device, I have no way of testing this.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Adjustment is done based on the "scale" value not the physical dots per inch. However as a developer you should not be concerned about it. On a slightly larger phone your layout will look slightly larger. I think that it is more than reasonable.

The problem isn't so much with a "slightly larger phone" but with one which is smaller but has the same scale as the device/emulator used to create the layout.

Let's say that I do my layout on a 7" Archos 70, which has a resolution of 800x480 (scale=1), then someone runs it on a 7" Galaxy Tab, which has a resolution of 1024x600 (also scale=1).

Because both devices have a nominal density of 1, Android will not do any resizing.

Going from the Archos to the Galaxy Tab, I will get the full layout, but with unused blank space of 224 pixels on the right (landscape) and 120 on the bottom.

That isn't very professional looking, but it's even worse if you do a layout on the Tab which uses the full screen and someone runs it on the Archos, since as I understand things, 224 pixels will be lopped off on the right and 120 lopped off the bottom, which would effectively prevent the use of my app.

Is this correct?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I just wanted to make shure that there is no confusion, not only for you.

Designing a program for almost all devices would need a lot of different layouts. Even within the two major groups: smartphones and tablets.
Smatphones with a resolution of 480/800 have extra pixels on the bottom or on the right in comparison with a device with the standard resolution 280/480.
With tablets it's even worse as you have shown it.
Writing a program for both groups will also need some changes in the code for the management of the user interface as tablets have 4 times the physical surface of smartphones.

Best regrads.
 
Last edited:
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Now after reading this I'm stumped as to whether it's best to use lots of layouts or dips in code for layouts.

My app is (will be) made up of activities that contain views like tabhosts and listviews/scrollviews that I want to fill width. Some will fill height, others will have the likes of a button at bottom. Currently the couple of parts I've done use layouts but was thinking it may be better to use dips and if it is better to 'code and dip' then convert the parts I've done now while it's in the early stages.

Any views appreciated. cheers
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The dimensions you enter in the designer are dip values.

You can design your layouts in the Designer.
Even if you design for a 320 * 480 pixels density 160 device (emulator)
it will be shown correctly on a device with 480 * 720 pixels density 240.
Where you need to add some code is for a device with 480 * 800 pixels dendity 240 because of the 80 extra pixels that you must handle.

Have a look at chapter 5. Screen sizes and resolutions in the Beginner's Guide.

Best regards.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Ha. That is AWSOME. That solves everything. Was that chapter there when I was reading the Beginners Guide other week. Read it front to back and don't remember that bit.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
It was in edition 1.0 not in the beta edition :).

Best regards.

Time for a re-read then me thinks. See what else I can pick up. May save some forum server bandwidth if I read it and answer my own questions lol
 
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
so how does the scale factor can be implemented (if necessary) ?
I do see the few variants in screen designer but there is no scaling factor or anything alike ?

Arthur
 
Upvote 0
Top