Search code examples
androidmaterial-designandroid-textinputlayoutandroid-support-design

Why cant I use TextInputLayout (from support design library) withour Theme.Appcompat


Am using TextInputLayout as below

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edit_text_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_full_name"
        android:inputType="text" />
</android.support.design.widget.TextInputLayout>

The code works fine if my app uses Theme.AppCompat

Theme.AppCompat being mandatory for Lollipop below is valid, hence I used it in values\theme.xml.

But my question is why can't I use the Material theme for Lollipop onwards i.e. when I use Theme.Material in values-v21\theme.xml the app crashes .... Any clues about this issue?

<style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">

below the app crashes with RuntimeException On inflate I get his Binary inflate error XML

Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 18
        at android.content.res.TypedArray.getColor(TypedArray.java:401)
        at android.support.design.widget.CollapsingTextHelper.setCollapsedTextAppearance(CollapsingTextHelper.java:166)
        at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:106)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:607)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:381)
        at android.app.Activity.setContentView(Activity.java:2144)
        at com.example.trybindinglib.MainActivity.onCreate(MainActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2407)
        at android.app.ActivityThread.access$800(ActivityThread.java:149)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5321)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

Solution

  • Widgets from the Design library rely on features backported from newest API levels of Android. That includes support for authentic drawable, color and attribute resolution and using AppCompat versions of platform attributes (such as colorAccent, colorControlNormal, colorError, etc).

    If your app already uses AppCompat for platforms below API 21 you can safely use AppCompat above and including API 21 as well. Not only that, I encourage you to. It contains bugfixes and features backported from even newer API levels.

    Save yourself a headache and don't try to use both AppCompat and Material themes at the same time unless you know exactly what you're doing.

    If your app is designed with minSdkVersion 21 and you decide you absolutely don't want to use AppCompat, there is a fork of the Design library that uses only framework features.

    https://github.com/commonsguy/cwac-crossport

    It still uses portions of the support-v4 library (which is totally fine):

    This project depends upon support-annotations and three pieces of the former support-v4: support-compat, support-core-ui, and support-core-utils. All will be pulled in via transitive dependencies, and you probably already are using some or all of those dependencies anyway.