Skip to content

Commit fc4c99b

Browse files
committed
Add constraintLayout to build Indian Flag
1 parent b3c152c commit fc4c99b

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ dependencies {
6060
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
6161
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
6262
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
63+
64+
implementation "androidx.constraintlayout:constraintlayout-compose:1.0.1"
6365
}

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/MainActivity.kt

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@ package com.lahsuak.apps.jetpackcomposebasic
33
import android.os.Bundle
44
import androidx.activity.ComponentActivity
55
import androidx.activity.compose.setContent
6+
import androidx.compose.foundation.background
7+
import androidx.compose.foundation.layout.Box
68
import androidx.compose.foundation.layout.fillMaxSize
9+
import androidx.compose.foundation.shape.CircleShape
710
import androidx.compose.material3.MaterialTheme
811
import androidx.compose.material3.Surface
9-
import androidx.compose.material3.Text
1012
import androidx.compose.runtime.Composable
1113
import androidx.compose.ui.Modifier
14+
import androidx.compose.ui.draw.clip
15+
import androidx.compose.ui.graphics.Color
16+
import androidx.compose.ui.layout.layoutId
1217
import androidx.compose.ui.tooling.preview.Preview
18+
import androidx.compose.ui.unit.dp
19+
import androidx.constraintlayout.compose.ChainStyle
20+
import androidx.constraintlayout.compose.ConstraintLayout
21+
import androidx.constraintlayout.compose.ConstraintSet
22+
import androidx.constraintlayout.compose.Dimension
1323
import com.lahsuak.apps.jetpackcomposebasic.ui.theme.JetPackComposeBasicTheme
1424

1525
class MainActivity : ComponentActivity() {
@@ -22,29 +32,70 @@ class MainActivity : ComponentActivity() {
2232
modifier = Modifier.fillMaxSize(),
2333
color = MaterialTheme.colorScheme.background
2434
) {
25-
Greeting("Android")
35+
IndianFlagScreen()
2636
}
2737
}
2838
}
2939
}
3040
}
31-
/***
32-
Composable functions :
33-
A composable function is a regular function annotated with @Composable.
34-
This enables your function to call other @Composable functions within it.
35-
You can see how the Greeting function is marked as @Composable.
36-
This function will produce a piece of UI hierarchy displaying the given input,
37-
String. Text is a composable function provided by the library.
38-
***/
41+
3942
@Composable
40-
fun Greeting(name: String) {
41-
Text(text = "Hello $name!")
43+
fun IndianFlagScreen() {
44+
val constraints = ConstraintSet {
45+
val orangeBox = createRefFor("orangebox")
46+
val greenBox = createRefFor("greenbox")
47+
val circle = createRefFor("circle")
48+
49+
constrain(orangeBox) {
50+
top.linkTo(parent.top)
51+
start.linkTo(parent.start)
52+
end.linkTo(parent.end)
53+
bottom.linkTo(circle.top)
54+
width = Dimension.fillToConstraints
55+
height = Dimension.value(260.dp)
56+
}
57+
constrain(circle) {
58+
top.linkTo(parent.top)
59+
bottom.linkTo(parent.bottom)
60+
start.linkTo(parent.start)
61+
end.linkTo(parent.end)
62+
width = Dimension.value(100.dp)
63+
height = Dimension.value(100.dp)
64+
}
65+
constrain(greenBox) {
66+
top.linkTo(circle.bottom)
67+
bottom.linkTo(parent.bottom)
68+
start.linkTo(parent.start)
69+
end.linkTo(parent.end)
70+
width = Dimension.fillToConstraints
71+
height = Dimension.value(260.dp)
72+
}
73+
createVerticalChain(orangeBox, circle, greenBox, chainStyle = ChainStyle.SpreadInside)
74+
}
75+
ConstraintLayout(constraints, modifier = Modifier.background(Color.White).fillMaxSize()) {
76+
Box(
77+
modifier = Modifier
78+
.background(Color(0xFFFB8C00))
79+
.layoutId("orangebox")
80+
)
81+
Box(
82+
modifier = Modifier
83+
.clip(CircleShape)
84+
.background(Color.Blue)
85+
.layoutId("circle")
86+
)
87+
Box(
88+
modifier = Modifier
89+
.background(Color(0xFF2EB734))
90+
.layoutId("greenbox")
91+
)
92+
}
4293
}
4394

4495
@Preview(showBackground = true)
4596
@Composable
4697
fun DefaultPreview() {
4798
JetPackComposeBasicTheme {
48-
Greeting("Android")
99+
IndianFlagScreen()
49100
}
50101
}

0 commit comments

Comments
 (0)