PopupWindow's size in px or dip? - android

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.

Related

how to calculate the actual text size display?

I have a textView in another view.
I want to calculate in runtime the actual display size of that text
and of its container.
If the text is too big to fit in the view I have few shorted text content alternatives.
I have read the textView doc, but it only shows [setTextSize][1]
how can i get the text size (in dp I guess ?) according to the actual screen.
(orientation, screen size, font size and so on)
TextView actually has a getTextSize() method, which returns the font size in pixels for you. You can check here the documentation. Of course this pixel value will vary accordingly to the screen density of the device. If you want to convert that value for dp or sp on runtime, you can do the following:
Pixels for SP:
float sp = px / getResources().getDisplayMetrics().scaledDensity;
Pixels for DP:
float dp = px / getResources().getDisplayMetrics().densityDpi;

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.

how can i know the actual size of an imageView in my device?

I have an ImageView with matchParent property in width.
how can i know its runtime width on my device (using eclipse, without adding code programmatically)?
how can I know the conversion ratio between dpi to pxls in my device?
I have an ImageView ... how can i know its runtime width on my device
You can get the width when the ImageView is measured, i.e. later than the runtime. However you can get the screen's width and height like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
And if for example your ImageView takes up a specific portion of your screen you can measure it using the screen's width and height.
how can I know the conversion ratio between dpi to pxls in my device?
To convert say 20 pixels to DP do (adding to the above code):
int dp = 20 / metrics.density:
Extra: Here's a dpi to pixel calculator
If you don't want to use the TreeObserverListener way, you can try to get the View to measure itself and give you what the dimensions would be if it were calculated. In my experience, this has worked in most cases but not all; especially if the View is in some special dynamic layout or the ordering of the hierarchy prevents the dimensions from being calculated in the correct order. You might find luck in refering to this popular answer.

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.

Support new measurement unit to use in android layout

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?

Categories

Resources