Mark M. | has entered the room |
Mark M. | turned on guest access |
May 19 | 8:35 AM |
Kai H. | has entered the room |
Kai H. |
Hello :)
|
Mark M. |
hello, Kai!
|
Mark M. |
how can I help you today?
|
May 19 | 8:40 AM |
Kai H. |
While I was doing a custom button, I defined its size with "dpi". And wondered if that is the way to go.
|
Mark M. |
do you mean dp?
|
Mark M. |
(or possibly the older dip equivalent)?
|
Kai H. |
I meant to write "dip" :D
|
Mark M. |
dip and dp are the same; we tend to use dp nowadays
|
Kai H. |
Should all elements that are not constrained by the very edges around them have a "definitive" size?
|
Kai H. |
I use "dip" alot because I like it more. "device independent pixels" = "dip"
|
Mark M. |
I think we settled on dp because all the other ones are two letters as well
|
Mark M. |
but dip was used a lot in the early days, and from your experiences, presumably it still works :-)
|
Mark M. |
in terms of whether your button should be sized in dp, that is partially a question for your designer and partially a question of whether the button has a text caption or not
|
Mark M. |
if it has a text caption, I'd lean towards sp, to take the user's chosen font scale into account
|
Mark M. |
and the designer may have had other sizing rules in mind (e.g., 40% of the screen width)
|
Kai H. |
It's more the question if all elements need some sort of non-relative size
|
Mark M. |
relative sizes are fine, so long as they look the way you want on all screen sizes
|
May 19 | 8:45 AM |
Mark M. |
and, if you include wrap_content in "relative sizes", that's fine so long as it looks the way you want with widely varying content
|
May 19 | 8:45 AM |
Mark M. |
you can use things like android:minWidth, android:minHeight, android:maxWidth, and android:maxHeight to help manage wrap_content behavior in a ConstraintLayout
|
Mark M. |
and you can use text autosizing to allow text to shrink if needed
|
May 19 | 8:50 AM |
Kai H. |
Do you implement "mostly relative" layouts often=?
|
Kai H. |
Or is it the exception?
|
Mark M. |
there are two types of projects that I work on: ones where there are graphic designers, and my own projects
|
Mark M. |
for my own projects, I try to use what I think you mean by "relative sizing", so the UI can hold up under varying screen sizes
|
Mark M. |
with graphic designers, I wind up in the dual problem of "they call the shots" and "they really can't be bothered with multiple screen sizes", so I tend to have to "wing it" a bit more to come up with something that hits their designs and still holds up on varying screen sizes
|
May 19 | 8:55 AM |
Mark M. |
the designs that the designers come up with usually are very nice, but the details that I get do not take varying screen sizes into account, so it is difficult to tell what a certain dimension really means if the screen is smaller or larger than the canvas the designer used
|
Kai H. |
Let's say you have a screen with four "fields", to choose one of four options to go forward. How would you implement the layout of such fields?
|
Mark M. |
ummm... a column? I guess I don't really understand the scope of your question
|
Kai H. |
It's hard to come up with examples of what I mean :D
|
Kai H. |
I don't really know how to phrase my question.
|
Mark M. |
perhaps a better approach is to point me to a screenshot of some mobile screen and ask how I would implement it
|
Mark M. |
after all, a picture is worth (checks calculator) a thousand words
|
Mark M. |
(approximately)
|
Kai H. | |
Kai H. |
You have a "calculate picture to words" button on your calculator? That's awfully nice
|
May 19 | 9:00 AM |
Mark M. |
programmable calculators FTW!
|
May 19 | 9:00 AM |
Kai H. |
I have implemented "Buttons" similar to the ones in above links
|
Mark M. |
yeah, I think you gave me that screenshot before, as it looks familiar
|
Kai H. |
My version sports a "highlight" bar and changes the background when selected, but it's the same idea.
|
Mark M. |
is this a fixed grid? or are there N buttons that wind up in a grid RecyclerView?
|
Kai H. |
In my case it's a fixed 2x2 grid in portrait and 1x4 in landscape.
|
Mark M. |
OK, let's talk portrait first
|
Mark M. |
the buttons are square, and you want them to fill the width of their container (e.g., the screen)?
|
May 19 | 9:05 AM |
Kai H. |
Let's assume yes
|
May 19 | 9:05 AM |
Mark M. |
OK, that container then should be a ConstraintLayout
|
Mark M. |
you have two main choices for setting the horizontal sizing:
|
Mark M. |
1. use percentages for the widths of the buttons and the widths of the margins
|
Mark M. |
2. use conventional fixed-sized margins and use a chain to allocate the remaining width evenly between the two buttons
|
Mark M. |
(BTW, by "buttons", I mean the rounded rectangle -- we'll talk implementation of the buttons themselves in a bit)
|
Mark M. |
I'd try option #2 first, as margins-as-percentages could get messy
|
Mark M. |
a "weighted chain" (with equal weights) and margins should give you your even sizing
|
Mark M. |
then, use layout_constraintDimensionRatio to have the buttons be equal height to their allocated widths
|
Mark M. |
(basically, a 1:1 ratio)
|
Mark M. |
that should get you your 2x2 grid -- for the 1x4 grid, you'd do the same basic thing with right aspect ratio
|
May 19 | 9:10 AM |
Mark M. |
with me so far?
|
May 19 | 9:10 AM |
Kai H. |
I think so. Would I not scale the text in any way?
|
Mark M. |
we haven't gotten to the interiors of the buttons yet
|
Mark M. |
right now, pretend the buttons are empty rounded rectangles
|
Kai H. |
Ok. Let's go :D
|
Kai H. |
Let's drop the "rounded" btw
|
Mark M. |
I'm going based on the screenshot, as that's all I have
|
Kai H. |
And make them normal equally-sized rectangles (== squares)
|
Mark M. |
translating these instructions to your real use case is left as an exercise for the reader :-)
|
Kai H. |
*shuts eyes*
|
Kai H. |
;-)
|
Kai H. |
Ok, we got the button shape and positions.
|
Mark M. |
so now, let's talk about the button interiors
|
Mark M. |
this is a case where the screenshot alone isn't really sufficient -- I'd talk to the designer if I had one
|
Mark M. |
if this design came to me in a dream, and I didn't have a designer...
|
Mark M. |
since we need a background (in the screenshot, the rounded rectangle), we need a container for the icon and caption
|
Mark M. |
I'd use a nested ConstraintLayout here, so the overall UI is a ConstraintLayout holding onto four nested ConstraintLayouts, the latter being your buttons
|
May 19 | 9:15 AM |
Mark M. |
the sticky wicket is in trying to interpret how much of the space goes to the icon vs. the caption
|
Mark M. |
I'd probably take a shot at setting the height of the icon to a % of its container (the button's ConstraintLayout), and using the aspect ratio to control its width
|
Kai H. |
You'd set the rounded rectangle via the background of the underlying container?
|
Mark M. |
yes
|
Kai H. |
huh
|
Mark M. |
so the ConstraintLayout for the button would have an android:background attribute
|
Mark M. |
in your case, with the "selected" concept, that's probably a StateListDrawable of some form
|
Mark M. |
I'd set the height of the text to fill the remaining space after the icon's height plus margins
|
Mark M. |
i.e. android:layout_height="0dp" in ConstraintLayout terms
|
Mark M. |
and I'd set up the autosize attributes to ensure that my text didn't run out of room
|
Mark M. |
that would be my first stab at this -- sometimes, my guesses as to the right types of constraints do not work, so I'd have to experiment
|
Mark M. |
but, in theory, that would get you what you want
|
Mark M. |
(again, relative to that screenshot)
|
May 19 | 9:20 AM |
Kai H. |
Interesting
|
Kai H. |
What are the autosize attributes?
|
Mark M. | |
Mark M. |
(and where that page says "Support Library", think "Jetpack")
|
Kai H. |
Won't that make you end up with different text sizes in similar elements?
|
Mark M. |
if your minSdkVersion is below 26, you would use the app: prefix instead of android: so the Jetpack handles those
|
Mark M. |
yes
|
Mark M. |
at least, if things get long
|
Mark M. |
typically, I aim for everything to be the same font size and fit at the designer's dimensions
|
Mark M. |
however, on smaller screens, or for German translations, the text needs to shrink
|
May 19 | 9:25 AM |
Mark M. |
that shrinking might not be even, because the translations might not be even
|
Mark M. |
if that would be a big problem, and you need to somehow synchronize the text sizes across four TextViews based on which one needs the smallest text...
|
Mark M. |
...well, let's just say that https://xkcd.com/1425/ comes to mind
|
Kai H. |
X)
|
Mark M. |
but an answer of "we're going to keep the same text size, and if the text gets cut off, that's too bad" is a non-starter to me
|
Mark M. |
so, I'd rather have varying font sizes and ensure everything is readable, despite the screen size and translation lengths
|
May 19 | 9:30 AM |
Mark M. |
OK, that's all the time I have for today's chat
|
Mark M. |
next one is Thursday at 7:30pm US Eastern
|
Mark M. |
have a pleasant day, and stay healthy!
|
Kai H. |
Have a good time
|
Kai H. | has left the room |
Mark M. | turned off guest access |