Search code examples
androidandroid-layoutandroid-edittextmaterial-designandroid-textinputlayout

Keep startIcon at top left position when using multi-line textview


I've got the following layout:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/vehicle_type"
    android:layout_marginTop="@dimen/layout_margin_default"
    app:startIconDrawable="@drawable/baseline_notes_24">
    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"/>
</com.google.android.material.textfield.TextInputLayout>

which looks like this:

current view

My goal is that the startIcon should remain at the top position:

goal view

Is that possible? I've looked into the options, which can be used for the TextInputLayout, however there doesn't seem any "gravity" option for the startIcon.


Solution

  • The startIcon is defined by a CheckableImageButton inflated with the design_text_input_start_icon layout

    There isn't a public API to get it , but as workaround you can use:

        val textField = findViewById<TextInputLayout>(R.id.textfield)
        val startIcon: CheckableImageButton =
            textField.findViewById(com.google.android.material.R.id.text_input_start_icon)
    
        val layoutParams = startIcon.layoutParams as LinearLayout.LayoutParams
        layoutParams.apply {
            gravity = Gravity.TOP or Gravity.LEFT
        }
    

    enter image description here