1

This is my custom view:


<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

       <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout"
            style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="40dp"
            app:boxBackgroundColor="#E6DEDE"
            app:boxStrokeColor="@color/black"
            app:boxStrokeWidth="1dp"
            app:boxStrokeWidthFocused="2dp"

            app:hintTextColor="@color/black">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/textInputEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:inputType="textEmailAddress"
                android:textColor="@color/black"
                android:textSize="18sp" />

        </com.google.android.material.textfield.TextInputLayout>

   </LinearLayout>

    </merge>

This is custom view class:


@SuppressLint("MissingInflatedId")
class CustomEditText(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {

    private val textInputLayout: TextInputLayout
    private val editText: TextInputEditText

    init {
        val view = inflate(context, R.layout.custom_edit_text, this)
        textInputLayout = view.findViewById(R.id.textInputLayout)
        editText = view.findViewById(R.id.textInputEditText)
    }

    fun getEditText():TextInputEditText
    {
        return findViewById(R.id.textInputEditText)
    }

    fun setHint(hint: String){
        editText.hint = hint
    }

    fun getText() : String {
        return getEditText().text.toString()
    }
}

@BindingAdapter("hint")
fun setCustomHint(editText: CustomEditText,hint: String?) {
    if (hint != null) {
        editText.setHint(hint)
    }
    }

This is fragment code where I use this function:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val username = binding.etUsername.getTextText()
        binding.btnSignUp.setOnClickListener {
            Toast.makeText(context, username, Toast.LENGTH_SHORT).show()
        }

  }

I try to get text from custom edit text but it returns null - (""). I use custom view in my fragment. I want to get input from custom view's editext in fragment.

1 Answer 1

0

use

fun getEditText():TextInputEditText
    {
        return editText
   }

or in getTextFunction

 fun getText() : String {
    return editText.text.toString()
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.