import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
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.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
@Composable
fun HorizontalImageList() {
val items = listOf(
Pair("Label1", R.drawable.image1),
Pair("Label2", R.drawable.image2),
Pair("Label3", R.drawable.image3),
Pair("Label4", R.drawable.image4),
Pair("Label5", R.drawable.image5)
)
LazyRow(
contentPadding = PaddingValues(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp),
modifier = Modifier.fillMaxWidth()
) {
items(items) { item ->
Column(
modifier = Modifier.width(100.dp),
verticalArrangement = Arrangement.Center
) {
Image(
painter = painterResource(id = item.second),
contentDescription = item.first,
contentScale = ContentScale.Crop,
modifier = Modifier
.size(100.dp)
.clip(RoundedCornerShape(8.dp))
)
Spacer(modifier = Modifier.height(4.dp))
Text(
text = item.first,
style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.fillMaxWidth(),
maxLines = 1
)
}
}
}
}We use LazyRow to create a horizontal scrolling list. The items function iterates over the list of pairs containing labels and image resource IDs. Each item is shown in a Column with a fixed width of 100dp.
The Image composable displays the image with a size of 100dp by 100dp and rounded corners using clip and RoundedCornerShape. Below the image, a Text shows the label.
We add horizontal spacing of 12dp between items using horizontalArrangement = Arrangement.spacedBy(12.dp). Padding is added on the sides for better layout.
This creates a neat horizontal list that users can scroll left and right to see all images with labels.