0

I'm trying to test a custom layout, which extends TextInputLayout from the design library. When i run the test, i get the error

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library. at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33) at android.support.design.widget.TextInputLayout.(TextInputLayout.java:192) at android.support.design.widget.TextInputLayout.(TextInputLayout.java:185) at android.support.design.widget.TextInputLayout.(TextInputLayout.java:181)

I'm using the context provided by InstrumentationRegistry.getTargetContext() to instantiate the custom layout. How do I fix this?

Edwin Nyawoli
  • 836
  • 8
  • 20
  • Hey, maybe answers here will help you? https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity – Micer Dec 15 '17 at 09:51

1 Answers1

0

Call setTheme() on the context object providing it with an AppCompat theme before using it to instantiate the custom layout, as shown in the example test method below.

@Test
public void testCustomLayout() {
    Context context = InstrumentationRegistry.getContext();
    context.setTheme(R.style.Theme_AppCompat);
    CustomLayout textInputEditText = new CustomLayout(context);
    assertNotNull(textInputEditText);
}
Edwin Nyawoli
  • 836
  • 8
  • 20