Skip to content

Commit 0a8dbfe

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

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-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: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@ 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.layout.padding
10+
import androidx.compose.foundation.shape.CircleShape
711
import androidx.compose.material3.MaterialTheme
812
import androidx.compose.material3.Surface
9-
import androidx.compose.material3.Text
1013
import androidx.compose.runtime.Composable
1114
import androidx.compose.ui.Modifier
15+
import androidx.compose.ui.draw.clip
16+
import androidx.compose.ui.graphics.Color
17+
import androidx.compose.ui.layout.layoutId
1218
import androidx.compose.ui.tooling.preview.Preview
19+
import androidx.compose.ui.unit.dp
20+
import androidx.constraintlayout.compose.ChainStyle
21+
import androidx.constraintlayout.compose.ConstraintLayout
22+
import androidx.constraintlayout.compose.ConstraintSet
23+
import androidx.constraintlayout.compose.Dimension
1324
import com.lahsuak.apps.jetpackcomposebasic.ui.theme.JetPackComposeBasicTheme
1425

1526
class MainActivity : ComponentActivity() {
@@ -22,29 +33,70 @@ class MainActivity : ComponentActivity() {
2233
modifier = Modifier.fillMaxSize(),
2334
color = MaterialTheme.colorScheme.background
2435
) {
25-
Greeting("Android")
36+
IndianFlagScreen()
2637
}
2738
}
2839
}
2940
}
3041
}
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-
***/
42+
3943
@Composable
40-
fun Greeting(name: String) {
41-
Text(text = "Hello $name!")
44+
fun IndianFlagScreen() {
45+
val constraints = ConstraintSet {
46+
val orangeBox = createRefFor("orangebox")
47+
val greenBox = createRefFor("greenbox")
48+
val circle = createRefFor("circle")
49+
50+
constrain(orangeBox) {
51+
top.linkTo(parent.top)
52+
start.linkTo(parent.start)
53+
end.linkTo(parent.end)
54+
bottom.linkTo(circle.top)
55+
width = Dimension.fillToConstraints
56+
height = Dimension.value(260.dp)
57+
}
58+
constrain(circle) {
59+
top.linkTo(parent.top)
60+
bottom.linkTo(parent.bottom)
61+
start.linkTo(parent.start)
62+
end.linkTo(parent.end)
63+
width = Dimension.value(100.dp)
64+
height = Dimension.value(100.dp)
65+
}
66+
constrain(greenBox) {
67+
top.linkTo(circle.bottom)
68+
bottom.linkTo(parent.bottom)
69+
start.linkTo(parent.start)
70+
end.linkTo(parent.end)
71+
width = Dimension.fillToConstraints
72+
height = Dimension.value(260.dp)
73+
}
74+
createVerticalChain(orangeBox, circle, greenBox, chainStyle = ChainStyle.SpreadInside)
75+
}
76+
ConstraintLayout(constraints, modifier = Modifier.fillMaxSize()) {
77+
Box(
78+
modifier = Modifier
79+
.background(Color(0xFFFB8C00))
80+
.layoutId("orangebox")
81+
)
82+
Box(
83+
modifier = Modifier
84+
.clip(CircleShape)
85+
.background(Color.Blue)
86+
.layoutId("circle")
87+
)
88+
Box(
89+
modifier = Modifier
90+
.background(Color(0xFF2EB734))
91+
.layoutId("greenbox")
92+
)
93+
}
4294
}
4395

4496
@Preview(showBackground = true)
4597
@Composable
4698
fun DefaultPreview() {
4799
JetPackComposeBasicTheme {
48-
Greeting("Android")
100+
IndianFlagScreen()
49101
}
50102
}

0 commit comments

Comments
 (0)