Android studio layout resource for screens with height bigger than width - android

In android studio I know that, for example if I will create a folder with name layout-sw600dp, the layout files inside that folder will be used for screens with minimum of 600dp width. I want to know is there a way to name a folder, which will be used for screens with height bigger than width? And if there is no way, how to create behavior like that(i.e. separate layouts for screens with height bigger than width and with width bigger than height)?

You can measure device's height and width with this code:
View view = getLayoutInflater().inflate(R.layout.your_activity, null);
//For example, linear layout
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.yourlinearlayout);
linearLayout.measure(0,0);
int width = linearLayout.getMeasuredWidth();
int height = linearLayout.getMeasuredHeight();
if(width > height) {
// your code here
}

You can use the folder name layout-port - indicating that the device is in portrait mode (taller than wide) or layout-land for landscape mode (wider than tall).
See here for more information on the available options.

There are some screen sizes with width longer than height (tablets for example).
you can check the screen aspect in xml using the "-long" "-notlong" suffix in your layout folder.
This is based purely on the aspect ratio of the screen (a "long" screen is wider). This isn't related to the screen orientation.
>long: Long screens, such as WQVGA, WVGA, FWVGA
>notlong: Not long screens, such as QVGA, HVGA, and VGA
https://developer.android.com/guide/topics/resources/providing-resources

Related

How to scale layout proportionally to screen's physical size?

Need the whole layout width to fit exactly the same within the portrait physical width from the screen in any device, so that it doesn't matter the DPI, the screen size, the pixel portrait width, nor the width-height relation.
All I have managed to do right now is to override the font size from the user's Android configuration, by adding the next code to MainActivity.cs:
public override Resources Resources
{
get
{
Configuration config = base.Resources.Configuration;
if (config == null)
{
config = new Configuration();
}
config.FontScale = 1f;
return CreateConfigurationContext(config).Resources;
}
}
That made a great improvement, but still see plenty of difference between different devices, those devices may have longer or shorter screens, greater or smaller DPI, and different sizes. For example device Pixel 2 from emulator, which has 1080x1920 resolution and 420 dpi, the layout fits perfectly (I made it that way), but when I try in physical device Redmi 9 1080x2340 resolution and 395 dpi (about 3 mm bigger portrait width), the layout becomes a little bigger, and elements don't fit in.
How to make the layout width to scale proportionally to the physical size, so that it can always fit within the width from portrait mode no matter the device?
You can try using percentage or ratio dimension attributes of ConstraintLayout.
ConstraintLayout : ConstraintLayout

Android-Studio: Layout for wide screen in a portrait mode

I have an application that should work in a portrait mode only.
it works great using just (Layout) resources.
Now I face a problem with some device,
The device has only Portrait mode, no orientation, it is ok for me.
The screen height is smaller than its width, and remember this is not landscape mode.
__________
| portrait |
|__________|
If I used Layout-land, it will not work, and the device pick the normal layout resource.
I tried layout-sw, layout-w, layout-h, without solution.
I want the normal devices to use "layout" and the devices which has height smaller than the width use another layout.
And remember please, this is not a landscape.
Thanks in advance.
===========================================================
Update
Is there any way to use Layout-11x00, where 11 is the width, and the 00 is the height? I tried it and it worked, but the problem is it worked for all devices, not the wide devices only. I tried Layout-774x480 as w=774 and h=480. but it worked also with my Samsung mobile, I do not want this.
Why when I set the layout to "Layout-774x480" it targeted my samsung? while my samsung width is not > height?! I mean what is the different between "Layout-774x480" and "Layout-480x774"?
You can set a qualifier along with the layout folder name to get your layout picked from that specific folder when you are running your application in a device. Here is the documentation where I recommend you to take a look.
In your case you just have to create a layout directory named res/layout-w600dp/main_activity.xml which indicates if the device screen is 600dp wide and bigger, the layout will be picked from this folder.
I checked that you have tried already with layout-w, layout-h and layout-sw settings. However, I think you are missing the expected width of the device screen which should be picked.
Hope that helps!
Update
If you are willing to set different layout based on the decision that the height is less than the width, then you might just consider checking the screen height and width before setting the content view of the activity.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
if(height > width) setContentView(R.layout.main_activity.xml);
else setContentView(R.layout.main_activity_wide.xml);

320dp for a android layout

