3

In my application, the layout must have a certain look but this is hard to achieve in many different screen resolutions and densities. I've read the developers article and tried my best to support many different screens by creating the following layout files :

layout dirs

However, 4.7" phones(1920x1080) and 5.8" phones(like my s8 : 1080x2220) use the same layout-sw360dp directory. Due to their difference in screen height resolution, the ui elements don't show up correctly for both phones. I want to know how i should go about solving this problem, can i use another qualifier to make android studio select the appropriate layout directory based on the phone's height or something? I'm starting to get lost here. Any help is welcome

Stelios Papamichail
  • 955
  • 2
  • 19
  • 57
  • Please visit this SO [question](https://stackoverflow.com/questions/9476662/how-to-set-android-layout-to-support-all-screen-sizes#29144549), a lot of resources have been referenced there. – Clement Osei Tano Dec 19 '17 at 23:06
  • @CoolGuyCG i'm sorry but i've read such posts and they don't help me much. They are too generic. I need to distinguish which layout to choose based on resolution and not just dp etc. – Stelios Papamichail Dec 20 '17 at 00:04

1 Answers1

1

After digging even deeper, i found out that you can choose different layouts inside the onCreate() method based on info from DisplayMetrics like so:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;

if (...) {
    setContentView(R.layout.ex1);
} else {
   // .....
}

By using the height and width variables as the conditions inside the if statement , i managed to select the appropriate layout files for each screen size!

Stelios Papamichail
  • 955
  • 2
  • 19
  • 57