0

I hit a brick wall trying to configure my app for multiple screens. I read a lot of information about it but so far I didn't manage to successfully do it. to begin with, I have to use hard-coded sizes for my buttons because there are many of them, 53 to be exact, and they have to be square, 40x40dp, and placed precisely on the layout. so I can't use wrap_content, fill_parent, etc' because then I can't fit all the buttons on the layout.

I've created 5 different layout 'activity_main.xml' with these screen configuration: normal_mdpi, large-hdpi, xlarge-xhdpi, xxhdpi, xxxhdpi. so far so good but if I don't apply (With the "device for preview" dropdown menu) a specific phone configuration to any of the layouts, then they won't fit on the screen. But then the layouts fit only for these phone configurations, they won't fit for one with a similar configuration but slightly different. what am I doing wrong? how many layouts do I need? do I need to create a layout for each phone out there?

  • Does this answer your question? [Different resolution support android](https://stackoverflow.com/questions/16706076/different-resolution-support-android) – Bö macht Blau Dec 10 '20 at 18:23
  • And maybe also take a look at [How to support different screen size in android](https://stackoverflow.com/questions/8255985/how-to-support-different-screen-size-in-android?noredirect=1&lq=1) or [How to support different screen size in android using dimensions?](https://stackoverflow.com/questions/32133266/how-to-support-different-screen-size-in-android-using-dimensions/32133834#32133834) – Bö macht Blau Dec 10 '20 at 18:26
  • @BömachtBlau do I need to create a different layout for every screen out there if I want it to fit? – Steve Ben-Ari Dec 10 '20 at 22:16
  • Not really :) One tries to do the job with a mixture of layouts and dimension resources (e.g. for button size or margins...) for different types of orientations and screen sizes. One more thing: if you have lots of buttons - like in a MineSweeper game - you can add them to a parent ViewGroup programmatically. That way, you only need to provide layouts containing the parent ViewGroup for the various configurations you want to support. – Bö macht Blau Dec 11 '20 at 13:58

1 Answers1

0

You cannot use fixed sizes. I know you want to use fixed sizes, but you can't. Creating multiple layouts for each density is a common mistake. There is usually only one xml layout resource file for each activity, but there are exceptions when making really advanced layouts (fragments will be used).

How to fix the layouts

As the base layout inside the layout resource file, i suggest you use ConstraintLayout as it has a ton of potential and can easily make complex layouts. It will take some time to edit each view to make them use wrap_content or match_constraint (match_constraint is basically match_parent but for ConstraintLayouts), but it is necessary. Android will then take care of resizing and fitting the views for any density/screen size if you use wrap_content and match_constraint (match_parent for other base layouts like LinearLayout).

And remember, never use fixed sizes for views.

radwl
  • 29
  • 2
  • OK great, following your advice I manage to create a layout with ConstraintLayout. I solved my specific problem by creating a grid of Guides positions on the layout with a percentage value. Constraining the Buttons to the grid kept them proportionally to the layout. before I used the grid with a fixed size and that didn't work. so this way the proportions were kept while the location and relative size were also kept. now I have a bit of a problem to keep the text size in TextView also proportionally fit. How to make the text size grow or shrink proportionally? – Steve Ben-Ari Dec 13 '20 at 21:48
  • @SteveBen-Ari Add the following xml attributes to your TextView: `app:autoSizeTextType="uniform"`, `app:autoSizeMinTextSize="10sp"`, `app:autoSizeMaxTextSize="100sp"`, `app:stepGranularity="20sp"`. Make sure to use `app:` not `android:`, otherwise it won't support old devices. The min and max textSize are to limit how big or small it can be. The `stepGranularity` is the step size. If it shows an error at each attribute, put your cursor on the `app:` and hit Alt+Enter to import what's needed. If it still shows an error (make sure you did the first step), from the error icon select Suppress: ... – radwl Dec 15 '20 at 06:58