1

I have a text input layout with id login_mobile_number, I have imported this module to stop using findViewById methods:

import kotlinx.android.synthetic.main.activity_login.*

Whenever I try to get text from the TextInputLayout by

val loginMobileNumberString:String = login_mobile_number.editText.toString()

It returns the object name instead of the string typed in it like this:

android.support.design.widget.TextInputEditText{dfdb412 VFED..CL. .F...... 0,0-888,136 #7f0800e1 app:id/textInputEditText}

Why this is occuring? What is the proper method to get string from that field?

Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
Naveen
  • 769
  • 1
  • 9
  • 28

4 Answers4

4

You can use this;

val loginMobileNumberString = login_mobile_number.text.toString()

No need to specify as :String.

Cafer Mert Ceyhan
  • 1,640
  • 1
  • 13
  • 17
1

You just need to do this:

val loginMobileNumberString:String = login_mobile_number.text.toString()

Also, you don't need to take the text from the text input layout, he should only take care of the layout of the input, as is in the name, you can do it directly from the edit text. Just give it an id and import the same way you did with the text input layout.

1

Another way is to enclose it in quotes, making it a string

val loginMobileNumberString = "${login_mobile_number.text}"

or if login_mobile_number refers to a TextInputLayout

val loginMobileNumberString = "${login_mobile_number.editText.text}"

poby
  • 1,572
  • 15
  • 39
0

To get the Text from TextInputEditText within the TextInputLayout

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/descriptionInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/editTextDate2"
    tools:layout_editor_absoluteX="184dp">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:hint="Description" />
</com.google.android.material.textfield.TextInputLayout>

Get the Text

val description = descriptionInputLayout.editText?.text.toString()
println("Description: $goalDescription")
Thiago
  • 12,778
  • 14
  • 93
  • 110