how to use dip and sip in android - java

hello and hi everyone,
can anyone suggest me how to use device independent pixels(dip),sip and convert px to dip with a sample code..

Check this out.
Density-independent pixel (dip)
A virtual pixel unit that applications can use in defining their UI, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the dip units needed, based on the actual density of the screen in use. The conversion of dip units to screen pixels is simple: pixels = dips * (density / 160). For example, on 240 dpi screen, 1 dip would equal 1.5 physical pixels. Using dip units to define your application's UI is highly recommended, as a way of ensuring proper display of your UI on different screens.

Related

Exactly same size for all screens physically in Android

how is it possible that dimensions of any object physically same size on all screens?
From the android developer's documentation:
To preserve the visible size of your UI on screens with different
densities, you must design your UI using density-independent pixels
(dp) as your unit of measurement. One dp is a virtual pixel unit
that's roughly equal to one pixel on a medium-density screen (160dpi;
the "baseline" density). Android translates this value to the
appropriate number of real pixels for each other density.
For example, consider the two devices in figure 1. If you were to
define a view to be "100px" wide, it will appear much larger on the
device on the left. So you must instead use "100dp" to ensure it
appears the same size on both screens.
So, what you are trying to achieve is possible using dp as the unit for the height and width properties of the object.

Android supporting multiple screens and standardizing UI, dp scale differs on some screens

We are working on an android app, and we have encountered an issue where devices in the same density bucket, i.e. "xxhdpi" do not all look the same, i.e. the Pixel and Pixel 2. We have a very image heavy UI and are using a Constraint view to organize the placement of several overlaid images. The Navigation drawer at the top of some of these screens further exacerbates the issue, as the margin is thicker with different densities on the same screen size and the rest of the app in squeezed into a smaller frame.
Heres an example of how different the Pixel and Pixel 2 are rendered:
Pixel 2 vs.
Pixel
We have tried making more specific layouts to match more specific ranges of DPI but the simulator groups the same devices together no matter what we try. These are the layout categories we've tried, where sw320dp for example refers to a minimum screen width.
Is there a way to combat this issue with constraint view features such as a constraint anchor or percentage constraints? Alternatively can we more narrowly define our layout categories or do something about the dp scale not being the same for similar devices? We have already consulted this page as well as numerous stack overflow posts: Android's Guide on Supporting Multiple Screen Sizes.
Any suggestions, comments, or specific questions welcome, thanks in advance to anyone who takes the time to read this.
This is my approach for getting the image to be shown completely without cropping on the edges, hope you get some kind of idea from this to tackle your situation.
Problem here is with wrap_content and match_parent, though pixel and pixel 2 falls in same category that is "xxhdpi" for getting the image from density bucket but they have little bit change with their width, pixel: 143.8 x 69.5 x 8.5 mm (5.66 x 2.74 x 0.33 inches) and pixel2: 145.7 x 69.7 x 7.8 mm (5.7 x 2.7 x 0.31 inches)
So I am guessing you probably have match parent for width and image scale type as center crop so image is scaled accordingly zooming in, so because they have different widths they are zoomed in more on one compared to the other.
I had similar kind of situation so what I did was used webview with match parent as width and wrap content as height then I displayed image in the webview
WebSettings settings = webView.getSettings();
settings.setLoadWithOverviewMode(true);
here setLoadWithOverviewMode actually zooms out and fits the image so no matter what you always see the whole image.

Correct calculation from ldpi dp values for mdpi and so on

I'm having a gridview for which I determine the cell size like this (in the xml file):
android:columnWidth="#dimen/gridview_cell_dimen"
As you can see I am getting the dp dimension from my dimens folders (values-ldpi, values-hdpi,...).
My question now is - how to determine the correct dp sizes?
For ldpi I simply use 20dp in this case, which perfectly fits the size from my smartphone. What would be the correct forumla to see which size would be correctly suitable for bigger screen resolutions like mdpi, hdpi, xhdpi, xxdpi?
Is there a rule of thumb which says: "Increase the size by 120%?" for example? So that I can say that the best resolution for mdpi would be: 24dp ? and for hdpi: 28dp in my case?
Thanks in advance!
using dp as the unit is independent of the density of the screen, so you can stick to one value for all screen sizes.
From the Documentation:
Density-independent pixel (dp) A virtual pixel unit that you should
use when defining UI layout, to express layout dimensions or position
in a density-independent way. The density-independent pixel is
equivalent to one physical pixel on a 160 dpi screen, which is the
baseline density assumed by the system for a "medium" density screen.
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
The conversion of dp units to screen pixels is simple: px = dp * (dpi
/ 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical
pixels. You should always use dp units when defining your
application's UI, to ensure proper display of your UI on screens with
different densities.

Separation between components doesn't change when screen resolution changes. Why?

I have created a form in Swing with 10 components (5 labels and 5 textfields), using Gridbaglayout.
When my screen resolution changes, the separation between the components is not changing. Why?
GridBagLayout uses pixels as the unit of measure. You can use java.awt.Toolkit.getScreenResolution() to get the pixel density and then scale your spacing accordingly. You also might find java.awt.GraphicsConfiguration.getNormalizingTransform() useful; it allows you to work in a coordinate system where 72 units is 1 physical inch.

Android: Scale fixed Layout down

i want to scale my android application, basically i developed it for 800x480 display with a AbsoluteLayout. This is necessary for my Application, i can't change it.
Now the Problem:
When i start my app in the Emulator the buttons disappear and the images are extremly large. I thought android would scale applications with a fixed size down by default, but it does not work. I already tried to manipulate the manifest but this did not work.
I use a ImageView component for graphics.
Targeting Android 2.1
Cheers
Felix
It is definitely not ideal to use AbsoluteLayout. But, if you want to just push through with it, you should switch the units of all your co-ordinates and sizes away from px (pixels) to dp (density independent pixels). You will have to scale all of your existing co-ordinates by a factor of 2/3 to start, since 1 dp = 1.5px at the density that your layout targets (hdpi).
You will need to explicitly specify the sizes of all your images and layouts. If, for example, you had a button that was 30px wide and 120px tall, then it will become 20dp wide and 80dp tall.
Of course, the images won't look great on smaller (mdpi) screens, since they will be scaled to 2/3 size. Also, some devices are fixed to landscape mode, where you will definitely encounter layout problems. So it's not pretty, but it may get you over the finish line, depending on your requirements.

Categories

Resources