Android change previous activity theme - android

I want change hole application theme and i can do it and handle this by saving in sharedPreference and set theme in my BaseActivity.
Changing theme is in separate activity, but when i go back to previous activity it has not updated theme.but if i close the application and reopen or move to unopened activity, theme changed.
I tried to set theme in onResume but nothing changed.

Theme can only be set before setContentView() is called in your Activity. You can try something like calling recreate() after updating your theme, but I would advise against that.
Here are some popular theming libraries that handle these things for you:
Aesthetic
Colorful
Magica Sakura

Related

Runtime Activity Theme Change Issue

When I remove the activity theme from its directive ([activity(Theme =)]) and set it in activity's OnCreate overrided method before base.OnCreate I expect the result be same but it does not. When I run app, first I see Android default theme then SetTheme change theme. As far as I know the OnCreate implement before an Activity is created!
Save theme state on sharedPreferences, then load it inside onCreate(). When user click change theme, you need to reload activity, by starting it again.

setTheme works but only after loading theme from manifest

I'm trying to set the theme of my Activity during runtime, choosing one of a number of themes. I want the chosen theme to display immediately on startup of the Activity.
In the <application> part of my manifest I have set a default theme with android:theme="#style/AppTheme". And then in my onCreate() I use setTheme(R.style.DarkAppTheme) to set the theme to a user-selected theme (replacing DarkAppTheme with the selected theme).
And based on research it seems that setTheme() should go before onCreate() and before setContentView(), which I do.
BUT although this works to display the Activity in the user selected theme, the Activity first loads with what looks like the default theme, and then after a short delay the correct theme loads.
If I set the user selected theme directly in the manifest, that loads immediately as I want, but of course that is hardcoded and I want to change this dynamically based on a shared preference.
How do I avoid the visible changeover? I want the user selected theme to be displayed right from the start.
Thanks.
The Activity first loads with what looks like the default theme, and then after a short delay the correct theme loads... How do I avoid the visible changeover?
There are 2 parts to an Activity 'Enter/Opening' window animation, when your app is first launched from the icon on the Home Screen:
The 'dark gray rectangle appear' animation. This is the initial blank screen that the system process draws when launching the app (source). It's also known as the "theme preview" screen or "splash screen". It can be white if your app uses a Light theme.
The 'fade in (or circular reveal) of the view layout'. This is the animation of your view layout appearing on top of the dark gray rectangle. It happens after part 1.
Part 1 is what you identified as "what looks like the default theme". You can disable this first part with the following item in your Activity/App style:
<item name="android:windowDisablePreview">true</item>
This will prevent the 'dark gray rectangle appear' animation, and only allow the 'reveal of the view layout', to therefore avoid the visible changeover or flicker. But there are caveats:
You have to ensure that your Activity launches quickly, because there will be no visual animation feedback for the user until your layout is fully loaded. That's why the theme preview is normally on by default.
It causes strange bugs on context menus: For any PopupWindow, the 'Enter' animation will no longer happen, and it will just display instantly ('Exit' animations are not affected). This also applies to system PopupWindows like the overflow menu list, and the drop-down list of an AutoCompleteTextView. This bug happens on Android 4/5/6, but not on Android 7/8. More info here.
Documentation of windowDisablePreview:
Flag allowing you to disable the splash screen for a window. The default value is false; if set to true, the system can never use the window's theme to show a splash screen preview before your actual instance is shown to the user.
Further info:
How to set a theme to whole application in code, but not in the Manifest?
Android - Disable dummy starting window of application
The theme on AndroidManifest just appears if your Activity take too long to load. You can try to tunning up Activity load and remove android:theme="#style/AppTheme" from AndroidManifest or even set a compromise between these two use.
I hope it helps you \o/

OnCreateOptionsMenu not called when Style set to a Dialog Theme

I have an activity in Android. Specifically, I am using Xamarin.Android, formerly monodroid. I have an activity. When it is running on a phone (small screen device), we want the activity to be full screen. When it runs on a tablet, we want the activity to have a "border" around similar to a dialog. I do a programmatic check to get the screen dimensions and then determine if we are on a tablet or phone. If we are on a phone, I call an activity's SetTheme method and pass in a dialog theme. I have tried several dialog themes with no difference. My menu items do not show. I track this down and my overridden OnCreateOptionsMenu method is not called. I know because I set a breakpoint in the first line of my activty's OnCreateOptionsMenu method and the breakpoint is not hit.
The code that I am using to set the theme is:
this.SetTheme (Android.Resource.Style.ThemeDialog);
I've also tried this from the Activty's theme attributes, but I am getting the same result.
I'm looking for any ideas, thoughts, whatever.
Thanks for your time.
Wally
By default the dialog theme does not have an actionbar. No reason to create menu items without it (on newer android versions).
Call RequestWindowFeature (WindowFeatures.ActionBar); in onCreate to get an actionbar in a dialog themed activity. Then OnCreateOptionsMenu will be called

How to change themes in android smoothly?

In my application i am using a splash screen for the application start up with the theme #android:style/Theme.Black.NoTitleBar.Fullscreen.
After 2 seconds the app goes on to the main activity which uses this theme: #style/Theme.Sherlock.Light.DarkActionBar.
One is in full screen, the other one is not. The transition between both is not smooth and it takes a while (~1 seconds) for the main activity to adjust to the status bar. Is there some trick to avoid this?
As far as I know, there is no api level solution for this. I suggest Using TransitionDrawable for example changing background color and/or PropertyAimation for changing view's properties like background color or text color. Then you can call setTheme() function of activity to apply your new theme. be careful that setTheme() must be called in onCreate() function before setContentView.

show pop-up from background service in android

Hi it is possible in android to show pop-up dialog from background running service?
and answer is positive than how can i do that
There are several options. You can use a theme and make an activity look and behave like a dialog (as in this question) by setting the android:theme attribute of your <activity> to #android:style/Theme.Dialog or a customized theme in your manifest.
Alternatively, you could create a translucent activity by setting the theme to #android:style/Theme.Translucent.NoTitleBar and then launch a regular dialog from the activity. As the comments on one of the answers to the question advise, in this case just make sure to finish() the translucent activity whenever the dialog is dismissed.

Categories

Resources