Support new measurement unit to use in android layout - android

As far as I know, different measurement units can be used for width, height, padding in android xml layouts. I know that dp is the best to use but I would like to define two new measurement units called "wp" (width percentage) and "hp" (height percentage). I would like to express width, height, padding using this unit. A button with a width of 50wp would be large as half of the screen width. I would like to redefine the point where dp, px are interpreted and introduce this new measurement units. How can I accomplish this? What should I modify?

Related

Is it a good approach to give Layout height and width in dp

I'm quite new with Android so I was wondering if it's a okay to give layout height or width in dp? Also if there is any other approach other than wrap_content/match_parent or dp than do tell. Thanks in advance.
Yes you can specify height and width in dp.
For conversions between px, dip, dp and sp please see stackoverflow question What is the difference between “px”, “dip”, “dp” and “sp”?
For layout I've found "match_parent" to be applicable in most cases. (To give you more context, "match_parent" used to be called "fill_parent" prior to API level 8). It basically means the view is as big as its parent, just without padding.
If your intention is the make the view just big enough for its content, then use "wrap_content".
Yes it is ok to give height & width in dp. You can also use fill_parent at place of match_parent.
Fill_parent was depreciated in API level 8. So if you are using API level 8 or above you must avoid using fill_parent.
For more information see this http://code2care.org/pages/fill_parent-vs-match_parent-vs-wrap_content/
You can give layout height and width in dp. Mostly used for custom size arrangement
Wrap content provides the view size, equal to the content size.
Match parent allows the view to be the same size, as the Relative or Linear layout.
Ofcourse not this is the not a best approach to give height and width in dps instead wrap_content and match_parent properties.
Documentation
The important thing is if you give width and heights in dp it is not in favour of supporting all different size of screens instead wrap_content and match_parent is much more favourable in adaptive designs and responsive designs where views get the provided space according to the need.
dp is defined as:
dp Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

difference between static and programatic declarations in Nexus tablets lyouts

when declaring some element (let's say a Button) and giving it some width and height (let's say 200dp) from the XML file, I got certain result when running, although when make the same steps but Programmatically I got much smaller width and height, and this case happens only with me in Nexus tablets.
If you set the size of a View programmatically, many times pixels are taken as an argument. You will need to convert your desired size in DPs to pixels first, and use those to set the width or height of your element:
float dp = 200;
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
See the API-docs of the specific Views for infos about what kind of dimensions are taken as an argument.

Android: Is line length in PX or DIP?

I have a custom view.
When I draw a line from (x,y) to (x+10,y), is the distance in DIP or PX?
As documentation says,
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
Thus, all units are in dp.
If you are using one of the Canvas.drawLine methods it is in PX
I believe it's in pixels. If you want to know how to convert from DP to pixels as in if you want your custom view to work with DPs just like all other Views.
Converting pixels to dp

Android Change Margin on Button in Layout

I have a button which on start is placed central and 50 pixels from the top of the layout using the following
android:layout_marginTop="50px"
I need to be able to change this margin depending on what background is being displayed to 100 pixels.
any ideas how i change this
There must be a one liner all the answers i can find involve a long layout params method
Any help is greatly appreciated
Mark
Option 1 - Relies on the surrounding/parent layout type, so if the parent layout type is a RelativeLayout, you should use RelativeLayout.LayoutParams instead of LinearLayout.LayoutParams:
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 100, 0, 0);
myButton.setLayoutParams(params);
//myButton.requestLayout();
Option 2 - Uses a generic method that doesn't rely on the surrounding/parent layout type:
public static void setMargins (View v, int left, int top, int right, int bottom) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(left, top, right, bottom);
v.requestLayout();
}
}
Usage:
setMargins(myButton, 0, 100, 0, 0);
this is for your reference...
i hope it will help full to you...
and the best use is dp supports for all device and its density pixels
px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
mm
Millimeters - based on the physical size of the screen.
pt
Points - 1/72 of an inch based on the physical size of the screen.
dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

PopupWindow's size in px or dip?

From PopupWindow Docs :
public PopupWindow (int width, int height)
Added in API level 1
Create a new empty, non focusable popup window. The dimension of the window must be passed to this constructor.
The popup does not provide any background. This should be handled by the content view.
Parameters
width the popup's width
height the popup's height
Question: does the width and height is in pixels (px) or Density-independent Pixels (dip) ?
Bonus Question: how can the PopupWindow achieve Multiple Screen Support ?
The width and height parameters are specified in pixels (px). If you want to convert to density independent pixels (dp), you can use the following:
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 65, getResources().getDisplayMetrics());
Where 65 is replaced by the number of density-independent pixels. This returns the number of pixels which you can then give to PopupWindow.

Categories

Resources