https://kotlinlang.org logo
#compose
Title
# compose
p

Pablo

02/21/2024, 2:07 PM
Can someone help me migrating from using percentage sizes in view system to Compose? I'm searching for the best strategy with compose for making apps that haves nice appearance in small res devices and big res devices. In all the samples I'm seeing when learning compose, even in android official courses, they use fixed size, for example, an Image gets a Modifier.size(72.dp). I can't understant why that should be a good strategy. If you display it in a small res device, the image will appear big, but if you display it in a big resolution device, the image will apear super small. Is not much better to use percentage size in all the views? For now the only way I found to give percentage size on Composables is using weight, but it is not easy when you are trying to do complex screens. With constraintlayout was far more easy in view system. Which is the better way to use percentage for specifying sizes on Compose?
f

Filip Wiesner

02/21/2024, 2:16 PM
DP unit is not "fixed size" in sense of pixels so it should look the same on different pixel densities. You can read up on it here. And if we are talking about big screens in terms of real size and not density, you should optimize for bigger screens by using different layouts, not just by making everything bigger. You can read about that here.
👆 1
👆🏻 1
p

Pablo

02/21/2024, 2:50 PM
thank you @Filip Wiesner I already know all that, I already was on that step on Android View system, but the problem here is different, I wanna understand how to deal with percentage sizes in Compose
for the moment, weight is the only way I found
is there a better way to specify percentage sizes on Compose?
f

Filip Wiesner

02/21/2024, 2:56 PM
I was answering to this:
In all the samples I'm seeing when learning compose, even in android official courses, they use fixed size, for example, an Image gets a Modifier.size(72.dp). I can't understant why that should be a good strategy.
It's in official courses because that's what you are supposed to do. If you insist on percentage sizes, the
weight
and `fillMaxSize(ratio)`/`fillMaxWidth(ratio)` is probably what you want.
p

Pablo

02/21/2024, 2:57 PM
does exist any difference between using .dp on compose and using dp pixels on view system?
f

Filip Wiesner

02/21/2024, 2:58 PM
I don't think so. Only that you are now forced to use
SP
for text. You could use DP and SP interchangeably in view system if I remember correctly. But I haven't touched Views for a long time now
p

Pablo

02/21/2024, 2:58 PM
after years of experience I found that using dp pixels on view system was a bad strategy to fit a great amount of device sizes. You have two possibilities, generate different sizes for each device type, increasing the work load, or using percentage sizes
is the same problem present on .dp with compose?
f

Filip Wiesner

02/21/2024, 2:59 PM
DP is still DP in both worlds 🙂
Out of curiosity, how do you handle text sizes? Do you also somehow change them proportionally? And how does your app based on percentages work in split screen when you get only half the screen available? Does everything become tiny?
p

Pablo

02/21/2024, 3:07 PM
I never designed anything for split screen
think smart 1
And about text, In some projects which I specifically want to get bigger text when the screen is bigger, I used this library:
implementation 'com.intuit.sspssp android1.0.6'
1
in the most of the projects I simply combine using sp on text with percentage size on the other views
f

Filip Wiesner

02/21/2024, 3:14 PM
The problem with this approach is that if user uses your app on tablet or now on foldable phone, the UI becomes huge. The reason people use devices with bigger screens is that they want more space that can fit more information at once. If you just make everything bigger, you just waste space. Anyway, there is probably no need to argue. If you want percentages, use
weight
and `fillMaxSize(ratio)`/`fillMaxWidth(ratio)`. Or you can create your own custom layouts that can work better with percentages.
p

Pablo

02/21/2024, 4:20 PM
I'm with you, in some circunstances like foldable devices or huge screens the perfect solution is to develop a specific UI
I simply followed that approach until now because I was forced to optimize development effort efficiency and develop different UIs for a single app when you need to mantain a lot of apps and even develop new ones constantly it's sometimes hard
@Filip Wiesner Having a LazyColumn with a Card that haves a Image and a Text, how you decide the size in .dp of the image? I mean, I have 30 images, for example, and some of them are smaller than others. How you decide how many size asign to the image?
7 Views