Skip to content

Commit d1da643

Browse files
Implemented MultiLineHintTextField;
1 parent a118f40 commit d1da643

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

app/src/main/java/xyz/teamgravity/multilinehinttextfield/MainActivity.kt

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@ package xyz.teamgravity.multilinehinttextfield
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.Column
68
import androidx.compose.foundation.layout.fillMaxSize
9+
import androidx.compose.foundation.layout.fillMaxWidth
10+
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.foundation.shape.RoundedCornerShape
712
import androidx.compose.material3.MaterialTheme
813
import androidx.compose.material3.Surface
9-
import androidx.compose.material3.Text
10-
import androidx.compose.runtime.Composable
14+
import androidx.compose.runtime.getValue
15+
import androidx.compose.runtime.mutableStateOf
16+
import androidx.compose.runtime.remember
17+
import androidx.compose.runtime.setValue
1118
import androidx.compose.ui.Modifier
12-
import androidx.compose.ui.tooling.preview.Preview
19+
import androidx.compose.ui.draw.clip
20+
import androidx.compose.ui.graphics.Color
21+
import androidx.compose.ui.unit.dp
1322
import xyz.teamgravity.multilinehinttextfield.ui.theme.MultiLineHintTextFieldTheme
1423

1524
class MainActivity : ComponentActivity() {
25+
1626
override fun onCreate(savedInstanceState: Bundle?) {
1727
super.onCreate(savedInstanceState)
1828
setContent {
@@ -21,6 +31,25 @@ class MainActivity : ComponentActivity() {
2131
modifier = Modifier.fillMaxSize(),
2232
color = MaterialTheme.colorScheme.background
2333
) {
34+
var text by remember { mutableStateOf("") }
35+
36+
Column(
37+
modifier = Modifier
38+
.fillMaxSize()
39+
.padding(16.dp)
40+
) {
41+
MultiLineHintTextField(
42+
value = text,
43+
onValueChanged = { text = it },
44+
hint = "1st line of hint\n2nd line of hint\n3rd line of hint",
45+
maxLines = 4,
46+
modifier = Modifier
47+
.fillMaxWidth()
48+
.clip(RoundedCornerShape(5.dp))
49+
.background(Color.LightGray)
50+
.padding(16.dp)
51+
)
52+
}
2453
}
2554
}
2655
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package xyz.teamgravity.multilinehinttextfield
2+
3+
import androidx.compose.foundation.layout.Box
4+
import androidx.compose.foundation.text.BasicTextField
5+
import androidx.compose.material3.MaterialTheme
6+
import androidx.compose.material3.Text
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.text.TextStyle
10+
11+
@Composable
12+
fun MultiLineHintTextField(
13+
value: String,
14+
onValueChanged: (String) -> Unit,
15+
hint: String,
16+
maxLines: Int,
17+
modifier: Modifier = Modifier,
18+
style: TextStyle = MaterialTheme.typography.bodyLarge,
19+
) {
20+
BasicTextField(
21+
value = value,
22+
onValueChange = onValueChanged,
23+
maxLines = maxLines,
24+
textStyle = style,
25+
decorationBox = { textfield ->
26+
Box(modifier = modifier) {
27+
if (value.isEmpty()) {
28+
Text(
29+
text = hint,
30+
style = style,
31+
)
32+
}
33+
textfield()
34+
}
35+
}
36+
)
37+
}

0 commit comments

Comments
 (0)