Layout Alignment Issues For Device to Device - android

When I try to run my project on LG Android Mobile then there is no alignment issues come with this device it is a 3.2 HVGA but when I try to run it on Motorola it is a 3.7 WVGA then it gives complete layout alignment issues so can you tell me suggestion to implement layouts uniquely to every device.
I don't know is it possible or not to make a unique layout design for all devices.

You can't create custom layouts for different devices, but you can for different screen densities and sizes, Supporting Multiple Screens has all of the information you should need.

You can create custom layouts by reading the device layout screen like this
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
There are two ways you can create your layout. You can create your own algorithm that checks the difference between the resolution you have made your layout for, and gets the difference for the current screen size, and adjust all those values progmatically (long and tedious, but DEFINETELY will work on all devices)
Or, you can define a layout for each of the common devices in your layout folder. This requires alot more space and time though.

Sometimes back I was having this type of problem. Because I was using Pixels as my unit to specify height, width or any layout specific dimensions. But I changed those px to dp and it worked for me. I got same layout for all the screens. Hope if this may help you anyways...

Here is a quick checklist about how you can ensure that your application displays properly on different screens:
1.Use wrap_content, fill_parent, or dp units when specifying dimensions in an XML layout file
2.Do not use hard coded pixel values in your application code
3.Do not use AbsoluteLayout (it's deprecated)
4.Supply alternative bitmap drawables for different screen densities

Related

How to add screen layouts for different screen sizes in android? (Update 2020)

I tested my application for screen sizes for 6 inches - 6.5 inches(on a one plus 6t/7t) and above and it's scaling the UI elements properly for it. But when I tested the application for screen sizes of 5 - 5.5inches, it's going out of the constraints and off the screen.
These are the things that I have tried:
Having wrap_content and match_parent with most of the UI elements(Textviews, Buttons to name a few)
Using constraint_layout which is the default layout in Android Studio 3.0 with all the constraints set for every element
I also tried understanding this document but I could not clearly understand this
Lastly, I came across their solution of having a different screen layout for different screen size. For this, what would I have to do? Create a new Layout and copy my Design XML text in the newly created layout and adjust scaling accordingly or is there a better approach for doing so?
Also, any other suggestions or any other approach to solve this would also help.
Edit 1:
So I figured something out, The UI is not setting correctly for xxhdpi resolutions. To compare, two screen sizes of similar sizes (1080x1920) having 420dpi and xxhdpi resolutions, the former works correctly but the latter does not. Any clues on how can I create or deal with xxhdpi resolutions?
[try with this,you can update AS]
[1]: https://i.stack.imgur.com/oLqHI.png

Android layouts size and resize

I am doing a lot of work about android layout I still can't create a layout working in every phone. I am not sure about best the way to create a layout so correct me if I am wrong . There are three things to keep in mind :
Screen px (resolution, for example 1080x1920 px)
Screen dimension "inches"
Screen density dp or also called dpi ( dp is a virtual resolution, it's correct?)
To draw a layout working in every phone (my app will works for phones) do I have to create a directory "layout-kindofdensitydpi for every screen density (layout-ldpi,layout-mdpi,layout-hdpi,layout-xhdpi,layout-xxhdpi,layout-xxxhdpi) and draw "manually" or do I have to do something else?
I did a test, I created these 6 directory and drew manually for every resolution. It requires a lot of time, also device with a resolution of 768x1280 my app doesn't respect what I drew, for example spaces aren't respected, the collocation of elements doesn't result correct and frame layout with ImageView inside isn't scaled.
What I have to do? In some Android books isn't mentioned that elements could not resize and usually them explain how to put some text or image without analyse resize in every device.
Thanks in advice
First you should find the appropriate layout type for your UI (RelativeLayout or LinearLayout). Sometimes using a good layout(or nested layouts) can make the UI look good on every screen. I prefer LinearLayout cuz I can simply set layout_weight for components.
Then set different sizes in dimens.xml file for different densities or screen size buckets. Like this:
And you can also use match_parent or wrap_content
Don't forget to set the screen orientation of your activity if it doesn't need to rotate. Having one orientation makes it much easier to design.
If you couldn't make a good layout using tips above you should create multiple layouts to fit every screen size or density (Screen size and density are two different things).
You should find the best way to determine your screens according to.
Screen size bucket (small, normal, large, and xlarge) picks a layout that fits the screen (or the closest), density bucket (ldpi, mdpi, hdpi, xhdpi, xxhdpi, and xxxhdpi) picks a layout according to the density ,or the smallest width (I think it's almost the same thing as density).
480dp is the sw of these two devices
You can make layouts for different screen orientations too.
I don't like creating layouts for different screens for some reason. One of them and the most important is that sometimes same screen sizes have different densities and it makes it hard and time consuming. To create a layout and you should also provide a lot of pictures.
Use
android:layout_width="match_parent"
android:layout_height="match_parent"
to the outer layout to get access of the full screen of the device.
Also, if there are no changes in your UI then you don't need to create different layout folders.
Refer http://developer.android.com/training/multiscreen/screensizes.html
You should have a look at
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/index.html
I would suggest using layout-swXXXdp or layout-large etc instead of layout-KindOfDensitydpi
Create your relative layouts using RelativeLayout and use the weights of LinearLayout. Don't hard code any of the positions in the layout

Android - dp units on layouts -look like bad idea?

Android developers site,and most of you,Push everybody to use dp units on their layouts,
I can understand this approach if you using different layout for different densities,but when my requires is one layout.xml ,and one drawable folder,it is make no sense to me to use it,
because it is NOT preserve proportion.
It seems weird to me that there is no simple way to preserve proportion for all devices.
Just help you to understand my point.
1.make any layout with view inside it
2.set the sizes on dp as Android advise you to do
3.and then check it on the eclipse graphical view of your XML.
It looks way way different between different size devices.
Button with width of 15 dp look like 10 times bigger in 640*480 devices then Tablets.
So what's the point?
I understand all the math of conversion between dp and pixel with density,
but i looking for simple way to preserver the proportion on my layout,without define others for others densities.
The only way that i find to help is using android:weightsum,
But it can not use for margins and other settings.
Any idea?
You can always use your values folder and implements the exact dp size for each of different device densities, so you wont create different layout on different density.
Tablet and handheld devices will never have the same sizes that is where the different layout folders are created to let the developers design on different devices.
I would recommend a different layout design on tablet and handheld devices which most apps applies.
Think of dp units as referring to actual size (like cm and inches). So something that is 250dp will look (for example) about 1 inch on a phone, but also roughly 1 inch on a tablet. No matter the size and resolution of the device, it will always be about 1 inch.
Why is it done this way? Well basically it is the Android creative vision that you won't simply scale your app from a handheld screen to a tablet sized screen. This is why they don't allow you to specify heights and widths as % of screen size. The one small exception is layout_weight in LinearLayout.
You may agree or disagree with their vision, but that's the reality.

Different sizes on 2 devices, even if I use "dp"

I've 2 tablets:
1 Samsung Android 3.0
DisplayMetrics {density=1.0, width=600, height=976,
scaledDensity=1.0, xdpi=161.55031, ydpi=155.51021}
1 Low cost device, Android 2.3.3
DisplayMetrics {density=1.0, width=480, height=800,
scaledDensity=1.0, xdpi=160, ydpi=160.42105}
If I use different layout for each screen size, both devices says they are large-long and mdpi, so I can't distinguish them by using layout folder names... The problem is:
I use a TextView with textSize="20dp"
In the firse device, text width is half of the screen, in the second device is bigger (80% of the screen width). Why? I expect that both devices display text in the same way if I use dp (and not px). I tried also with sp but nothing changes...
(I used TextView as example, I've the same problem with all elements in the layout: button sizes, ...)
I would try to use layout folder names like "layout-w600dp" or something else introduced in Android 3.2 but this is not the case.
I know I can change element dimensions by code in onLayout() but I don't want to do that...
Any suggestion?
Update
I solved my problems with layouts using themes: Activate a specific dimens.xml at runtime
The android documentation on Dimention say this
The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion
So it's totally ok what you see
You can achieve 50% of the screen using LinearLayout weigth..Refer this

Screen size supporting from xml?

Supporting multiple screen is most important thing is android i think.We are doing some view different size.I know it is basic but i am curious about that.
For example i do a Edittext and i give width 200 dip for 480X800 screen.When i run this project 240X320 screen it will seen too big.We can fix this problem image with the drawable folder but how can we fix it view this problem?I know we can give wrap_content but when we give dip it is bad? When we give dip what is best effort from scale?
THX.
Just like with drawables, you can create different folders with layouts for different screen sizes.
As long as you keep the names of the files consistent across the folders, Android will manage picking the right one automatically.
Just create folders such as:
res/layout-small
res/layout-medium
res/layout-large
res/layout-small-land (incase you want landscape mode as well)
However, this does mean you will need to manually make adjustments for all screen sizes. Like in your example of the EditText, you will need to manually set the size to 100dp for example in the small screen layout.
The best way is to use wrap_content, fill_parent.
You can see layout_weight parameter of linearlayout where you can give value in %. But remember to give ur height or width as 0dp when you are using layout_weight.

Categories

Resources