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,
This file contains 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.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")) | |
} |
Download complete code here
Hope this will help someone.
Enjoy coding.... :)
Enjoy coding.... :)
0 comments:
Post a Comment