Skip to content

Instantly share code, notes, and snippets.

@aadennis
Created December 18, 2022 10:49
Show Gist options
  • Save aadennis/cf92317bec2816071cf732e30a92b006 to your computer and use it in GitHub Desktop.
Save aadennis/cf92317bec2816071cf732e30a92b006 to your computer and use it in GitHub Desktop.
Android Compose: weight modifier gives missing library. I don't know why.
package com.example.mahstaff
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
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.dp
import androidx.compose.ui.unit.sp
import com.example.mahstaff.ui.theme.MahStaffTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MahStaffTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
}
}
}
}
@Composable
fun MyApp(
modifier: Modifier = Modifier,
names: List<String> = listOf("World", "Compose")
) {
Column(modifier = modifier.padding(vertical = 4.dp)) {
for (name in names) {
Greeting(name = name)
}
}
}
@Composable
private fun Greeting(name: String) {
Surface(
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
) {
Row(modifier = Modifier.padding(24.dp)) {
Column(/* modifier = Modifier.weight(1f) */) {
Text(text = "Hello, ")
Text(text = name)
}
ElevatedButton(
onClick = { /* TODO */ }
) {
Text("Show more")
}
}
}
}
//@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
MahStaffTheme {
Greeting("Android")
}
}
@Preview(showBackground = true)
@Composable
fun MyAppPreview() {
MahStaffTheme {
MyApp()
}
}
@aadennis
Copy link
Author

And now it is working?!
Column(modifier = Modifier.weight(1f)) {
I can only imagine the compiler / Android Studio / intellisense was taking a long time to realise there was a perfectly good library there.
Will try to recreate the problem again with screen shots.

@aadennis
Copy link
Author

Frustrating. Cannot now get this to break, so I have none of the compile errors.
If I can get it to repeat, will paste screenshots here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment