. f Android Jetpack- Jetpack cardview sample ~ Android Developers Blog

Wednesday, 22 September 2021

Android Jetpack- Jetpack cardview sample

 Hi All,
         Today I am going to share Jetpack CardView sample. Here I am going to create a simple android cardview UI  withoutt using the android Xml and layout editor.

We are going to build the UI using Composable funtion(i.e: using Jetpack Compse).

Column :  We are going to use Column function for arranging the view vertically.
For arranging the view horizontally you can use Row function.

 

 Check MainActivity.kt file for Code,


package com.example.composesample
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.composesample.ui.theme.ComposeSampleTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Greeting(User("Mukesh","Welcome to Android Developer Solutions"))
}
}
}
data class User(
val author : String,
val description : String
)
@Composable
fun Greeting(user: User) {
Card(
backgroundColor = Color.White,
elevation = Dp(2F),
modifier = Modifier.padding(16.dp)
) {
Column(modifier = Modifier.padding(16.dp)) {
Text(text = "Hello ${user.author}!")
Text(text = user.description)
}
}
}
@Preview
@Composable
fun DefaultPreview() {
Greeting(User("Android","test"))
}
view raw MainActivity.kt hosted with ❤ by GitHub

Download complete code here

Hope this will help someone.
Enjoy coding.... :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.