Graphics and units

Erel

B4X founder
Staff member
Licensed User
Longtime User
Basic4android includes two special numeric units which are helpful when you create graphics or dynamic layouts and you want to support devices with different scales and dimensions.

dip
DIP - Density Independent Pixel.
Dip values are written related to the default scale, which is 160 DPI (not DIP ;) ) - Dots Per Inch.
At runtime these values are scaled based on the device scale.
For example:
B4X:
Button1.Width = 100dip
This button "physical" size will be the same whether the user device screen is a high resolution device or a low resolution device.
Writing 'dip' value is almost always preferred over writing absolute position values.

%x, %y
Percentage of the activity's width and height respectively (Activity is similar to Form).

For example:
B4X:
Button1.Width = 50%x
Button1.Height = 25%y
This button width will always be half the width of the activity and the height is quarter of the activity height.
The percentage units are useful for tabular layouts and to make sure that your layout or graphics always fit (especially with orientation changes).

And some code:

graphics.png


The saved image:

graphics-result.png
 

JTmartins

Active Member
Licensed User
Longtime User
Is dip de default B4A unit ?

I mean, if we have

B4X:
ImageView1.Width=63
(wich throws a compiler warning)

is it the same as if we have

B4X:
ImageView1.Width=63dip
?
Thanks.
 
Upvote 0
Top