My problem is very simple.
I have a button that have 100dp marginRight and 100dp marginTop in a xxhdpi density screen.
The problem is, when I change to another density screen or size, the button its not in position I want. It remains the 100dp margin top and right but I don’t won’t this.
I thought that if I use dp, when I change screen it’s was going to be in the position I want, calculate de position in smaller or bigger screens.
I hope you understand, I don’t want to create a layout for every density or size screen.
How can position a button that be in the same position in every screen?
on a different screen density that 100dp will change position.
what you can do is support multiple size and create different layout for each.
read http://developer.android.com/guide/practices/screens_support.html.
Using the new size qualifiers is a solution.
Related
In my activity, I use DisplayMetrics to dynamically get the pixel height and width of the screen, and then I assign each of the components in the Activity sizes based on those dimensions. I wanted to know how this could be affected by screens that have different densities? Is it a good idea to use pixels?
Edit:
The purpose of using pixels dynamically is so that my layout scales based on the given screen. I just want to know how density will play into this. For example, if I have two screens with a height of 1024px and width of 800px, but one is twice as dense as the other, and I want to use 40% of the height and 40% of the width (this is just hypothetical) for a button, why should the density matter? This will just mean that the size of the button will have more pixels in the higher density screen, but the physical size of the button will be the same as DisplayMetrics will always give me the absolute size in pixels. Or am I wrong about this?
See this question and its answers. You will get the answer to your question.
Edit:
From one of the answers on the mentioned question
If you are any serious about developing an Android app for more than one type of device, you should have read the screens support development document at least once. In addition to that it is always a good thing to know the actual number of active devices that have a particular screen configuration.
Screen Sizes and Densities
To help in your case it is proposed to use dp units instead of pixels, but still there will be differences from one device to another.
On a tablet screen with a high pixel density, the elements probably will occupy less relative space.
If you want to improve it more then you will have to do the dimensions calculation by your own.
Or use a layout that auto distributes the space, for example the LinearLayout
Also you have to take into account that it is the system that decides the size of some widgets, for example the standard buttons
I have created a layout that consists of imageviews and textviews. When I run the App every thing is fine on portrait screen orientation, but when I rotate the device to be in lanscape the imageviews shrinks (smaller in size).
I want to have (regardless of the screen orientation) the same size of views. I do not want the views to look smaller or bigger, I want the same sizes across different screen orientations.
Note: All dimesnions in the layout are in dp for width and height and the text font it is in sp.
Are your ImageView's being sized by attributes that align to edges, and then margin distances? This would distort your image as the parent boundaries change on device orientation.
Also, please provide your xml code, I don't have the reputation to ask as a comment.
Android tries to help you create responsive layouts (layouts that change the position and size of elements depending on how large the device screen is) through the use of things like layout_weights, settinging the width/height to match_parent, etc. Because of this, if you use these attributes and then rotate the phone screen, the size of your images is going to change because the system will think that you want to text/images to change size depending on orientation or device screen size.
Even if you mix layout weights and hard coded dp pixel sizes this will happen - what happens is that android measures all of your hard coded values and wrap content values, and then for any extra room on the screen, it expands the views that have weight, proportional to the weight number you give them. This is why you'll sometimes see people setting layout_height="0dp" and then setting a weight.
If you want an image that is always the same size, you can hard-code a certain number of dp pixels and remove any mention of layout_weight or match_parent. You can hard-code margins too. Depending on the size of the image, this will mean if you view the image on a phone that's too small, part off it will end up off-screen. There are a variety of ways to deal with this depending on what you want to happen. For example, if you want to elements on the screen to stay the same size but rearrange themselves depending on the device's screen size, you can make multiple layouts for different screen sizes and use resource folder qualifiers.
I am a webdeveloper, and I am very lost in all the Android screen settings for aligning content. I've seen weight, gravity, scaletypes of the image etc. etc.)
I hope I can make my intentions clear, and that it's possible to fullfill them.
In this picture of a phone screen, I have 2 square pictures (the purple and the green one).
What I want to accomplish is that the width of the pictures always take up 50% of the screen width, and that it is centered (so the margin left and margin right is 25%).
So let's say you have a 600*800 screen, the width should be 300 pixels and the margin left and right would be 150 pixels (or better 25%). On a 768*1024 screen the width should be 384 pixels.
The height should follow that size (I've tried a lot of code, and the best result was that the width was sort of a pertentage, with 3 relative layouts in a lineair layout, but then only the width was correct, but the height wasn't)
Depending on your screen size it could be that the height of the two images together would be higher than the screen and a scrollbar would be neccessary)
Wrap them in PercentRelativeLayout and fix their width 50%. set their height equal to their width in Java when the view gets populated
my problem is quite simple, i never did this before but now i'm forced to search for a solution.
working with layout-normal
Let's say i have a image background set to match_parent, with 3 different points on it, on that points i should place buttons, i added the buttons and set some margin dp's till i meet the points. for example: layout_marginTop="50dp"
on the preview screen it looks ok, but when i start some emulators from the same size category "layout-normal"
3.7inch; 4.0inch; 4.65inch ... it looks different. it looks ok only on the device size i got the preview
What should i do? what can i do?
i tryed to fix the image, created a linear layout, set the image as background, set the size of the screen (let's say 400x250) and added the buttons in the layout.
but now when i run it in different emulators i get empty space and the image does not fit the entire screen.
Hrrrrrrr! what can i do?
Get your ImageView Size with a onPreDrawListener, or if it fill the Screen you can get it with getDisplaySize
You know the points in your image where to place your Buttons:
orginal image 480x800 -> button1 left:XX px top:xx px
new size 720x1280 -> button1 left: YY px top: YY px
with this info you can calculate where to place the Button on real device with an other Resolution than 480x800 (using the rule of three)
then set your margins programaticly
actualy i found out that android documentation gives you other posibilities in layouts arranging.
-- layout-normal; layout-small; layout-large; layout-xlarge
-- layout-sw300dp; layout-sw360dp; layout-sw450dp; layout-sw600dp;
layout-sw800dp; where sw means smallest available width required by your layout resources
-- layout-w300dp; layout-w360dp; layout-w450dp; layout-w600dp; layout-w800dp; where w means the minimum width required for the layout
-- layout-h200dp; layout-h260dp; ... where h means the minimum height required for the layout
i used to give them the smallest available width.
Is the Eclipse graphical layout editor consistent with actual phone realities? I only own a droid x and have nothing to test say a "2.7 in QVGA: setting with.
I have a button I want to show up in a specific place on the phone. I use the buttons properties to set a specific layout height and layout width and specific layout margin left and top using dp units , however when I change phone sizes in the emulator the buttons are the wrong size and wrong place on a lot of them. The buttons work and are laid out correctly on the screen for the "4in WVGA (Nexus S)" and they work great on my droid X. But when I change the emulator to "2.7in QVGA" the button has moved way down the screen. Doesn't dp mean that the buttons will remain proportionate to the screen size?
Ps I have placed appropriate DPI'd images into their respective Res Drawable folders ie: drawable-hdpi and drawable-ldpi , etc.
I thought maybe i could use weight, but I have never used that before and maybe I am just missing something stupid. Any help would greatly be appreciated.
The size and position, relative to the topleft corner should be the same on each device. But, there are some devices which have a different resolution in height and width, which means that you will have more or less pixels in width/height on different devices, although they might share the same density. For example there are devices which have 800px in height, and some which have 854px, but they have both hdpi-density, so you will have the same margin to the topleft corner on both devices, but a different to the bottomright corner for example.