kotlinandroid-jetpack-compose

Android project not building due to "Unresolved reference: fontSize"


Completely new guy trying to learn Kotlin from developer.android.

I have a simple project to make a Text appear with different properties. I have been able to successfully change the text but am unable to resolve this issue.

The next step is to change font size but I getting nowhere. I've done exactly as the instructions advise. The relevant androidx.compose.ui.unit.sp has also been imported.

The 'fontSize' variable (80.sp) and it's value within the 'GreetingText' Composable is showing as error and preventing the build.

I've also attached the screenshot for your kind attention. Please let me know what I'm missing.

package com.example.happybirthday

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
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.sp
import com.example.happybirthday.ui.theme.HappyBirthdayTheme



class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            HappyBirthdayTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->

                }
            }
        }
    }
}

@Composable
fun GreetingText(message: String, modifier: Modifier = Modifier){
    Text (
        text = message
        fontSize = 80.sp
    )

}


@Preview(showBackground = true)
@Composable
fun BirthdayCardPreview() {
    HappyBirthdayTheme {
        GreetingText(message = "Happy Birthday Pithi!")
    }
}

The code that ain't processing.

I tried instructions, but I am stuck as the solution is unclear to me.


Solution

  • Please have a close look at your Text Composable. Let's break down what you do there on the Kotlin level.

    You are calling a function named Text() and provide two named arguments to it.
    When calling a function with parameters in Kotlin, the parameters need to be separated by a comma. This also holds true even if you spread the function parameters across several lines.

    You need to change your code like this:

    Text (
        text = message,
        fontSize = 80.sp
    )