https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
s

Stylianos Gakis

12/22/2021, 6:54 AM
I have a code-style related question. How do you prefer to write your layouts A:
Column {
    Item()
    Spacer(Modifier.height(16.dp))
    OtherItem()
}
B:
Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {
    Item()
    OtherItem()
}
I’d love to hear arguments/opinions too if someone feels like they have strong opinions about either way.
🅰️ 3
🅱️ 13
For people answering B. What do you do when the design comes with one of the spacings being
20.dp
instead of
16.dp
a list with let’s say >5 items? Do you remove the
verticalArrangement
and add a spacer in-between all items manually?
f

Filip Wiesner

12/22/2021, 8:54 AM
If it's one of many spacings that is bigger than others, than I would hack it some other way like adding padding to one of the items next to it. The problem is when the spacing is smaller than the common one or there is more than one different spacing. Than I would remove arrangement add
Spacers
like you mentioned. Tldr: 🅱️ but depends on the context 🙂
Thanks for this interesting discussion btw 👌
🙏 1
1
s

Stylianos Gakis

12/22/2021, 9:02 AM
These are two points that really have been bothering me too. • Space is smaller problems: Literally impossible, have to fallback to spacings • Space is bigger problems: If we add a padding to a specific item as you said, it 1) Feels like a hack, since that item itself isn’t what needs a padding, so you add something to it that’s unrelated to it (adding it to its modifier alleviates this problem at least a bit) 2) It becomes an actual problem when things start being rearranged in the list (very common) and suddenly that item takes its padding with it to weird places in the list. With all these in mind, I really feel like even though I like 🅱️ more, and it seems like most people like it too, it starts becoming a problem in every slight change the Column/Row might see.
j

Joseph Hawkes-Cates

12/22/2021, 6:21 PM
Another option when the space is smaller between two items in the column is to encapsulate those two items in another container (column, box, etc) and then set their spacing accordingly. This helps you avoid adding spacers between everything else.
c

Chris Sinco [G]

12/22/2021, 7:14 PM
Another option: push back on the designer about why their spacing choices are inconsistent 🤷‍♂️🏻
😂 1
🙌 1
c

Colton Idle

12/22/2021, 7:42 PM
Have you ever tried speaking to a designer? :troll: 😂
😢 1
c

Chris Sinco [G]

12/22/2021, 9:58 PM
Revisiting this thread, and jokes partially aside, as a designer, I would question my design choices about being super precise with spacing (20 vs 16) between items based on # of items, assuming the parent Column width is the screen width (albeit padding). Not sure if this is design is based on iOS but I’d push back and say hey Android screens have different widths, so it’s more scalable to use
weight
or
Arrangement.SpaceBetween
in this scenario. This would also ensure the children in the Column would all be the same size (assuming this is what you want), and have even spacing between them.
I guess to others’ points, it all depends on context and the design you are trying to achieve. But again, as a fellow designer, I’d welcome the pushback because we shouldn’t be designing everything pixel perfect to a single device.
:yes: 3
☝️ 2
☝️🏽 1
s

Stylianos Gakis

12/23/2021, 8:41 AM
I think you got me wrong Chris. The spacing of the items isn’t supposed to change depending on the number of items on it, and the children’s height or width doesn’t change when this spacing changes. Actually I was referring to columns/rows that do not contain much of the same item, but are for example screens that contain a bunch of completely different items in them with potentially varying spacings between them. This has nothing to do with pixel perfectness, but more about giving things various spacings to give less/more emphasis maybe? Is that really something that I should push back on? I really don’t think so, this feels like a very natural use case.
I think I can use this image as an example. Think of this screen having a
Column {}
wrapping everything, you have the
CircleView
then
MoveAndHeartItems
then
StepCalcStats
then
HeartGraph
composables. These in-between them have varying Spacings as it is right now as far as my eye can see (I don’t have a great “designer’s eye”). Thing is this screen may have started with less features, and a common
verticalArrangement
spacing may have sufficed, but then as soon as more of those composables were introduced, you’d have to drop that in favor of individual Spacings. Am I wrong to think of it this way? Please do help me understand your point if so, I’d really appreciate it.
c

Chris Sinco [G]

12/23/2021, 9:20 PM
Gotcha, indeed I misunderstood. Though I think the general answer of "it depends on context" still applies. In the screenshot you provided, yes I think you may end up having variability in the spacing across the entire screen just due to the size of elements (font size, height/width) which I believe is being used more to communicate hierarchy and emphasis than space. Space here is used more to communicate grouping of items together / container relationships.
Related, I tend to use the
spacedBy
modifier in containers where I expect some uniformity amongst the children elements. It's handy in the screenshot above for the overall Column of the screen, but I'd use it out of convenience, especially in the beginning as you mentioned. Over time, I may remove it and rely on padding within each child element because as the screen gets more complex and the children get more complex, they could have their own set of spacing built-in since they may be reused across your app. In that case,
spacedBy
doesn't quite scale.
🙌 2
I myself use
Spacer
less often since I tend to have each component handle their own spacing, and/or rely on arrangement when brought together in a Column/Row. It might be because I have a designer brain and am used to tools that don't make adding arbitrary Spacers convenient so most choices are padding or arrangement. But Spacer is very handy when hacking quickly / debugging layout / finessing before cleaning up. Oh it's also very useful when you need to make padding/spacing percentage or ratio-based since we aren't able to do that with the Padding modifier just yet.
Am I wrong to think of it this way?
I personally don't think there's a wrong or right way to think about how to implement spacing. I think what is great about Compose is there is just enough variability in how to implement spacing that you can choose what works for you but not be overwhelmed with too much choice, e.g. CSS. One general rule I do have though in a world of variable spacing is at least for Android/Material apps, using multiples of
4.dp
is always handy to keep UI spacing harmonious across your layouts with minimal effort.
s

Stylianos Gakis

12/25/2021, 6:35 AM
Hey thanks for taking the time to write down all this advice, I do appreciate it!
👍 2
Re-reading this post 8 months later (damn time flies) and I must say I’ve pretty much defaulted to going with Spaces in the majority of the cases with the exception being when for sure all items are equally away from each other so I just use
spacedBy
. The idea of the items themselves knowing about their own space has never worked for me still. Depending on different contexts these items are being used in, it’s always easier to reason about by adding the spacer itself. Either I’m interpreting the designs weirdly and I don’t quite get that spacing, or not sure 😅
c

Colton Idle

08/26/2022, 10:22 PM
Yeah. thats what i do basically. https://mxstbr.com/thoughts/margin/
"margin considered harmful" blog post basically says that putting padding into a component makes it less reusable. and ive definitely swayed that way over time as well.
https://chris.banes.dev/always-provide-a-modifier/ has also pushed me towards doing that.
c

Chris Sinco [G]

08/26/2022, 10:26 PM
Yeah always passing a modifier is a nice tool