-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathTextFieldActivity.kt
441 lines (399 loc) · 22.7 KB
/
TextFieldActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package com.example.jetpackcompose.text
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.activity.compose.setContent
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.InternalTextApi
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import com.example.jetpackcompose.core.hideKeyboard
import com.example.jetpackcompose.image.TitleComponent
class TextFieldActivity : AppCompatActivity() {
@InternalTextApi
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 {
// We create a ScrollState that's "remember"ed to add proper support for a scrollable component.
// This allows us to also control the scroll position and other scroll related properties.
// remember calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. More details are available in the
// comments below.
val scrollState = rememberScrollState()
// 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.
Column(
modifier = Modifier.verticalScroll(scrollState)
) {
// Title Component is a custom composable that we created which is capable of
// rendering text on the screen in a certain font style & text size.
TitleComponent("This is a Simple Text Input field")
SimpleTextInputComponent()
TitleComponent("This is a TextInput with custom text style")
CustomStyleTextInputComponent()
TitleComponent("This is a TextInput suitable for typing numbers")
NumberTextInputComponent()
TitleComponent("This is a search view created using TextInput")
SearchImeActionInputComponent()
TitleComponent("This is a TextInput that uses a Password Visual Transformation")
PasswordVisualTransformationInputComponent()
TitleComponent("This is a filled TextInput field based on Material Design")
MaterialTextInputComponent()
}
}
}
}
// 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.
@InternalTextApi
@Composable
fun SimpleTextInputComponent() {
// Surface is a composable provided to fulfill the needs of the "Surface" metaphor from the
// Material Design specification. It's generally used to change the background color, add
// elevation, clip or add background shape to its children composables.
// 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, we assign a padding of
// 16dp to the Surface.
Surface(color = Color.LightGray, modifier = Modifier.padding(16.dp)) {
// BasicTextField is a composable that is capable of accepting text user input. It renders the
// value that you pass to the "value" field. In order to update this as the user is
// typing a new string, we make use of the state delegate.
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var textValue by remember { mutableStateOf(TextFieldValue("Enter your text here")) }
BasicTextField(
value = textValue,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
// Update value of textValue with the latest value of the text field
onValueChange = {
textValue = it
}
)
}
}
// 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.
@InternalTextApi
@Composable
fun CustomStyleTextInputComponent() {
// Surface is a composable provided to fulfill the needs of the "Surface" metaphor from the
// Material Design specification. It's generally used to change the background color, add
// elevation, clip or add background shape to its children composables.
// 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, we assign a padding of
// 16dp to the Surface.
Surface(color = Color.LightGray, modifier = Modifier.padding(16.dp)) {
// BasicTextField is a composable that is capable of accepting text user input. It renders the
// value that you pass to the "value" field. In order to update this as the user is
// typing a new string, we make use of the state delegate.
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var textValue by remember { mutableStateOf(TextFieldValue("Enter your text here")) }
BasicTextField(
value = textValue,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
// You can also customize the appearance of the TextInput by passing a TextStyle
// configuration to the TextField composable. If you don't pass this, it's just going
// to use the default values for all the properties.
textStyle = TextStyle(
color = Color.Blue,
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
textDecoration = TextDecoration.Underline
),
// Update value of textValue with the latest value of the text field
onValueChange = {
textValue = it
}
)
}
}
// 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.
@InternalTextApi
@Composable
fun NumberTextInputComponent() {
// Surface is a composable provided to fulfill the needs of the "Surface" metaphor from the
// Material Design specification. It's generally used to change the background color, add
// elevation, clip or add background shape to its children composables.
// 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, we assign a padding of
// 16dp to the Surface.
Surface(color = Color.LightGray, modifier = Modifier.padding(16.dp)) {
// BasicTextField is a composable that is capable of accepting text user input. It renders the
// value that you pass to the "value" field. In order to update this as the user is
// typing a new string, we make use of the state delegate.
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var textValue by remember { mutableStateOf(TextFieldValue("123")) }
BasicTextField(
value = textValue,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
// Setting the keyboard type allows you to configure what kind of data you can input
// in this TextInput. Some examples are number, phone, email, password, etc.
// Update value of textValue with the latest value of the text field
onValueChange = {
textValue = it
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number
)
)
}
}
// 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.
@InternalTextApi
@Composable
fun SearchImeActionInputComponent() {
// LocalContext is a LocalComposition for accessting the context value that we are used to using
// in Android.
// LocalComposition is an implicit way to pass values down the compose tree. Typically, we pass values
// down the compose tree by passing them as parameters. This makes it easy to have fairly
// modular and reusable components that are easy to test as well. However, for certain types
// of data where multiple components need to use it, it makes sense to have an implicit way
// to access this data. For such scenarios, we use LocalComposition. In this example, we use the
// LocalContext to get hold of the Context object. In order to get access to the latest
// value of the LocalComposition, use the "current" property eg - LocalContext.current. Some other
// examples of common LocalComposition's are LocalTextInputService,LocalDensity, etc.
val context = LocalContext.current
// Surface is a composable provided to fulfill the needs of the "Surface" metaphor from the
// Material Design specification. It's generally used to change the background color, add
// elevation, clip or add background shape to its children composables.
// 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, we assign a padding of
// 16dp to the Surface.
Surface(
color = Color.LightGray, modifier = Modifier.padding(16.dp),
shape = RoundedCornerShape(5.dp)
) {
// BasicTextField is a composable that is capable of accepting text user input. It renders the
// value that you pass to the "value" field. In order to update this as the user is
// typing a new string, we make use of the state delegate.
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var textValue by remember { mutableStateOf(TextFieldValue("Enter your search query here")) }
BasicTextField(
value = textValue,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
// Changing the imeAction allows you to change the primary icon of the keyboard which
// is typically shown in the bottom right corner of the keyboard. Some examples of
// ImeActions are search, send, done, go, etc.
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
// Update value of textValue with the latest value of the text field
onValueChange = {
textValue = it
},
keyboardActions = KeyboardActions(
onSearch = {
hideKeyboard(context)
}
)
)
}
}
// 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.
@InternalTextApi
@Composable
fun PasswordVisualTransformationInputComponent() {
// Surface is a composable provided to fulfill the needs of the "Surface" metaphor from the
// Material Design specification. It's generally used to change the background color, add
// elevation, clip or add background shape to its children composables.
// 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, we assign a padding of
// 16dp to the Surface.
Surface(color = Color.LightGray, modifier = Modifier.padding(16.dp)) {
// BasicTextField is a composable that is capable of accepting text user input. It renders the
// value that you pass to the "value" field. In order to update this as the user is
// typing a new string, we make use of the state delegate.
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var textValue by remember { mutableStateOf(TextFieldValue("Enter your password here")) }
BasicTextField(
value = textValue,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
// Visual transformation is used to modify the visual output of the input field. In
// this example, I'm using an existing visual transformation - the
// PasswordVisualTransformation. All it does is that it transforms any input character
// into "•". The text itself isn't altered, just its visual appearance is. You can
// easily created you own visual transformations by implementing the
// VisualTransformation interface.
visualTransformation = PasswordVisualTransformation(),
// Update value of textValue with the latest value of the text field
onValueChange = {
textValue = it
},
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Done,
keyboardType = KeyboardType.Password
)
)
}
}
// 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 MaterialTextInputComponent() {
var textValue by remember { mutableStateOf(TextFieldValue("")) }
// TextField is a composable that is capable of accepting text user input. It renders the
// value that you pass to the "value" field. In order to update this as the user is
// typing a new string, we make use of the state delegate. Reacting to state changes is
// the core behavior of Compose. Any composable that reads the value of the textValue
// field will recompose whenever this value is changed. In this example, since the
// TextField is reading the value from the textValue value, and that's also the value
// that we update as the user types (through the onValueChange lambda), this composable
// is redrawn and updated with the latest value.
// In particular, the FilledTextField is a Material Design implementation of the
// Material Filled TextField - https://material.io/components/text-fields/#filled-text-field
// It also gives us the ability to provide some placeholder text.
// We also pass it a modifier. 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, we
// assign a padding of 16dp and ask the FilledTextField composable to occupy the entire
// available width.
TextField(
value = textValue,
onValueChange = { textValue = it },
label = { Text("Enter Your Name") },
placeholder = { Text(text = "John Doe") },
modifier = Modifier
.padding(16.dp)
.fillMaxWidth()
)
}
/**
* 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.
*/
@InternalTextApi
@Preview
@Composable
fun SimpleTextInputComponentPreview() {
SimpleTextInputComponent()
}
@InternalTextApi
@Preview
@Composable
fun CustomStyleTextInputComponentPreview() {
CustomStyleTextInputComponent()
}
@InternalTextApi
@Preview
@Composable
fun NumberTextInputComponentPreview() {
NumberTextInputComponent()
}
@InternalTextApi
@Preview
@Composable
fun SearchImeActionInputComponentPreview() {
SearchImeActionInputComponent()
}
@InternalTextApi
@Preview
@Composable
fun PasswordVisualTransformationInputComponentPreview() {
PasswordVisualTransformationInputComponent()
}
@Preview
@Composable
fun FilledTextInputComponentPreview() {
MaterialTextInputComponent()
}