Search code examples
androidandroid-themeandroid-styles

TextView with custom color attribute not renders in Layout preview


TextView with custom color attribute not renders in Layout preview.

I have defined attributes attr.xml inside values folder.

<attr name="secondary_text_color" format="color"/> 

And override this color in style xml for dark and light themes.

Inside TextView:

android:textColor="?attr/secondary_text_color"

Layout preview show error, however the app work as expected.

java.lang.NullPointerException at android.widget.TextView.updateTextColors(TextView.java:5773) at android.widget.TextView.setHintTextColor(TextView.java:5047) at android.widget.TextView.applyTextAppearance(TextView.java:4053) at android.widget.TextView.(TextView.java:1604) at android.widget.TextView.(TextView.java:968) at android.widget.TextView.(TextView.java:964) at sun.reflect.GeneratedConstructorAccessor273.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at android.view.LayoutInflater.createView(LayoutInflater.java:854) at android.view.LayoutInflater.createView(LayoutInflater.java:776) at android.view.BridgeInflater.onCreateView(BridgeInflater.java:129) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:930) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:950) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004) at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:308) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1123) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084) at android.view.LayoutInflater.inflate(LayoutInflater.java:682) at android.view.LayoutInflater.inflate(LayoutInflater.java:501) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:353) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:404) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:713) at com.android.tools.idea.rendering.RenderTask.lambda$inflate$6(RenderTask.java:844) at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)


Solution

  • You should change the default theme to desired them, in this case light or dark theme, in layout preview control panel. enter image description here

    Or, set app theme to your desired default theme in manifest application tag:

    android:theme="@style/MyTheme"