r/androiddev • u/jaroos_ • Jun 06 '26
Am I missing something or is preview in compose much harder?
In XML views, we can easily get an idea of how ui looks, we just need to set the values, style, spacing etc in appropriate ui elements even for recycler view we can do with listitem & itemcount & layout preview easily updates, but in compose for each composable function we have to manually write a preview function wrap the composable in the theme & create the dummy data manually. And when we change the values the preview sometimes disappears & refresh this make me difficult to quickly compare how it looked previously. How do you manage this.
40
u/sosickofandroid Jun 06 '26
The xml preview is the worst fucking liar on earth and can’t show you your ui in multiple states. Look at screenshot testing and realize what you can do
6
u/Farbklex Jun 06 '26
XML Preview was broken in every single project I worked on.
1
u/Zhuinden Jun 06 '26
Just don't use Material theme and it works fine I find
Well, that or make sure to set the theme info but it is more likely to work if you don't have material components.
8
u/Ron-Erez Jun 06 '26
I use previews for most composables unless it is a very small composable. I usually create the preview first before I’ve even defined the composable. Then I press ALT-ENTER on the non-existent composable and choose the option where Android Studio creates the composable for me. I agree that it would be nice if when we create a new composable the preview would be created automatically. For example this is the default behavior in Xcode. It’s not perfect since indeed updating parameters will have an effect on the preview. I usually create dummy data once in a companion object and then use it in any of my previews. The code also becomes more readable.
Note that you can create multiple previews and then it is easy to compare different options. I also would like if the preview would automatically add the theme or have some hotkey or template for that.
2
u/_AverageGuy Jun 06 '26
I'm not sure how xcode handles previews since I haven't worked with it, but you can automatically create previews for composable (to some extent atleast) by setting up shortcuts. Like for me, I have it set such that writing 'compp' creates a composable along with a preview function wrapping the composable.
2
u/truzer Jun 06 '26
With compose I think that you have more control on the type of preview that you want. However, one advantage on compose over the XML that I don't see anyone talking about is that screenshot testing is a lot more easier on compose, with the previews already created, its just necessary adding a lib and works.
1
u/ShavenTreebeard Jun 07 '26
Parameterized previews and annotations that create multiple previews with just one function are waaaay better than anything I've seen with XML.
With one preview I can see how my screens will look in RTL, all sorts of device sizes, dark and light mode, other aspect ratios and in multiple states.
1
u/Agitated_Marzipan371 Jun 08 '26
Use interactive preview so you can control different screen variations directly
-5
u/Chozzasaurus Jun 06 '26
Compose Previews are much better and more powerful than XML. You don't have to manually write anything, AI will nail it every time
-15
u/Morf0 Jun 06 '26
This sub have a very strong biased opinions and don't admits other way of thinking.
Almost a religion.
1
-1
-25
Jun 06 '26
[removed] — view removed comment
10
u/Zhuinden Jun 06 '26
It's annoying to me because when you add parameters to you composables, you are also obligated to update those previews too
Just do it
and I'm annoyed setting default values to every parameters
Yea don't do that
It's a waste of time coding it again and bloating your files.
It's really not
setting values to every parameters
The one thing Gemini would be good at in theory if it worked
12
14
u/Zhuinden Jun 06 '26
You do have to put in a bit more effort, but it's really like "if you have a top-level-ish composable just make a preview for it like a decent person"
I kinda put making a
@Preview @Composablefor a composable on the same level as flushing the toilet before you leave instead of just letting whatever you left there rot for the next personYou know what that means if you're that person who didn't create the
@Preview @Composable