Islam Assem
Islam Assem

Reputation: 1512

Android attribute works only from style?

I have textinputlayout and want to add an attribute to it but it work only when assigned from style

my style

 <style name="round_text" parent="Base.Widget.MaterialComponents.TextInputLayout">
    <item name="editTextStyle">@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox</item>
</style>

my view with style

 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_input"
    android:theme="?attr/textInputStyle"
    style="@style/round_text"
    android:layout_width="300dp"
android:layout_centerInParent="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

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

my view without style and attribute assigned directly

 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_input"
    android:theme="?attr/textInputStyle"
    android:editTextStyle="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox"
    android:layout_width="300dp"
android:layout_centerInParent="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

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

Upvotes: 2

Views: 288

Answers (1)

Nizar
Nizar

Reputation: 2254

There is no such thing as @style/Widget.MaterialComponents.TextInputEditText.OutlinedBox.

There is, however, the @style/Widget.MaterialComponents.TextInputLayout.OutlinedBox style for the TextInputLayout.

The attribute you assigned directly was TextInputEditText.OutlinedBox. It should be TextInputLayout.OutlinedBox

Please Note that there is no editTextStyle. The reason it was being applied from your style is because round_text had the parent="Base.Widget.MaterialComponents.TextInputLayout" which by default uses the TextInputLayout.OutlinedBox as style.


According to the Material Design Docs, you should do the following:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_input"
    android:theme="?attr/textInputStyle"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="300dp"
    android:layout_centerInParent="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

Upvotes: 1

Related Questions