https://kotlinlang.org logo
a

amar_1995

11/13/2019, 12:41 PM
I am using simple
TextField
to get userInput. But I am not able to write anything in
TextFeild
Copy code
@Composable
fun EditTextView(text: String) {
    val textState = +state{ text }
    Padding(left = 8.dp, top = 8.dp, right = 8.dp) {
        Surface(
            color = Color.Gray,
            shape = RoundedCornerShape(8.dp)
        ) {
            Padding(padding = 5.dp) {
                TextField(value = textState.value,
                    onValueChange = { textState.value = it })
            }
            
        }
    }
}
b

bmo

11/13/2019, 1:19 PM
I believe it should be like this :
Copy code
@Composable
fun EditTextView(text: String) {
    var textState by state{ text }
    Padding(left = 8.dp, top = 8.dp, right = 8.dp) {
        Surface(
            color = Color.Gray,
            shape = RoundedCornerShape(8.dp)
        ) {
            Padding(padding = 5.dp) {
                TextField(value = textState,
                    onValueChange = { textState = it })
            }
            
        }
    }
}
a

amar_1995

11/13/2019, 1:33 PM
What is a difference between
+state
and
by state
a

Adam Powell

11/13/2019, 1:34 PM
Not having to type
.value
when using it
Still need the + either way though
👍 1
m

Muhammad Usman

11/14/2019, 5:33 AM
@bmo I am creating simple edittext , i tried your composable fun but it says Type Effect<State<Stromg>> has no metheds getValue and set value on var textState by state{ text }
a

amar_1995

11/14/2019, 5:46 AM
@Muhammad Usman I am recieving the same error
Try using this
var textState by +state{ "" }
And as @Adam Powell suggested you don't have to use
.value
while calling textFeild
m

Muhammad Usman

11/14/2019, 5:57 AM
@amar_1995 thanks
a

amar_1995

11/14/2019, 6:07 AM
But still the problem exist cannot able to get userInput.
If I turn on this show virtual keyboard option. I am getting this error.
Copy code
kotlin.NotImplementedError: An operation is not implemented: not implemented
        at androidx.ui.core.input.RecordingInputConnection.requestCursorUpdates(RecordingInputConnection.kt:244)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:527)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:93)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I beleive there is some issue with textFeild. Tried the sample code as writen in google
ui-framework-integration-tests-sample
Copy code
@Sampled
@Composable
fun EditorModelTextFieldSample() {
    val state = +state { EditorModel() }
    TextField(
        value = state.value,
        onValueChange = { state.value = it }
    )
}
b

bmo

11/14/2019, 7:09 AM
@amar_1995 I have used it in my sample app : https://github.com/bmonjoie/ComposeTipsCalculator/blob/master/app/src/main/java/be/xzan/composetipscalculator/HintedTextField.kt It does work but I didn't use a
state
for it. I'm propagating the value back up in order to change my model and then re-rendering the textfield with the value from my model : https://github.com/bmonjoie/ComposeTipsCalculator/blob/master/app/src/main/java/be/xzan/composetipscalculator/MainActivity.kt The reason I did that is if I stored the value in a
state
within my method, there is no easy way for me to retrieve that value later (or none that I could think of).
a

amar_1995

11/14/2019, 9:25 AM
I see, you was using
@Model
class and propagating value back up in order. In any change model will reload view. But, I want to know why simple code of
textFeild
not working using state. Is there any bug ?
b

bmo

11/14/2019, 9:58 AM
I have had issue with
state
and I used
stateFor
to fix that but no clue why to be honest ¯\_(ツ)_/¯
a

amar_1995

11/14/2019, 10:20 AM
I tried using
stateFor
but it is also not working.
Copy code
val state =
        +stateFor<String?>() { "sample1" }
    MaterialTheme() {
        TextField(value = state.value!!,
            onValueChange = { state.value = it})
    }
What am I doing wrong ?
b

bmo

11/14/2019, 1:56 PM
@amar_1995 You need to pass a value in the
()
it can be a value that you pass to your function or a constant. This should look like :
Copy code
@Composable
fun EditTextView(text: String) {
    val textState = +stateFor("sample1"){ text }
    Padding(left = 8.dp, top = 8.dp, right = 8.dp) {
        Surface(
            color = Color.Gray,
            shape = RoundedCornerShape(8.dp)
        ) {
            Padding(padding = 5.dp) {
                TextField(value = textState.value,
                    onValueChange = { textState.value = it })
            }
            
        }
    }
}
a

amar_1995

11/14/2019, 2:02 PM
It is now working for me. Issue is with the emulator, I was using genymotion emulator. But after running it in normal avd emulator it is working. Seems like there is some configuration issue with genymotion emulator.
4 Views