difference between static and programatic declarations in Nexus tablets lyouts - android

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.

Related

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.

Image Height Appear Different on different density

I'm new in designing the layouts. I am getting the bitmap from the server and I'm setting the imageview height such that it look good on every density either ldpi, mdpi, hdpi, xhdpi.
stripImageView.getLayoutParams().height = (int) ((Util.screenWidth(this) * strip.getHeight() / strip.getWidth()) * getResources().getDisplayMetrics().density);
But This code not working for me. Some device images looking very small and Some device it look fine
Try just setting the dimensions in your layout using dp instead of px. Dp (or dip) stands for density independent pixels and will automatically scale based on density.
EDIT: another issue I see is that you're referencing both the screen width and height so your screen aspect ratio would also affect that calculation.
convert them to DIP:
stripImageView.getLayoutParams(). height =
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>,
getResources().getDisplayMetrics());
where HEIGHT is the height in px you want. You could base it off the strip height or something.

dpi on activities and on XML

Why dp used in the XML files are not the same as dp used in activities? When I create a textView in the XML file with 20dp of width, its size will be the same in all different type of screens, but when I use this:
int sizeInDip = 20;
int width= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDip, getResources().getDisplayMetrics());
The size is different in all types of screen.
I want to know if there's a way to get the same result using dp in both, activities and XML files.
Thank you in advance!
Why are you doing it that way? IIRC, theres a TextView.setSize method you can invoke programatically and just set the size as 20dp that. Also, you should use sp(scaleable pixels) for text sizes

Specify height width of the view to support multiple screen sizes?

I have created the activity with a button. I need to specify the height and width of the button like below.
Button btn=new Button(this);
btn.setWidth(100);
btn.setHeight(100);
For multiple screen support is it a right way to specify the size of the view . Guide me to set the height and width to the view.
It really depends on how you want to determine the button size. Basically you have two options
Constants like FILL_PARENT or WRAP_CONTENT
A number
If you choose the 2nd way, you must also choose a strategy, eg
30% of the screen/parent width (so it shrinks/grows depending on the screen actual size)
1 inch on all screens
The former is trivial: just get the width of the target element (either via DisplayMetrics if it's the whole display, or with getWidth() if it's a View) and multiply it by your coefficient.
The latter requires you to know the density of the display, ie how big is a pixel on the device. For example by doubling the density, the pixel width will halve
DENSITY PIXEL SIZE
100 dpi 1/100 in
200 dpi 1/200 in
So if you want your button to be 1 inch wide on all possible devices, you can use
button.setWidth(1 * (int) metrics.xdpi); //1 inch * DisplayMetrics.xdpi
There is a little variant if you know the size in pixels on a MDPI device, ie a device where one DIP is one pixel on an approximately 160 dpi screen. In this case you can use the following
button.setWidth(100 * metrics.density);
This is the javadoc for DisplayMetrics.density:
The logical density of the display. This is a scaling factor for the
Density Independent Pixel unit, where one DIP is one pixel on an
approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
providing the baseline of the system's display. Thus on a 160dpi
screen this density value will be 1; on a 120 dpi screen it would be
.75; etc. This value does not exactly follow the real screen size (as
given by xdpi and ydpi, but rather is used to scale the size of the
overall UI in steps based on gross changes in the display dpi. For
example, a 240x320 screen will have a density of 1 even if its width
is 1.8", 1.3", etc. However, if the screen resolution is increased to
320x480 but the screen size remained 1.5"x2" then the density would be
increased (probably to 1.5).
#Karthick If your button is not used in xml of android layout then you can do below thing for set Button width and Height.
Use Below code to get known what is device height and width.
int public static int LDPI_BUTTONSIZE=100;
int device_height;
int device_width;
WindowManager wm=getWindowManager();;
DisplayMetrics dm=new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
device_width=dm.widthPixels;
device_height=dm.heightPixels;
From Above Code you will decide in which screen of your android device ,for Example you can refer this link of developer click here. in that Refer Table No 3.
Now if your device is LDPI then you can set Button value like this.
Button btn=new Button(this);
btn.setWidth(LDPI_BUTTONSIZE);
btn.setHeight(LDPI_BUTTONSIZE);
For this purpose you need to convert pixels to dp's. You can do this with TypedValue.
Example:
int buttonWidthInPixels = 100;
float buttonWIdthInDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
buttonWidthInPixels, getResources().getDisplayMetrics());
button.setWidth((int) buttonWidthInDp);
If instead you want to convert to SP (for text), use TypedValue.COMPLEX_UNIT_SP as the first parameter for applyDimension(). See this page on the Android dev guide.

Android - setting layout params to TextView in millimeter

I believe you can set layout_width and layout_height to textview element dynamically using something like:
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
centimeterLayout.addView(textview, lparams);
But - how do I set the layout width and height in other units - e.g. I want to set the layout width of my text view to 20 mm. How do I do that?
LayoutParams either take one of the constants (FILL_PARENT, WRAP_CONTENT) or a pixel value. Since you want to use millimeters, you have to convert a millimeter dimension to a pixel dimension. Doing so is pretty easy:
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 20,
r.getDisplayMetrics());
In this example, 20 mm are converted into a pixel dimension that you can use in your LayoutParams. You can use other dimensions too, such as TypedValue.COMPLEX_UNIT_DIP for dp/dip¹. Just change the constant in the arguments. The android doc has a list of useable dimensions.
¹ Density independend pixels, a highly recommended dimension that adjusts to the many different screen sizes and densities. Using mm isn't the best way to do things, since your text will probably look okay on a big screen and fill a big portion of a small screen, or the other way around - which is not ideal. I also recommend sp for font sizes. See the dimension list for details.

Categories

Resources