I have an app that consists of two fragments connected with ViewPager. The one on the left has custom background, the one on the right has basic light theme (white background, black text color, etc.). Now I want to implement DayNight theme to the right fragment (not to the one that has custom background). I tried to set DayNight to the main activity in AndroidManifest, but it changed the whole app - the left fragment and the right fragment.
Manifest:
android:theme="#style/MyTheme"
Styles.xml:
<style name="MyTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
...
</style>
Is it possible to set DayNight theme to only one of them? Thank you very much for your advice.
Related
We are applying dark and light themes to an app. Our original theme is going to be light. In this situation I have a problem changing the real transition color of an activity. While switching between activities, I apply an animation using this code:
finish();
startActivity(new Intent(this, MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
This leaves a background that we set on the application theme on the manifest that goes like this:
<activity
android:name=".MainActivity"
android:theme="#style/MyStyle_Light" />
where the styles are something like this:
<style android:name="MyStyle_Light" parent="Theme.AppCompat.Light">
<!-- some style resources -->
</style>
<style android:name"MyStyle_Dark" parent="Theme.AppCompat">
<!-- some style resources -->
</style>
Now, the style Theme.AppCompat.Light has a light background on its window. It is defined within the activity from AndroidManifest.xml.
To change an activity's theme I use a code like this, and it works after seeing the original color that comes from the manifest at the very first opening.
#Override
public void onCreate()
{
setTheme(R.style.MyStyle_Dark); // can also be MyStyle_Light depending on user choice
super.onCreate();
setContentView(R.layout.MyLayout);
// rest goes on
}
The problem lies on the original color that appears after the fade-out and fade-in animation, because even if user chooses the dark theme, since MyStyle_Light is on manifest theme, they always see a light background on the fade out animation.
How can I change the background color that comes from the manifest?
Things I've tried or thought so far:
Using "android:windowBackground" which doesn't work,
Setting the theme on the application class without using a theme in manifest which also didn't work.
Using an attribute on the manifest to change styles at runtime from .xml which I don't think will work since there is nothing to refer to. (Just tried, it doesn't work.)
Using a duplicate activity with different theme resource attached to the manifest and starting it depending on user choice (seems unnecessary to me)
Any suggestion is appreciated, thanks.
I have an app in which the user can choose between the default theme and a dark theme. The manifest applies AppTheme to the <application>, while each activity checks the relevant flag and uses setTheme(AppTheme.Dark) if necessary.
This implementation works well for all my activities, but the preview window shown by the OS before the application launches uses the Manifest theme, i.e. the light one. So users get a sudden white screen before reaching the dark app.
Based on other SO answers, I gathered that the only customization we can do is to set a windowBackground drawable for the window to use (and a statusBarColor if needed).
Even though I have two different windowBackground set for each theme, I can't change the theme for the preview window since the Application class seems to load after it, and setting theme there makes no difference.
I'd like to avoid disabling the preview since it adds a noticeable delay without much indication that the app is launching. Is there any solution to this? I'm considering having a dark color for both themes in the worst case, like the Reddit app does.
Styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- primary and accent color attributes -->
<item name="android:windowBackground">#color/activityBackgroundLight</item>
</style>
<style name="AppTheme.Dark" parent="AppTheme">
<!-- primary and accent color attributes -->
<item name="android:windowBackground">#color/activityBackgroundDark</item>
</style>
Note: While I'm using colors here, I've already tried with a gradient drawable and it doesn't change anything.
UPDATE: I wasn't able to get any other method, so I went ahead and applied the dark theme by default on the SplashActivity. This means the preview window is always dark, while the check in the SplashActivity's onCreate is inverted (compared to other activities) and applies the light theme if needed.
Still looking for a solution that actually lets me switch at runtime.
have an Activity extending PreferenceActivity opening like so,
PreferenceActivity
but when i add a splash screen to the app with:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">#drawable/splash_screen_frameup</item>
<!-- Customize your theme here. -->
</style>
when i navigate to the PreferenceActivity, the toolbar remains but the whole screen is my splash screen (black screen)PreferenceActivity after apptheme background declaration. i have a feeling it is due to the PreferenceActivity not having a declared xml layout? how do i have the splash screen but not have it overlap the PreferenceActivity?
There seems to be high chance that your are using the same theme for PreferenceActivity too!
Reason: windowBackground!
Create another theme without windowBackground flag & assign that theme to PreferenceActivity...
If that doesn't work, please elaborate more. :)
I'd like to override the accentColor used by v14 PreferenceFragmentCompat.
I'm using a pink accent color for the outer frame of my Android app. This creates problems in many situations as it causes standard controls to use an accent color that's close enough to red that the effect is disturbing. Be that as it may, I like the effect of having a pink FAB and button controls on the frame.
For child activities, I use a them with the standard teal accent color. However, I have a PreferenceFragment compat in a drawer on the main activity, and I cannot figure out how to override the main activity's accent color.
Things I have tried (none of which work):
Setting a theme on the frame of the fragment that receives the PreferenceFragmentCompat (doesn't work):
<FrameLayout android:id="#+id/preferenceFragmentFrame"
android:layout_width="match_parent" android:layout_height="0dp"
android:layout_weight="1"
android:theme="#style/AppTheme.TealAccentColor"
/>
where the AppTheme.TealAccentColor style provides an explicit teal acccentColor.
Setting the accentColor in a preference theme (doesn't work):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="preferenceTheme">#style/MyPreferenceThemeOverlay</item>
</style>
<style name=MyPreferenceThemeOverlay parent="PreferenceThemeOverlay.v14.Material>
<item name="colorAccent">#color/colorAccentTeal</item>
</style>
Adding an accent color to preference-v14's PreferenceThemeOverlay (doens't work):
<!-- use the library's theme-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="preferenceTheme">PreferenceThemeOverlay.v14.Material</item>
</style>
<!-- but add an accentColor item to the library's theme -->
<style name="PreferenceThemeOverlay.v14">
<item name="colorAccent">#color/colorAccentTeal</item>
</style>
No matter what I do, PreferenceFragmentCompat seems to take the pink accent color from the Activity's theme instead.
I'm sure it has something to do with a disconnect between the Activity's theme and a Fragment's theme. But there's no xml element for the fragment, since PreferenceFragmentCompat provides its own layout.
Maybe there's a way to do it programmatically with an override in the class that extends PreferenceFragmentCompat, but if there is, I can't imagine what it would be. Most of the attack points I can think of either have access to the internally-created layout, or have access to the layout after it has been created, which is too late.
A picture might help:
Have you tried overriding android:colorAccent instead of colorAccent?
Use the method you described as "Setting the accentColor in a preference theme", just change the attribute name.
The preference support library doesn't take into consideration appcompat at all, so
forget about appcompat attributes
color* attributes will only work on API 21, I think
If you want consistent behavior you can use my library Android Support Preference which aims to connect preference and appcompat support libraries.
Then you original style with colorAccent (unprefixed) will work as expected.
I am using HoloEveryWhere Library with Holo.Theme.Sherlock.Light as the parent theme. I am also using splitActionBarWhenNarrow and so the top ActionBar and bottom ActionBar will be of Light colors. I would like to have dark color in top ActionBar, so I have changed it programmatically and it works fine.
As I have Light theme as parent and dark color in top ActionBar, the homeAsUpIndcator will be of dark color which is not visible in a dark top ActionBar.
Thus I have created a custom theme to make the homeAsUpIndcator a light one. I have succeeded in what I need using this theme :
`
<style name="AppTheme" parent="#style/Holo.Theme.Sherlock.Light">
<item name="android:homeAsUpIndicator">#drawable/abs__ic_ab_back_holo_dark</item>
<item name="homeAsUpIndicator">#drawable/abs__ic_ab_back_holo_dark</item>
</style>
`
This is working perfectly in Android 2.2 Emulator but not in ICS+ I am getting the dark bottom ActionBar and ActionPopupMenus :(
Actually I did not specify any Dark theme anywhere!
Am I missing any attributes for the custom theme ?
I suspect that you have a values-v11 folder where you also have your theme declared('AppTheme'). You can remove the style(if you don't plan to do something else with it)