0

I would like to know how to make my app support different screen sizes and densities, including tablets, regarding the layout. I've found different answers on SO, but I can't find one that explains this issue the best.

I've already included this in my Manifest:

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:xlargeScreens="true" />

According to this answer, I should create multiple layout folders such as small, large, xlarge.

According to documentation I should create multiple layouts and use size qualifiers such as sw<N>dp.

Which would be the best solution if my app needs to support both phones and tablts?

Additional question: In connection with the abovementioned question, I have a recycler view with list of items. If the screen is portrait then there's a list, if the screen is landscape, then there's grid.

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mBinder.rvHistoryList.setLayoutManager(manager);
    } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        mBinder.rvHistoryList.setLayoutManager(manager);
    }

But, if the app is used on a tablet in portrait mode I would like the items to be in a grid ordered by 2 in a row, if the app is used on a tablet in landscape mode I would like the items to be in a grid ordered by 4 in a row. How can I achieve this?

Community
  • 1
  • 1
Esteban
  • 667
  • 9
  • 22
  • There is a difference between *should* and *can*. Do you really need any more than 1 main layout? If you want to support both tablets and phones, then maybe 3-4 layouts? 2x portrait and 2x landscape? You don't need to target screen density unless you absolutely have to. – OneCricketeer Dec 17 '16 at 20:53
  • Well, yes, I need to support both phones and tablets. How would I achieve this by your proposal of having 2x portrait and 2x landscape layouts? – Esteban Dec 17 '16 at 21:00
  • res/layout is portrait and res/layout-land is landscape. if you need more then a `swdp` folder – OneCricketeer Dec 17 '16 at 22:25

1 Answers1

0

Creating few layouts for different size is old and confusing method.
You can set weight for each layout and component.

This is good tutorial for beginning.

http://www.101apps.co.za/index.php/articles/using-android-s-layout-weight-attribute.html

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ehsan Mashhadi
  • 2,568
  • 1
  • 19
  • 30