Home > Software engineering >  Is it possible to call a donut routine within the MainActivity using different parameters?
Is it possible to call a donut routine within the MainActivity using different parameters?

Time:01-01

    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                Row() {
                    donut(_size = 100.dp, _padding = 10.dp)
                }
            }
        }

        @Preview
        @Composable
        fun donut(_size: Dp = 120.dp, _padding: Dp = 5.dp) {
            Box(modifier = Modifier
                .size(_size)
                .clip(CircleShape)
                .background(Color.Green)
                .padding(_padding)
                .clip(CircleShape)
                .background(Color.Red))
        }
    }

Unable to find @Preview 'com.pelicancolder.chapter1_03.MainActivity.donut' The preview will display after rebuilding the project

Rebuilding the project revealed the same error message.

I am using compose 1.1.1 and Kotlin 1.6.10 so there appears to be no incompatibilities there.

build.gradle file:

    buildscript {
        ext {
            compose_ui_version = '1.1.1'
        }
    }// Top-level build file where you can add configuration options common to all sub-projects/modules.
    plugins {
        id 'com.android.application' version '7.3.1' apply false
        id 'com.android.library' version '7.3.1' apply false
        id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    }

jvm_target is 1.8

About:

    Android Studio Dolphin | 2021.3.1 Patch 1
    Build #AI-213.7172.25.2113.9123335, built on September 29, 2022
    Runtime version: 11.0.13 0-b1751.21-8125866 amd64
    VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
    Windows 10 10.0
    GC: G1 Young Generation, G1 Old Generation
    Memory: 1280M
    Cores: 12
    Registry:
        external.system.auto.import.disabled=true
        ide.text.editor.with.preview.show.floating.toolbar=false

CodePudding user response:

The @Preview is not supposed to be applied like this and I see a couple of problems in your code.

  1. a @Composable function is supposed to be a top level function, not within a class, so move your donut function outside MainActivity. Also for code structure and if you don't mind them being public to your whole project I would move the composables to a different file named with the same name as the primary composable within the file. If you would like though, you can keep it in the same file as the activity and make them private.
  2. Composables naming convention wants their name to start with a capital letter unlike what we are used to with Kotlin or Java functions/methods.
  3. Your composable is supposed to be used by your code as you do in your MainActivity but no code used by your code should have the @Preview annotation. Remove the @Preview annotation from your donut function.
  4. Create another function to be your previewable composable to call your donut function like this (it can be private)
@Preview
@Composable
private fun DonutPreview() {
    Donut()
}
  • Related