Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set TextInputLayout theme programmatically

Is there a way to change the theme of a TextInputLayout programmatically in Android. If I have the following TextInputLayout for ex.:

<android.support.design.widget.TextInputLayout
    android:id="@+id/label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:paddingTop="16dp"
    android:theme="@style/TextInputLayoutTheme"
    app:errorTextAppearance="@style/Error">

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"/>
</android.support.design.widget.TextInputLayout>

Could I somehow change this line android:theme="@style/TextInputLayoutTheme" to another theme programmatically?

like image 784
buellas Avatar asked Jan 25 '18 13:01

buellas


People also ask

How do I set TextInputLayout style programmatically?

Method 1 - Create TextInputLayout programmatically with wrapping Context with android. view. ContextThemeWrapper and use. TextInputLayout layout = new TextInputLayout(new ContextThemeWrapper(getContext(), R.


1 Answers

There is no way to change theme of any view or any layout at runtime. Because of themes and styles are applied during creation of view, recursively. (Themes also applies child views of layouts)

But, you can change that theme before creation of view using XML layout or programmatically.

Programmatically:

Method 1 - Create TextInputLayout programmatically with wrapping Context with android.view.ContextThemeWrapper and use.

TextInputLayout layout = new TextInputLayout(new ContextThemeWrapper(getContext(), R.style. TextInputLayoutTheme));

Method 2 - Extend TextInputLayout and use your own layout. Pass ContextThemeWrapper as context.

public class MyTextInputLayout extends TextInputLayout {
    public MyTextInputLayout(Context context) {
        super(new ContextThemeWrapper(context, R.style.AppTheme));
    }

    public MyTextInputLayout(Context context, AttributeSet attrs) {
        super(new ContextThemeWrapper(context, R.style.AppTheme), attrs);
    }

    public MyTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(new ContextThemeWrapper(context, R.style.AppTheme), attrs, defStyleAttr);
    }
}

Now, you can use MyTextInputLayout in your XML layout

With XML Layout:

1) In attrs.xml file, create new attribute named textInputLayoutTheme

<attr name="textInputLayoutTheme" format="reference"/>

2) In your AppTheme in styles.xml file set your @style/TextInputLayoutTheme as textInputLayoutTheme.

<resources>
    <style name="AppTheme" parent="PARENT_THEME">
        <item name="textInputLayoutTheme">@style/TextInputLayoutTheme</item>
    </style>

    <style name="AppTheme.Secondary">
        <item name="textInputLayoutTheme">@style/TextInputLayoutTheme_Secondary</item>
    </style>
</resources>

3) In your layout.xml file, set ?attr/textInputLayoutTheme as a TextInputLayout theme

<android.support.design.widget.TextInputLayout
    android:id="@+id/label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:paddingTop="16dp"
    android:theme="@?attr/textInputLayoutTheme"
    app:errorTextAppearance="@style/Error">

Now, when you change your application theme from AppTheme to AppTheme.Secondary TextInputLayoutTheme_Secondary will be used as a theme of your TextInputLayout instead of TextInputLayoutTheme.

like image 92
Tolga Okur Avatar answered Oct 01 '22 14:10

Tolga Okur