package com.example.jetpackcompose.material import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.height import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.shadow import androidx.activity.compose.setContent import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.example.jetpackcompose.core.colors import com.example.jetpackcompose.image.TitleComponent class ShadowActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // This is an extension function of Activity that sets the @Composable function that's // passed to it as the root view of the activity. This is meant to replace the .xml file // that we would typically set using the setContent(R.id.xml_file) method. The setContent // block defines the activity's layout. setContent { ShadowComponent() } } } // We represent a Composable function by annotating it with the @Composable annotation. Composable // functions can only be called from within the scope of other composable functions. We should // think of composable functions to be similar to lego blocks - each composable function is in turn // built up of smaller composable functions. @Composable fun ShadowComponent() { // Column is a composable that places its children in a vertical sequence. You // can think of it similar to a LinearLayout with the vertical orientation. // In addition we also pass a few modifiers to it. // You can think of Modifiers as implementations of the decorators pattern that are used to // modify the composable that its applied to. In this example, as the Column composable to // occupy the entire available height & width using Modifier.fillMaxSize(). In addition, we // specify that the content of this Column should be centered. Column( modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { // We used another Composable composable to create a container that will have the shadow // applied. Column( // We specify that the box should have round corners with 8.dp as the radius. // It will occupy the maximum available width modifier = Modifier.fillMaxWidth() // with a height of 250 dp .height(250.dp) // and a padding of 16 dp .padding(16.dp) // In addition, we will also draw a shadow around the Box using the // shadow modifier. Because its a modifier, it can basically be applied // to any modifier without much hassle. It's that simple! .shadow( elevation = 3.dp, shape = RoundedCornerShape(8.dp) ) .background(color = colors[2]), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { // TitleComponent is a composable we created in one of the files that merely renders // text on the screen. TitleComponent("This container has a shadow applied to it") } } } /** * Android Studio lets you preview your composable functions within the IDE itself, instead of * needing to download the app to an Android device or emulator. This is a fantastic feature as you * can preview all your custom components(read composable functions) from the comforts of the IDE. * The main restriction is, the composable function must not take any parameters. If your composable * function requires a parameter, you can simply wrap your component inside another composable * function that doesn't take any parameters and call your composable function with the appropriate * params. Also, don't forget to annotate it with @Preview & @Composable annotations. */ @Preview @Composable fun ShadowComponentPreview() { ShadowComponent() }