1

I can't figure out what the problem is. I'm trying to create a button, but one of the modifier attributes doesn't work, I tried wrapping it in a column, but it doesn't help.

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {

        }
    }
}

@Preview
@Composable
fun Greeting() {
    val context = LocalContext.current

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Blue)
            .padding(0.dp, 0.dp, 0.dp, 28.dp),
        verticalArrangement = Arrangement.SpaceEvenly
    ) {

        TextPlayList(str = R.string.main_text_Playlist_Maker)

        Button(
            onClick = { context.startActivity(Intent(context, SearchMain::class.java)) },
            colors = ButtonDefaults
                .outlinedButtonColors(backgroundColor = Color.White),
            shape = RoundedCornerShape(16.dp),
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .padding(17.dp, 0.dp, 15.dp, 8.dp)
        )

        {
            IconButton(R.drawable.ic_search_main)
            TextButton(str = R.string.main_button_search)
        }

        Button(
            onClick = { context.startActivity(Intent(context, MediaActivity::class.java)) },
            colors = ButtonDefaults
                .outlinedButtonColors(backgroundColor = Color.White),
            shape = RoundedCornerShape(16.dp),
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .padding(17.dp, 8.dp, 15.dp, 8.dp)
        )
        {
            IconButton(R.drawable.ic_media_main)
            TextButton(str = R.string.main_button_media)
        }

        Button(
            onClick = { context.startActivity(Intent(context, SettingMain::class.java)) },
            colors = ButtonDefaults
                .outlinedButtonColors(backgroundColor = Color.White),
            shape = RoundedCornerShape(16.dp),
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .padding(17.dp, 8.dp, 15.dp, 0.dp)
        )
        {
            IconButton(R.drawable.ic_setting_main)
            TextButton(str = R.string.main_button_setting)

        }
    }

}


@Composable
fun ButtonMain() {
    val context = LocalContext.current
        Button(
            onClick = { context.startActivity(Intent(context, SettingMain::class.java)) },
            colors = ButtonDefaults
                .outlinedButtonColors(backgroundColor = Color.White),
            shape = RoundedCornerShape(16.dp),
            modifier = Modifier
                .weight(1f, false)
                .fillMaxWidth()
                .padding(17.dp, 8.dp, 15.dp, 0.dp)
        ) {
            IconButton(draw = R.drawable.ic_setting_main)
            TextButton(str = R.string.main_button_setting)
        }

}


@Composable
fun IconButton(draw: Int) {
    Icon(
        painter = painterResource(id = draw),
        "Setting",
        Modifier.padding(10.dp),
        tint = Color.Black
    )
}

@Composable
fun TextButton(str: Int) {
    Text(
        text = stringResource(id = str),
        fontSize = 22.sp, color = Color.Black
    )
}

@Composable
fun TextPlayList(str: Int) {
    Text(
        text = stringResource(id = str),
        fontSize = 22.sp,
        color = Color.White,
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp, 14.dp, 210.dp, 40.dp)
    )
}

I tried to solve the problem like this, but it turns out not what I need. Modifier weight not available in jetpack compose 1.1.1

Rinat
  • 21
  • 1
  • Can you post the exact error message you're getting and where? – m0skit0 Mar 21 '23 at 18:10
  • None of the following functions can be called with the arguments supplied. Modifier.weight(Float, Boolean = ...) defined in androidx.compose.foundation.layout.ColumnScopeInstance Modifier.weight(Float, Boolean = ...) defined in androidx.compose.foundation.layout.RowScopeInstance – Rinat Mar 21 '23 at 18:12
  • This error occurs in the ButtonMain() method. with the weight modifier – Rinat Mar 21 '23 at 18:15

1 Answers1

1

The weight modifier you're using in ButtonMain is only available in ColumnScope or RowScope. What you need to do is pass the scope to ButtonMain. You can do that easily by making it an extension function of the ColumnScope, like so:

@Composable
fun ColumnScope.ButtonMain() {
    ...
}
Serge
  • 359
  • 2
  • 13