Issue with themes
ritvikbhawnani opened this issue · 3 comments
I have a light theme and dark theme version of my app. Unfortunately, the preference screen does not change with the current theme. I set my theme in my manifest file. Should I create a separate theme for preferences or should it work with the overall theme? Also, I don't think PreferenceFixTheme works anymore.
This is the AndroidManifest
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/BaseTheme">
This is my XML
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:key="themes_category"
android:title="Theme" />
<ColorPickerPreference
android:defaultValue="@color/colorPrimary"
android:dialogTitle="Select a Theme"
android:key="color_picker"
android:persistent="false"
android:summary="Light"
android:title="Color Picker"
app:pref_size="small" />
</PreferenceScreen>
This is the activity
public class CalculatorSettings extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
public static class CalculatorPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferencesFix(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.calculator_preferences, rootKey);
If you use the AndroidX version, you don't need the PreferenceFixTheme
anymore, just use whatever you use for the rest of the app (based on AppCompat
or MaterialComponents
, of course).
I used androidX
implementation "androidx.preference:preference:1.0.2"
implementation 'com.takisoft.preferencex:preferencex:1.1.0-alpha05'
implementation 'com.takisoft.preferencex:preferencex-colorpicker:1.1.0-alpha05'
Note: I deleted the previous comment
I apologize for the large number of comments but I think I have a better understanding of my issue. I am supposed to call getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
from both activities which I was not.
I was under the impression calling it once would cause an app-wide switch to a dark theme. Are you aware of a better way of achieving this?
The only difference in theme between both activities is the android:windowBackground attribute. Should I create a new theme just for the Settings activity with the new background or is there a way override just that attribute?
I do not fully understand the difference between setOnPreferenceChangeListener
and setOnPreferenceClickListener
. I want to update the theme of my app depending on a user click.
Lastly, cannot access setTheme() from the fragment. Can you recommend a way I can set the theme of all actvities.