Reputation: 1454
My question is as follows: I have some layouts that are by default light themed meaning that they are with a white background. My Ripple effects look fine and my Widgets look fine.
The issue arises when I use my dark theme or black theme. My Widgets become less visible as does my Ripple which is supposed to be white for darker themes. The Ripple looks like this using the dark theme.
On the dark theme, the Ripple is only partly visible since the color of the background is #303030, lighter than the ripple which is #000000. Ideally, the Ripple should be white. On the black themed Layout, the ripple is invisible since both the background and the ripple are black. I have actually fixed this issue by using
android:theme="ThemeOverlay.AppCompat.Dark"
in my Layout. If I use this the ripple is white and the Widgets are also more visible. The issue is that when I click on a widget like the first switch, the app crashes and leaves me with a nonsensical error.
EDIT: The error is as follows:
12-29 23:57:39.801 13055-13055/com.company.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.app, PID: 13055
java.lang.IllegalArgumentException: Expected receiver of type com.company.app.SettingsActivity, but got android.view.ContextThemeWrapper
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4453)
at android.view.View.performClick(View.java:5204)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
How can I theme my Widgets and my Ripple in such a way to display them properly (as described above) on darker themed layouts without error?
Thanks!
Upvotes: 1
Views: 447
Reputation: 11
I had this problem with my AppCompat subclassed activity. I searched diligently and found the following issue posted in Android's issue tracker: https://code.google.com/p/android/issues/detail?id=174871
There seems to be some issues recently with AppCompat's inflater, it appears that OnClick handlers generated from OnClick attributes in layout xml are not handling ContextWrappers properly. The work-around is to remove the OnClick attributes from your xml and register OnClick listeners programmatically in your code. I went around diligently registering all my click events explicitly in the code and removing all the onClick attributes from the widgets in my layout's xml file. That did fix it.
Today I discovered for my own application which uses AppCompat DayNight themes that a much easier fix was to remove "android:theme="@style/Theme.AppCompat.DayNight" from the root layout. I can now go back to using onClick attributes in the widget's xml. I don't know whether other AppCompat themes cause the same problem, but would not be surprised. My main activity sets the app's theme using AppCompatDelegate.setDefaultNightMode. Hope this helps, I was beside myself when I ran into this, my code worked great until I tested in Marshmallow, and there's scant help to be found as you can see with your question going unanswered for 6 months.
Upvotes: 1