In Android is it ok do set the width of a linear layout to 320dp? Can it be to big for some small screens?
On the internet people say that 320dp is the smallest android screen, but I just want to ask if someone tested this. Thanks
This question is related to android dp size for LinearLayout but nobody answered the question so I wanted to try to ask the question in a different way
You can use multi layout files as docs for small and large screens.
We have 2 positions (landscape and portrait) and 2 sizes (normal and large) of screen. So, there are 4 types of layout.
res/
layout/ # default (portrait)
main.xml
layout-land/ # landscape
main.xml
layout-large/ # large (portrait)
main.xml
layout-large-land/ # large landscape
main.xml
You have to create layout file with same name in them.
Instead of using fixed size you should try match_parent vs wrap_content.
On the internet people say that 320dp is the smallest android screen
Smallest device that I know is 240dp x 320dp which means 240dp is the smallest device size.
See following links for clarity
What is the difference between match_parent and fill_parent?
Supporting Different Screen Sizes
http://www.randomlytyping.com/blog/2014/2/9/matchparent-vs-fillparent
ViewGroup.LayoutParams
Update
Smallest device can have resolution of 180px x 240px = 240dp x 320dp

Android Resource Qualifiers -sw#dp vs -w#dp

Say I'm developing a different layout for devices with screen size equal to or greater than 600dp.
I want to use the post android 3.2 resource qualifiers. I created a folder named layout-sw600dp and put my layout there, but at the same time I could have created a folder named layout-w600dp and put the layout xml file there.
I'm trying to figure out what is the difference between -sw600dp and -w600dp? After all they are both meant to use the layout for device of width >= 600dp.
sw is "smallest width". It doesn't change if the device is rotated.
w, on the other hand, is available (i.e. current) width.
See Providing Alternative Resources:
smallestWidth - sw<N>dp - The smallestWidth is a fixed screen size characteristic of the device;
the device's smallestWidth does not change when the screen's
orientation changes.
Available width - w<N>dp - This configuration value will change when the orientation changes between landscape and portrait to match
the current actual width.
Example. Say that you have a device that is 600dp x 400dp.
If you have a w600dp resource, it will be used in landscape, but not in portrait.
If you have a sw600dp resource, it will not be used for any orientation (smallest is 400).

Using android's grid layout with different DPI on the same screen size and resolution

Ok, i got two tablets, both got 7" screen and 1024 x 600 resolution, one is using 160dpi and the other 240dpi (probably changed on build.prop). I'm trying use a grid layout but my problem is, the layout showing bigger than my screen on the 240dpi tablet (1,5 times bigger).
What can i do in order to make the both layouts works in the same way? or at least the 240dpi renders entirely inside my screen?![enter image description here][1]
I'm making two lines of buttons on the center of the screen using this code
gl = (GridLayout) findViewById(R.id.GridLayout1);
for(int i = 0;i<11;i++)
{
for(int j =0;j<15;j++)
{
Button b = new Button(this);
b.setWidth(getWindowManager().getDefaultDisplay().getWidth()/15);
b.setHeight(getWindowManager().getDefaultDisplay().getHeight()/11);
if(j== 7 || i==5)
{
b.setBackgroundColor(Color.LTGRAY);
}
else
{
b.setBackgroundColor(Color.WHITE);
}
gl.addView(b);
}
}
But it is showing like this:
http://i.stack.imgur.com/rmVL0.jpg
My layout:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="15"
android:orientation="horizontal"
android:rowCount="11" >
The software were made to work in the white one (see picture) by people who worked in my company before me but now I have to make it work on both.
Use relative values for the screen layout, not pixels or dp.
The root cause is that one of your tablets is reporting an incorrect pixel density. The native density of both displays is close to 160 dpi. The 240dpi tablet will appear to Android as if it had a 5" display, and Android will scale the fonts to be the same size.
If that causes screen real estate issues on the 240dpi tablet, do the following:
Create a copy of your layout in one of the following folders:
layout-w1024dp if width is an issue
layout-h600dp if height is the main limiting factor
In the main layout, decrease the font size to work on the 240dpi tablet.
This will reduce the font size to get more content on the screen if the screen size is below a certain minimum. However, fonts may appear tiny on a really small screen (devices with less than 7" which report correct pixel density).
As for relative values: I have done this extensively with linear layouts but not with grid layouts so far, but maybe this helps you get started.
Suppose you have a horizontal LinearLayout (child items arranged in columns) and you want them to maintain the same width relative to each other, taking up the whole screen.
For each child layout, set its layout_width to 0dp and give it a layout_weight.
The layout_weight is fairly arbitrary – it is just the ratio that matters, as the ratio of the layout_weight parameters of the child layouts equals the ratio of their widths. Hence having three child layouts with a layout_weight of 2 vs. 1 vs. 2 will have the same effect as 200 vs. 100 vs. 200.
For the LinearLayout holding all of these, specify a weightSum that is the sum of its children's layout_weight. Set the parent's layout_width to fill_parent.

Categories

Resources