I want to make a dark mode for my app, my only problem is that i cant change the text color in a fragment, the user is able to turn on/off the dark mode. That's why I can't change the theme in the manifest any solution for this problem?
if(!DarkTheme()) {
setTheme(R.style.AppTheme);
} else {
setTheme(R.style.DarkAppTheme);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.blacklight, null)));
View fragment = (View) findViewById(R.id.settings_fragment);
fragment.setBackgroundColor(getResources().getColor(R.color.Dark, null));
}
That is to detect if dark mode is switched on, the background color changes and all, but I don't know how to change the text color in the fragment.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<PreferenceCategory
app:iconSpaceReserved="false"
android:key="PREFS"
android:title="Main settings (BETA)">
<SwitchPreference
android:key="darktheme"
android:id="@+id/DarkTheme"
app:iconSpaceReserved="false"
android:title="Dark Theme"
android:summary="Enable dark theme (BETA)"
android:defaultValue="false" />
</PreferenceCategory>
<PreferenceCategory
app:iconSpaceReserved="false"
android:key="DOWN"
android:title="Download settings">
<CheckBoxPreference
android:id="@+id/MobileData"
android:key="mobiledata"
android:title="Mobiele data"
android:icon="@drawable/ic_network_cell_blue_24dp"
android:summary="Download de voicemails op mobiele data"
android:defaultValue="true"/>
</PreferenceCategory>
<PreferenceCategory
app:iconSpaceReserved="false"
android:key="Other"
android:title="Other">
<Preference android:title="Boodschap opnemen"
android:id="@+id/Boodschap"
android:key="@string/setVoicemail"
android:summary="Spreek je eigen voicemail boodschap in">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.tismi.voozzle_main"
android:targetClass="com.tismi.voozzle_main.Popup" />
</Preference>
<Preference android:title="App uitleg"
android:id="@+id/AppUitleg"
android:key="AppUitleg"
android:summary="Loop de uitleg van de app nog een keer door">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.tismi.voozzle_main"
android:targetClass="com.tismi.voozzle_main.Reopen_Expanation" />
</Preference>
<Preference android:title="Voozzle uitschakelen"
android:key="voozzle_off"
android:id="@+id/VoozzleOff"
android:summary="Door op deze knop te klikken schakel je voozzle uit">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.tismi.voozzle_main"
android:targetClass="com.tismi.voozzle_main.tasks.TurnoffTask" />
</Preference>
</PreferenceCategory>
</PreferenceScreen>
This is my setting page (the fragment), I want to change the color of the titles and the summaries.
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryLight</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/white</item>
<item name="panelBackground">@color/white</item>
</style>
<style name="DarkAppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/Dark</item>
<item name="colorPrimaryDark">@color/Dark</item>
<item name="colorAccent">@color/white</item>
<item name="android:windowBackground">@color/Dark</item>
<item name="panelBackground">@color/Dark</item>
<item name="android:textColor">@color/white</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="android:textColorSecondary">@color/white</item>
<item name="android:textColorTertiary">@color/white</item>
</style>
Try to use ContextThemeWrapper
to set the theme.
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.PreferenceScreen);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
Within styles.xml define your theme.
<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
<item name="android:textColor">@color/white</item>
</style>
Okay i figured it out and it's very simple
getContext().getTheme().applyStyle(R.style.PreferenceScreen, true);
This just apply's the style to the activity, thanks for the help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.