A
A
Alexander2021-11-29 17:43:35
Android
Alexander, 2021-11-29 17:43:35

How to make interface layout per dpi in Android Studio?

While making the application, I ran into the problem of interface layout on phones and tablets. I specify everything in dp, but the interface is still blurry. On another forum, they suggested making an interface for each dpi (mdpi, hdpi, etc.), but they didn’t say how. I hope this form will help me. I can attach screenshots if you need.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2021-11-29
@402d

Are you sure it's density problem?
dpi must be taken into account in resources if they are not a vector.
It seems to me an error in the type of layots you used. You from a corner of a coordinate for all elements specify? Read about existing types. You can nest them for the desired effect.
In reality, I used the -land modifier - I redefined it for horizontal orientation.
And between phones and tablets, the difference is more in the diagonal.
For example, on a phone, a matrix of 2x3 buttons looks good, but on a tablet it asks for 4x6.
Unfolded on the left and right edge. Already in landscape orientation is not so good on the phone. And on a tablet or TV it becomes terrible.
So, it makes sense to make different folders of layouts.
Layout folder conventions
layout-swdp, where sw stands for smallest width, and is where you will replace with the width of the device you are targeting. Smallest width nerver changes, it always refers to the width of the device whether if the device is on portrait or landscape.
layout-wdp, where w stands for available width, and is where you will replace with the avaible width of the device you are targeting. The available width changes as you rotate the device. On portrait, the width will be shorter, on landscape, the width will be longer.
layout-hdp, where h stands for available height, and is where you will replace with the avaible height of the device you are targeting. The available height changes as you rotate the device. On portrait, the height will be longer, on landscape, the height will be longer.
If you support sw360dp, sw600dp and sw720dp devices, then in the res/ folder, you will create folders layout-sw360dp, layout-sw600dp, and layout-sw720dp respectively. When you create a layout file for an Activity or Fragment, you will create the same file within each of these folders, same file name but with different layout contents. By doing this, when the app starts, Android will automatically pick the corresponding layout for the device it is running on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question