In my current project there are numerous instances where I want to scale a View as 1/8th of its parent container because I have to make use of all the space at my disposal, since I have so many features to implement on a small android device's screen.
Now, when I programmatically generate Views that must be scaled to 1/8th of their parent, there are many approaches I could try, but I am sure most of them are bad design. I was wondering how I should go about doing it.
As example let's take a table/grid of buttons. I want a scrollable button grid where each row has 0-8 buttons:
Mockup of example.
Please share your thoughts and solutions. I have not found any information on this topic online and GPT struggles with that question.
---------------------------------------------------------------------------------------------------------------------------------
Approach 1: Get the width of the parent container with findViewById
divided through 8. Write it into a layoutParams
set and pass it as the view's width before it gets inflated.
Approach 2: Let the view get inflated with a hardcoded set of default dimensions (for example 100dp x 100dp), then get the width of the parent container with findViewById
divided through 8. Rescale the already inflated view by overriding its width property with the newly calculated one.
Approach 3: In the design tab create 2 guidelines inside the parent container: 1 at 0%, 2 at 12.5% of its width. Add an empty View object and constrain it horizontally to the 1st and 2nd guidelines without defining any value for width. Then whenever you want to create the same view with different informations, you just duplicate the already existing view and adjust the values.