google-developer-training / basic-android-kotlin-compose-training-practice-problems

Apache License 2.0
90 stars 112 forks source link

Help! Only the two left quadrants appear. #12

Closed sgkouzias closed 2 months ago

sgkouzias commented 9 months ago

With the code below only the left quadrants appear. I still can't figure out the solution.


package com.example.composequadrant

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composequadrant.ui.theme.ComposeQuadrantTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeQuadrantTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Quadrants(
                        titleUpperRight = "Text composable",
                        textUpperRight = "Displays text and follows the recommended Material Design guidelines.",
                        colorUpperRight = Color(0xFFEADDFF),
                        titleUpperLeft = "Image composable",
                        textUpperLeft = "Creates a composable that lays out and draws a given Painter class object.",
                        colorUpperLeft = Color(0xFFD0BCFF),
                        titleBottomRight = "Row composable",
                        textBottomRight = "A layout composable that places its children in a horizontal sequence.",
                        colorBottomRight = Color(0xFFB69DF8),
                        titleBottomLeft = "Column composable",
                        textBottomLeft = "A layout composable that places its children in a vertical sequence.",
                        colorBottomLeft = Color(0xFFF6EDFF)
                    )
                }
            }
        }
    }
}

@Composable
fun ComposableInfoCard(title: String, description: String,
                        color: Color, modifier: Modifier = Modifier) {
    // Setup background, padding and size modifiers. Also, set the alignment to center - vertically as well
    // as horizontally
    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = modifier
            .fillMaxSize()
            .background(color)
            .padding(16.dp)
    ) {
        Text(
            text = title,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.padding(bottom = 16.dp)
        )
        Text(
            text = description,
            textAlign = TextAlign.Justify,
        )
    }
}

@Composable
fun Quadrants(titleUpperRight: String, textUpperRight: String, colorUpperRight: Color,
              titleUpperLeft: String, textUpperLeft: String, colorUpperLeft: Color,
              titleBottomRight: String, textBottomRight: String, colorBottomRight: Color,
              titleBottomLeft: String, textBottomLeft: String, colorBottomLeft: Color,
              modifier: Modifier = Modifier) {
    Column(modifier = modifier) {
        Row(modifier = Modifier.weight(1f)) {
            ComposableInfoCard(
                title = titleUpperRight,
                description = textUpperRight,
                color = colorUpperRight
            )
            ComposableInfoCard(
                title = titleUpperLeft,
                description = textUpperLeft,
                color = colorUpperLeft
            )
        }
        Row(modifier = Modifier.weight(1f)) {
            ComposableInfoCard(
                title = titleBottomRight,
                description = textBottomRight,
                color = colorBottomRight
                )
            ComposableInfoCard(
                title = titleBottomLeft,
                description = textBottomLeft,
                color = colorBottomLeft
                )
            }
        }

    }

@Preview(showBackground = true)
@Composable
fun QuadrantPreview() {
    ComposeQuadrantTheme {
        Quadrants("1,1,1",
            "1,1,2",
            Color(0xFFEADDFF),
            "1,2,1",
            "1,2,2",
            Color(0xFFD0BCFF),
            "1,2,1",
            "1,2,2",
            Color(0xFFB69DF8),
            "2,2,1",
            "2,2,2",
            Color(0xFFF6EDFF)
        )
    }
}
HackerGprat commented 3 months ago

your programming writing approach can be easy just look at others code, #15 and get some ideas from official code to.

official code

Your did well .. just guess why you are not getting 4 boxes? you forgot to add modifier = Modifier.weight(1f) i also dont know what is it, i have to google it, i guess it like css, just change the no. 1f to 2f or 3f and see effects

       Row(modifier = Modifier.weight(1f)) {
            ComposableInfoCard(
                title = titleUpperRight,
                description = textUpperRight,
                color = colorUpperRight,
                modifier = Modifier.weight(1f)  // πŸ‘ˆ just add this line
            )
Image description
You full code fixed with details ( πŸ‘ˆ click to expand ) ```kotlin @Composable fun ComposableInfoCard(title: String, description: String, color: Color, modifier: Modifier = Modifier) { // Setup background, padding and size modifiers. Also, set the alignment to center - vertically as well // as horizontally Column( verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally, modifier = modifier .fillMaxSize() .background(color) .padding(16.dp) ) { Text( text = title, fontWeight = FontWeight.Bold, modifier = Modifier.padding(bottom = 16.dp) ) Text( text = description, textAlign = TextAlign.Justify, ) } } @Composable fun Quadrants(titleUpperRight: String, textUpperRight: String, colorUpperRight: Color, titleUpperLeft: String, textUpperLeft: String, colorUpperLeft: Color, titleBottomRight: String, textBottomRight: String, colorBottomRight: Color, titleBottomLeft: String, textBottomLeft: String, colorBottomLeft: Color, modifier: Modifier = Modifier) { Column(modifier = modifier) { Row(modifier = Modifier.weight(1f)) { ComposableInfoCard( title = titleUpperRight, description = textUpperRight, color = colorUpperRight, modifier = Modifier.weight(1f) // πŸ‘ˆ just add this line ) ComposableInfoCard( title = titleUpperLeft, description = textUpperLeft, color = colorUpperLeft, modifier = Modifier.weight(1f) // πŸ‘ˆ just add this line ) } Row(modifier = Modifier.weight(1f)) { ComposableInfoCard( title = titleBottomRight, description = textBottomRight, color = colorBottomRight, modifier = Modifier.weight(1f) // πŸ‘ˆ just add this line ) ComposableInfoCard( title = titleBottomLeft, description = textBottomLeft, color = colorBottomLeft, modifier = Modifier.weight(1f) // πŸ‘ˆ just add this line ) } } } @Preview( showBackground = true, showSystemUi = true, name = "First Screen" ) @Composable fun QuadrantPreview() { ComposeQuadrantTheme { Quadrants("1,1,1", "1,1,2", Color(0xFFEADDFF), "1,2,1", "1,2,2", Color(0xFFD0BCFF), "1,2,1", "1,2,2", Color(0xFFB69DF8), "2,2,1", "2,2,2", Color(0xFFF6EDFF) ) } } ```

You can read this πŸ‘‡ you will get some more idea, because they got trouble something like you got

15