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);
Related
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
I have two versions of my layout, for smaller screens and for larger screens. Incidentally, I have a device where different layouts are required in different orientations. On other device that may not be so. I want to base it on screen width, not the orientation as such.
I've read this article and noticed that the "Available screen width" (w<N>dp modifier) can be used for specifying the proper layouts. It also says:
The system's corresponding value for the width changes when the screen's orientation switches between landscape and portrait to reflect the current actual width that's available for your UI.
Sounds perfect. So I put the smaller layout in the base layout folder, and the larger one into layout-w750dp. And the larger layout is picked. The problem is that it doesn't switch to the base layout when I rotate the device into portrait mode.
I have used the code from this answer to check the screen width in dp. It's 960 in landscape and 600 in portrait. Then I made sure android:configChanges="orientation" is not specified for this activity. I have also put Log into this activity's onCreate() - it is indeed called when I rotate the device, so it should have received the correct layout?.. Why doesn't it work and how to make it work?
Update: launching the activity (and even the whole application) in portrait mode right from the start still picks the w750dp layout.
Update 2: layout-land didn't work either. This layout is still picked in the portrait mode. Odd. It's becoming clear that the issue has little to do with width but with general functioning of the resource selectors.
Comments are getting long so to answer.
I just tested on tablet (768x1024 dp) two layouts first in layout, second in layout-w900dp and everything works just fine.
Second layout is shown in landscape mode which is correct because 900 < 1024.
Note: I used getResources().getConfiguration().screenWidthDp for screen width!
So it's definitely problem on your side :)
Ether you messed up your layouts or android studio messing with you :D.
Sorry for lack of more definitive answer :/
I have a view in my android app that I would like to toggle between visible/gone on smaller screens, and visible/invisible in larger sizes. The initial set up (gone for small, invisible for large screens) is done by having two separate XML layout files under layout and layout-sw600dp-land, but then when I need to dynamically swap the visibility setting, how can I determine from within Java code which one to pick based on screen size?
Edit: more specifically, I want to detect in my code the same condition that causes Android to use layouts from layout-sw600dp-land. I was thinking even recording the value somewhere in the values-sw600dp-land directory, but not sure which file to put it into and how to access it.
You can get the size in pixels of the screen using the following.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
However, your question was ambiguous as to whether size of screen meant pixels or inches. You may need to take advantage of the dm.densityDpi value, to convert the values from pixels to inches, for a more useful calculation of the "size" of the screen.
ANSER FOR EDITS:
There are two potential solutions. One is referenced in this thread, very simple and you alluded to it above.
How to programatically determine which XML layout my Android apps is using?
The second isn't a solution, but rather an explanation. The layout-sw600dp-land file replaces an old naming convention pre 3.2 that went like this layout-xlarge-land. This essentially manes "xlarge" screen in "landscape" mode. So you can detect this programmatically by finding xlarge screen sizes, in which the width > height. Below is a good reference to compare the old convention vs the new "sw600dp" = smallest width is 600 dp convention.
http://developer.android.com/training/multiscreen/screensizes.html
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
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