Created
December 4, 2025 16:33
-
-
Save rubywai/f3d71b65c865a8919421b51c67c3a953 to your computer and use it in GitHub Desktop.
News feed ui compose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.rubylearner.mycomposelesson | |
| import android.os.Bundle | |
| import android.widget.Toast | |
| import androidx.activity.ComponentActivity | |
| import androidx.activity.compose.setContent | |
| import androidx.activity.enableEdgeToEdge | |
| import androidx.compose.foundation.Image | |
| import androidx.compose.foundation.background | |
| import androidx.compose.foundation.layout.Arrangement | |
| import androidx.compose.ui.draw.clip | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.Column | |
| import androidx.compose.foundation.layout.Row | |
| import androidx.compose.foundation.layout.fillMaxWidth | |
| import androidx.compose.foundation.layout.height | |
| import androidx.compose.foundation.layout.padding | |
| import androidx.compose.foundation.layout.size | |
| import androidx.compose.foundation.shape.CircleShape | |
| import androidx.compose.foundation.shape.RoundedCornerShape | |
| import androidx.compose.material.icons.Icons | |
| import androidx.compose.material.icons.filled.Close | |
| import androidx.compose.material.icons.filled.MailOutline | |
| import androidx.compose.material.icons.filled.MoreVert | |
| import androidx.compose.material.icons.filled.Notifications | |
| import androidx.compose.material.icons.filled.Share | |
| import androidx.compose.material.icons.filled.ThumbUp | |
| import androidx.compose.material3.Badge | |
| import androidx.compose.material3.BadgedBox | |
| import androidx.compose.material3.CenterAlignedTopAppBar | |
| import androidx.compose.material3.ExperimentalMaterial3Api | |
| import androidx.compose.material3.Icon | |
| import androidx.compose.material3.IconButton | |
| import androidx.compose.material3.Scaffold | |
| import androidx.compose.material3.Text | |
| import androidx.compose.material3.TopAppBarDefaults | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.graphics.Color | |
| import androidx.compose.ui.text.font.FontWeight | |
| import androidx.compose.ui.layout.ContentScale | |
| import androidx.compose.ui.res.painterResource | |
| import androidx.compose.ui.unit.dp | |
| import androidx.compose.ui.unit.sp | |
| import com.rubylearner.mycomposelesson.ui.theme.MyComposeLessonTheme | |
| //Scaffold related compose | |
| //Row Column | |
| class MainActivity : ComponentActivity() { | |
| @OptIn(ExperimentalMaterial3Api::class) | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| enableEdgeToEdge() | |
| setContent { | |
| MyComposeLessonTheme { | |
| Scaffold( | |
| topBar = { | |
| CenterAlignedTopAppBar( | |
| colors = TopAppBarDefaults.centerAlignedTopAppBarColors() | |
| .copy( | |
| containerColor = Color.Red, | |
| titleContentColor = Color.White, | |
| navigationIconContentColor = Color.White, | |
| actionIconContentColor = Color.White, | |
| ), | |
| title = { | |
| Text("Compose Lesson") | |
| }, | |
| navigationIcon = { | |
| IconButton( | |
| onClick = { | |
| Toast.makeText( | |
| this@MainActivity, | |
| "Close", | |
| Toast.LENGTH_SHORT | |
| ).show() | |
| } | |
| ) { | |
| Icon( | |
| Icons.Default.Close, | |
| contentDescription = "Close", | |
| ) | |
| } | |
| }, | |
| actions = { | |
| BadgedBox( | |
| badge = { | |
| Badge { Text("99") } | |
| } | |
| ) { | |
| IconButton( | |
| onClick = { | |
| } | |
| ) { | |
| Icon( | |
| Icons.Default.Notifications, | |
| contentDescription = "Notification", | |
| ) | |
| } | |
| } | |
| Text( | |
| "Setting", | |
| color = Color.White, | |
| ) | |
| } | |
| ) | |
| }, | |
| bottomBar = { | |
| Box( | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .height(100.dp) | |
| .background(Color.Red) | |
| ) | |
| }, | |
| ) { | |
| Box(modifier = Modifier.padding(it)) { | |
| NewFeedUI() | |
| } | |
| } | |
| } | |
| } | |
| } | |
| @Composable | |
| fun NewFeedUI() { | |
| Column( | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .background(Color.White) | |
| .padding(8.dp) | |
| ) { | |
| // Header: Profile + Name + More Options | |
| Row( | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .padding(8.dp), | |
| verticalAlignment = Alignment.CenterVertically | |
| ) { | |
| // Profile Image | |
| Image( | |
| painter = painterResource(id = R.drawable.cat), | |
| contentDescription = "Profile", | |
| modifier = Modifier | |
| .size(40.dp) | |
| .clip(CircleShape), | |
| contentScale = ContentScale.Crop | |
| ) | |
| // Name and time | |
| Column( | |
| modifier = Modifier | |
| .weight(1f) | |
| .padding(horizontal = 8.dp) | |
| ) { | |
| Text( | |
| text = "Ruby Wai", | |
| fontWeight = FontWeight.Bold, | |
| fontSize = 16.sp, | |
| color = Color.Black | |
| ) | |
| Text( | |
| text = "2 hours ago", | |
| fontSize = 12.sp, | |
| color = Color.Gray | |
| ) | |
| } | |
| // More options button | |
| IconButton(onClick = { | |
| Toast.makeText( | |
| this@MainActivity, | |
| "More options", | |
| Toast.LENGTH_SHORT | |
| ).show() | |
| }) { | |
| Icon( | |
| Icons.Default.MoreVert, | |
| contentDescription = "More", | |
| tint = Color.Gray | |
| ) | |
| } | |
| } | |
| // Post text | |
| Text( | |
| text = "Check out this cute cat! 🐱", | |
| fontSize = 14.sp, | |
| color = Color.Black, | |
| modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp) | |
| ) | |
| // Post Image | |
| Image( | |
| painter = painterResource(id = R.drawable.cat), | |
| contentDescription = "Post Image", | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .height(300.dp) | |
| .clip(RoundedCornerShape(8.dp)), | |
| contentScale = ContentScale.Crop | |
| ) | |
| // Like, Comment, Share buttons | |
| Row( | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .padding(8.dp), | |
| horizontalArrangement = Arrangement.SpaceEvenly | |
| ) { | |
| // Like Button | |
| Row( | |
| modifier = Modifier | |
| .weight(1f) | |
| .padding(8.dp), | |
| horizontalArrangement = Arrangement.Center, | |
| verticalAlignment = Alignment.CenterVertically | |
| ) { | |
| Icon( | |
| Icons.Default.ThumbUp, | |
| contentDescription = "Like", | |
| tint = Color.Gray, | |
| modifier = Modifier.size(20.dp) | |
| ) | |
| Text( | |
| text = "Like", | |
| fontSize = 14.sp, | |
| color = Color.Gray, | |
| modifier = Modifier.padding(start = 4.dp) | |
| ) | |
| } | |
| // Comment Button | |
| Row( | |
| modifier = Modifier | |
| .weight(1f) | |
| .padding(8.dp), | |
| horizontalArrangement = Arrangement.Center, | |
| verticalAlignment = Alignment.CenterVertically | |
| ) { | |
| Icon( | |
| Icons.Default.MailOutline, | |
| contentDescription = "Comment", | |
| tint = Color.Gray, | |
| modifier = Modifier.size(20.dp) | |
| ) | |
| Text( | |
| text = "Comment", | |
| fontSize = 14.sp, | |
| color = Color.Gray, | |
| modifier = Modifier.padding(start = 4.dp) | |
| ) | |
| } | |
| // Share Button | |
| Row( | |
| modifier = Modifier | |
| .weight(1f) | |
| .padding(8.dp), | |
| horizontalArrangement = Arrangement.Center, | |
| verticalAlignment = Alignment.CenterVertically | |
| ) { | |
| Icon( | |
| Icons.Default.Share, | |
| contentDescription = "Share", | |
| tint = Color.Gray, | |
| modifier = Modifier.size(20.dp) | |
| ) | |
| Text( | |
| text = "Share", | |
| fontSize = 14.sp, | |
| color = Color.Gray, | |
| modifier = Modifier.padding(start = 4.dp) | |
| ) | |
| } | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment