hey everyone! :android-wave:can y'all pls help me ...
# compose
c
hey everyone! 👋can y'all pls help me to figure out why my
MainActivity
is blank? • the data I need to show, is in the
NatureCardsScreen
class in the same project, and my
MainActivity
just calls this screen. • `NatureCardsScreen`'s Previews are working perfectly fine, data is there and visible within this class. • if I put all the code from
NatureCardsScreen
in
MainActivity
, it's not blank anymore and I can see everything. But when I separate
NatureCardsScreen
from
MainActivity
, the activity is blank. What am I missing? (will post
MainActivity
code in the thread)
Copy code
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ThirtyDaysOfNatureTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    ThirtyDaysOfNature()
                }
            }
        }
    }
}

@Composable
fun ThirtyDaysOfNature() {
    NatureCardsScreen()
}
its really simple one, the
NatureCardsScreen()
shows all the previews on its own, is there something I have to include in the
MainActivity
in this case?
t
Does your NatureCardsScreen take a modifier? Assuming yes.. what happens if you alter to NatureCardsScreen(modifier = Modifier.background(Color.Red)). Do you get a red background?
❤️ 1
c
thank you for ur reply @Travis Griggs ! so I tried to add this Color, as you're saying, to the card - and I can see the red color in the NatureCardsScreen preview, but the MainActivity is still blank 😄 I simplified the
NatureCardsScreen
for the sake of testing, maybe there is anything wrong/suspicious here that I'm not seeing? Here is the code:
Copy code
class NatureCardsScreen {

    @Composable
    fun NatureCard(modifier: Modifier = Modifier) {
        ElevatedCard(
            elevation = CardDefaults.cardElevation(10.dp),
            modifier = Modifier.background(Color.Red)
        ) {
            Column(
                modifier = modifier.padding(16.dp),
            ) {
                Text(text = "Some text")
                Text(text = "Some more text")
            }
        }
    }

    @Preview(showBackground = true)
    @Composable
    fun NatureCardPreview() {
        ThirtyDaysOfNatureTheme {
            NatureCard()
        }
    }
}
m
NatureCardScreen is a class in your example code, not a composable and you're just creating an instance of a class in your main activity. Typically, you wouldn't need to put a composable in a class unless you had a specific reason to so you could delete the class and then use the NatureCard composable in the main activity
❤️ 1
c
thank you Mike! yes, I got rid of the class, but kept composables in the separate file, and I can successfully call them from the
MainActivity
👍