Изменение стиля для TextInputEditText в Android приложении

Имеется такой код:

private fun createNewTextInputLayout(): TextInputLayout {
        val newTextInputLayout = TextInputLayout(
            ContextThemeWrapper(requireContext(), R.style.MyTextInputLayout),
            null,
            0
        )
        newTextInputLayout.layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            1f
        )

        return newTextInputLayout
    }

    private fun createNewEditText(): TextInputEditText {
        val newEditText = TextInputEditText(
            requireContext()
        )
        newEditText.layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        newEditText.id = View.generateViewId()

        return newEditText
    }

Какой должен быть TextInputEditText

Какой он получается

Все TextInputLayout и TextInputEditText кроме первого (первый скриншот) добавляются в коде, все элементы добавленные в коде имеют абсолютно другой вид (вторая картинка).

Как изменить TextInputEditText добавляемый в коде, чтобы он имел такой же стиль как изначально добавленный?


Ответы (1 шт):

Автор решения: jarongg

Разобрался в собственной проблеме

    private fun createNewTextInputLayout(): TextInputLayout {
    val newTextInputLayout = TextInputLayout(requireContext())
    newTextInputLayout.layoutParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT,
        1f
    )

    return newTextInputLayout
}

private fun createNewEditText(): TextInputEditText {
    val newEditText = TextInputEditText(
        ContextThemeWrapper(requireContext(), R.style.MyTextInputEditText),
        null,
        0
    )
    newEditText.layoutParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    )
    newEditText.textSize = 16f
    newEditText.id = View.generateViewId()
    newEditText.setBackgroundColor(resources.getColor(R.color.transparent, null))

    return newEditText
}

Применять стиль нужно было не к Layout-у, а к самому EditText

<style name="MyTextInputEditText" parent="@style/Widget.MaterialComponents.TextInputEditText.FilledBox"/>

К тому же оказалось, что при вводе данного стиля, Android Studio не выдаёт подсказок, что этот стиль есть, но он есть. Может проблема такая была только у меня, а может это был баг Android Studio.

Всем спасибо!

→ Ссылка