3

I'm using compose3 and dolphin Android Studio on Gruda Linux. All updated!

I have a @Composable function called feed which just have a FilledTonalButton.

For the content of FilledTonalButton I'm giving the icons.rounded.Add

Whenever I'm building, it gives me

e: /home/kumar-p/Desktop/AndroidStudio/<app_name>/app/src/main/java/com/f/rateme/MainActivity.kt: (95, 27): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public val Icons.Rounded.Add: ImageVector defined in androidx.compose.material.icons.rounded

<app_name> = my app name, removed because of confidentiality.

However when I replace the Icon() with Text(), it works!

My dependencies:

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.compose.material3:material3:1.0.0-alpha02'
implementation "androidx.compose.material:material-icons-extended:$compose_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

My imports on MainActivity.kt

package com.<example>.rateme // <example> = my company name

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material3.*
//import androidx.compose.material.icons.Icons
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview


import com.<example>.<app_name>.ui.theme.AppTheme // <example> = my company name, <app_name> = name of curent app

The function:

@Composable
fun feed(page: String): String {

    FilledTonalButton(
        onClick = {/*TODO: Add onClick for feed - New Post*/},
        enabled = true) {
        Icon(
            imageVector = Add,
            "New Post"
        )
    }

    return page // TODO: implement the changes of page variable
}

If you find any typo please update it or tell me to fix.

PS: This is my first time here and I have an almost zero experience on android development and Kotlin. I developed terminal apps and worked on ML kinda work in Python, C++, C. So I may need more information in explanation. I started learning Android Development a week ago only.

Edit: You can ask me any more information.

Peace

z.g.y
  • 5,512
  • 4
  • 10
  • 36
Prabhas Kumar
  • 230
  • 10

1 Answers1

1

Just specify the actual imageVector like this,

 Icon(
    imageVector = Icons.Default.Add
    "New Post"
 )

but you will still get ambiguity error. Since based on your post it looks like you want to use material3, just remove this import

import androidx.compose.material.* // remove this

or if you still want to use them both, you'll then have to declare their fully qualified name on usage,

androidx.compose.material3.Icon(
     imageVector = Icons.Default.Add,
     "New Post"
)
z.g.y
  • 5,512
  • 4
  • 10
  • 36
  • I checked the code and I’m not using material, all functions are in material 3. So I commented it out But error still occurs. Plus icon.Icons.default gives filled icon. I want rounded version of it that’s why I’m using icon.rounded – Prabhas Kumar Nov 26 '22 at 02:43
  • 1
    I copy pasted the codes in your question, there's an ambiguity in imports, `import androidx.compose.material.*` and `import androidx.compose.material3.*`, if you want filled Icon, just replace the one in my answer with `Icons.Filled.Add` – z.g.y Nov 26 '22 at 02:45
  • It worked. I just have one question: why `androidx.compose.material.icons.rounded.Add` caused error but `androidx.compose.material.icons.Icons.Rounded.Add` didn't even though it imported same `androidx.compose.material.icons.rounded.Add`? – Prabhas Kumar Nov 26 '22 at 02:55
  • 1
    if you want rounded, just call `Icons.Rounded.Add` or if you want want to shorten it just import `androidx.compose.material.icons.Icons.Rounded` and simply call `Rounded.Add` – z.g.y Nov 26 '22 at 05:51