I have created a Grid out of using the new concept of List in Kotlin. I'm open to any feed back to how this code could be improved. It functions as expected and I'm happy with the results.
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
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.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.grid.ui.theme.DataSource
import com.example.grid.ui.theme.DataSource.topics
import com.example.grid.ui.theme.GridTheme
import com.example.grid.ui.theme.Topic
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
GridTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
GridApp()
}
}
}
}
}
@Composable
fun GridApp() {
GridList(gridList = DataSource.topics, modifier = Modifier)
}
@Composable
fun Topics(topic: Topic, modifier: Modifier = Modifier){
val number = topic.numbers.toString()
Card{
Row {
Image(painter = painterResource(topic.image), contentDescription = stringResource(topic.name),
modifier
.size(68.dp)
.fillMaxHeight())
Column(modifier.width(120.dp)){Text(LocalContext.current.getString(topic.name),
modifier = modifier
.padding(horizontal = 16.dp)
.padding(top = 16.dp)
.padding(bottom = 8.dp),
style = MaterialTheme.typography.bodyMedium)
Row(modifier = Modifier, horizontalArrangement = Arrangement.End){
Icon(painterResource(R.drawable.ic_grain), contentDescription = "few dots",
Modifier
.padding(start = 16.dp)
.padding(end = 8.dp))
Text((number), modifier)
}
}
}
}
}
@Composable
fun GridList(gridList: List<Topic>, modifier: Modifier = Modifier.padding(8.dp)){
LazyVerticalGrid(columns = GridCells.Fixed(2), verticalArrangement = Arrangement.spacedBy(8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = modifier.padding(8.dp)) {
items(topics) { topic ->
Topics(topic = topic)
}
}
}
@Preview
@Composable
private fun showTopicGrid() {
Topics(Topic(R.string.film, 321, R.drawable.film))
}