Reputation: 33579
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.
Upvotes: 0
Views: 1081
Reputation: 203
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 :/
Upvotes: 1