0

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.

Community
  • 1
  • 1
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • You need `res/layout-land` folder for landscape – OneCricketeer Mar 28 '17 at 13:36
  • @cricket_007: that's exactly what I don't want to do. Did you read the question? – Violet Giraffe Mar 28 '17 at 13:36
  • Check this out http://stackoverflow.com/questions/13157971/have-a-fixed-orientation-based-on-screen-size – Ajith Pandian Mar 28 '17 at 13:37
  • Do you have something else in `android:configChanges`? – makvasic Mar 28 '17 at 13:38
  • @makvasic: no, this particular activity only has its name in the manifest, nothing else. Also, please see the update I edited into the bottom of my question. This problem may not be related to orientation change at all. – Violet Giraffe Mar 28 '17 at 13:39
  • You can add screen width into the landscape resources, was my point – OneCricketeer Mar 28 '17 at 13:41
  • @cricket_007: I understand. And my point was that there should be a way to avoid dividing layouts into landscape and portrait in the first place. All my layout cares about is width, it doesn't care which of your screen's sides is the longer side. – Violet Giraffe Mar 28 '17 at 13:44
  • That's really strange... try maybe `layout-w750dp-land` just to see what happens... – makvasic Mar 28 '17 at 13:56
  • @makvasic: good test. It did *not* work. Any idea why? – Violet Giraffe Mar 28 '17 at 14:10
  • I have no more ideas other then trying to get smallest width with this [method](http://stackoverflow.com/a/23900692/7177329) and see what that gives you but i doubt that is less then 750dp :/ – makvasic Mar 28 '17 at 14:10
  • @makvasic: that's what I used. It gave 600dp for portrait mode width, that's why I picked 750 as the threshold. – Violet Giraffe Mar 28 '17 at 14:11
  • I am intrigued... Will make same sample project to test this and see what happens! – makvasic Mar 28 '17 at 14:17
  • @makvasic: Thanks! I thought maybe it's because I was renaming the folders using the Android Studio itself, maybe something didn't register. So I restarted the Studio, and nope, still no dice. And yes, I did check for typos :) – Violet Giraffe Mar 28 '17 at 14:20

1 Answers1

1

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 :/

makvasic
  • 213
  • 1
  • 8