0

I am new to Android development and I am currently working on supporting different screen sizes and pixel densities for my app. Now, I have a button with the height @dimen/button_height. In dimens.xml I have the following resource:

<resources>
    <dimen name="button_height">55dp</dimen>
</resources>

Now, I tried the app on different screen sizes and noticed, that, for example, on 3.3" WQVGA, the button is too big. This device is 240x400 px ldpi, which is, 320x533 dp.

I read App resources overview and Support different Screen Sizes (I have to say that to me it is still not 100% clear how to support every devices configurations). According to that, the screen-size of this device is small, so I made another resource file dimens.xml (small) with

<resources>
    <dimen name="button_height">30dp</dimen>
</resources>

Now, when I try this on the emulator, the button is still 55dp high. So, what did I understand wrong? What would be a better approach to support smaller devices?

Apri
  • 1,241
  • 1
  • 8
  • 33

2 Answers2

1

Basically, Firstly understand the difference between px, dp,sp etc.

Prefer below link

https://stackoverflow.com/a/2025541/6997819

1) To support multiple screen sizes

You need to create

values-hdpi
values-xhdpi
values-xxhdpi
values-xxhdpi

NOTE: I am suggesting four basic apart from above there are few more.

every values directory will have their own dimen.xml file

Suppose,

dimen.xml file inside values-hdpi have

<dimen name="button_height">55dp</dimen>

then in dimen.xml file inside values-xhdpi you should ideally increase the size

<dimen name="button_height">60dp</dimen>

Now, when you will execute the code the device will automatically pick based on their screen density.

2) Refer below link

https://github.com/intuit/sdp

This will ommit your above struggle. The mechanism is same, but provided the gradle that will be easy to import and use as well.

Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
  • I thought if I use dp, then mdpi, hdpi, ... don't make a difference? – Apri Nov 13 '19 at 11:10
  • dp is basically density pixel which differs screen by screen. – Vir Rajpurohit Nov 13 '19 at 11:10
  • dp is density-independent-pixel. `To preserve the visible size of your UI on screens with different densities, you must design your UI using density-independent pixels (dp) as your unit of measurement. One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density). Android translates this value to the appropriate number of real pixels for each other density.` -> From Android Developers – Apri Nov 13 '19 at 11:12
  • The link I have suggested on start, please go through it and also implement solution I have provided. Let me know does that helps you? – Vir Rajpurohit Nov 13 '19 at 11:13
  • A device with a screen width of 320 dp can be hdpi as well as xhdpi. So using that as a qualifier qouldn't make a difference. The library you sugested would do the job, but what if I have to, for example, organize the structure of the layout in a completly different way on smaller screen sizes? – Apri Nov 13 '19 at 11:17
  • Please give it a try. It will make your understanding pretty clear. As this discussion will be much longer to carry on here. – Vir Rajpurohit Nov 13 '19 at 11:18
0

you can use ssp library to scale font size and sdp library to scale dimention size.

Amit Goswami
  • 314
  • 2
  • 11