Hello everyone, very minor problem here, I get 2 ...
# compose-android
k
Hello everyone, very minor problem here, I get 2 results for my code, one from the preview and one on the virtual device:
Copy code
The code:
package com.example.basicscodelab

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.basicscodelab.ui.theme.BasicsCodelabTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            BasicsCodelabTheme {
                MyApp(modifier = Modifier)
            }
        }
    }
}

@Composable
private fun MyApp(
    modifier: Modifier = Modifier,
    names: List<String> = listOf("World", "Compose")
) {
    Column(
        modifier = Modifier
            .padding(vertical = 4.dp)
    ) {
        for (name in names) {
            Greeting(name = name)
        }
    }
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Surface(
        modifier = modifier
            .padding(vertical = 5.dp, horizontal = 8.dp)
            .fillMaxWidth(),
            //.border(width = 4.dp, color = Gray)
        color = MaterialTheme.colorScheme.primary
    ) {
        Row(
            modifier = Modifier
                .padding(24.dp)
               //.border(width = 1.dp, color = Red)
        ) {
            Column(
                modifier = Modifier
                    //.border(width = 2.dp, color = Black)
            ) {
                Text(
                    text = "Hello,",
                    modifier = Modifier
                        //.border(width = 1.dp, color = White)
                        .padding(top = 10.dp, end = 100.dp)
                )
                Text(
                    text = "$name!",
                    modifier = Modifier
                        //.border(width = 1.dp, color = White)
                )
            }
                ElevatedButton(
                    onClick = {},
                    modifier = Modifier
                        //.border(width = 1.dp, color = Yellow)
                        .padding(start = 50.dp)
                ) {
                    Text(text = "Show more")
                }
            }
        }
    }

@Preview(showBackground = true, widthDp = 320)
@Composable
fun GreetingPreview() {
    BasicsCodelabTheme {
        MyApp()
    }
}
🧵 3
ł
You should post everything in one thread to avoid spamming the channel. If you remove
widthDp = 320
or add for example.
Copy code
@Preview(showBackground = true, device = Devices.NEXUS_5)
@Preview(showBackground = true, device = Devices.PIXEL_4)
@Preview(showBackground = true, device = Devices.PIXEL_4_XL)
@Composable
fun GreetingPreview() {
...
then it works as you wanted
k
Sorry about that. About the code, thanks it works, I forgotten that value.
I moved the code into that topic to reduce the non desired "flood".
s
Hey, you've been asked a couple of times already, you came back to write another comment, but didn't edit your original one. Could you please edit it now so that you're not bloating the main channel so much?
k
I try to edit my message, but… I don't see any button for that…! I don't know why, sorry 😐.
l
It's too late now
k
I'll try to be more careful and use online pastebin to avoid such inconvenience, thx.
l
No
Just put the code in a "text snippet" (see the + button) or in the thread. No need to add an indirection