google-developer-training / basic-android-kotlin-compose-birthday-card-app

Apache License 2.0
113 stars 86 forks source link

Text composables | Template project code mismatch with Android Studio Android Studio Koala 2024.1.1 Patch 1; #808

Open HybridZach opened 1 month ago

HybridZach commented 1 month ago

URL of codelab

Step Step 9.

Describe Issue The default template for "Empty Activity" on Android Studio (Versions and OS below) Appears to give Scaffold not Surface.

Example;

import androidx.compose.material3.**Scaffold**

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                **Scaffold**(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                        name = "Android",
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

Where tutorial shows;

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            HappyBirthdayTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                }
            }
        }
    }
}

Therefore although the code can work following the tutorial, it does not appear to work when you get to the steps about aligning the text with textAlign, which I assumed was due to it using Scaffold and not Surface.

Given my assumption and noticing the differences between my code and the tutorial, I fixed it by importing what I assumed was missing for example;

import androidx.compose.material3.Surface
import androidx.compose.ui.Alignment
import androidx.compose.ui.text.style.TextAlign

Then I tried to match the code to the the tutorial the best I could and got it working as expected, I assume there is missing code and or imports from the outdated/incorrect template?

Steps to reproduce? In Android Studio New Project Phone and Tablet Empty Activity

Versions

Android Studio Koala | 2024.1.1 Patch 1 Build #AI-241.18034.62.2411.12071903, built on July 10, 2024 Runtime version: 17.0.11+0-17.0.11b1207.24-11852314 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Linux 6.1.52-valve16-1-neptune-61 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 8 Registry: ide.experimental.ui=true Current Desktop: KDE

Additional information I did get my Android Studio from Flathub, and initially thought this may be why there was differences in the templates and the tutorial, but as you can see it is the latest version (I think?).

HybridZach commented 1 month ago

Duplicate issues;