In My application :
setTheme(R.style.sometheme)
does not work same when theme is set in manifest, for activity tag as below:
android:theme="#style/sometheme"
I need to set the theme dynamically, in the android application onCreate . I can't do this, since the behavior of both the implementations is different.
Any help would be greatly appreciated.
Got it..
'setTheme(R.style.sometheme)' takes some time to apply, so if you put certain delay, you could see it. Where as if you apply the theme in manifest, it is applied instantly.
Related
I recently wanted to create a setting, to enable a night mode in my android app, i searched in the internet and found a nice solution with AppCompatDelegate´s DayNight theme and a short code fragment... :
if(settingsSharedPreferences.getBoolean(getString(R.string.design_dark_design_key),false))
{
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
...to enable dark mode for my MainActivity, if it is enabled in my app settings. It worked perfectly and changed my MainActivity in "DarkMode"! But if i open another activity, this not appears in dark mode like main activity, but if i add the code lines above to this new activity it also starts with dark mode. So my question:
Do i have to call this code lines in every activity or is there a way to set global night mode for any activity in my app?
UPDATE:
I just called the code baove in a new activity before the super.onCreate() and setContentView() methods and my whole app theme changed. But if i call it in MainActivity just the Theme of my MainActivity changes... Its really strange.
Can i change whole theme also in MainActivity?
Thank you in advanced!
Ok, I've found a solution!
For anyone who has the same problem, this is the correct way to apply dark design for your whole application:
I got it from this NICE site
https://blog.iamsuleiman.com/daynight-theme-android-tutorial-example/
It's pretty easy, you just have to use:
AppCompatDelegate.setDefaultNightMode(mode);
instead of
getDelegate().setLocalNightMode(mode);
Quite easy, but really useful!!! Happy coding.
Sorry for my English, I'm German :)
I made a new Fragment and tried to set it's theme to AppCompat.NoActionBar using Android Studio's Design tool. The problem is, once I selected it, the theme did not change and stayed on the default app theme.
After Manually Setting the theme on the Android Manifest to Theme.AppCompat.NoActionBar the Fragment then accepted the Theme Change.
I want to ask the following:
Can Fragments have different Themes independent from the App Manifest or Main Activity? (Since I assume setting the Theme in the App Manifest changes it for all Activities and Fragments).
I was looking for a way to just set the Fragment's theme to AppCompat.NoActionBar but didn't get a way to do it without setting it in the App Manifest.
This is my First Question on StackOverFlow, hope it's a valid question since I couldn't find the answer online. :)
I have backend response with some bunch of colors that I need to change in my app. Is there any way to change theme attributes for activity? I understand that I can change whole theme but I don't know what colors will be used in future. So my app has to be customizable totally from the backend
You can call setTheme() for an activity before calling setContentView(). The theme will be changed just for that activity.
If you want totally customized colors (that can't be captured in a set of pre-defined themes) then you're going to have to set up your activity and its views to do that in code.
In my android app, i can change the theme, but to see the change I have to exit the app and open it again.
This is how I change the theme.
ThemeSetterActivity.setStyle(signup.this);
which happens on the create event.
But is there some code like this (below) that I can run, and will change the theme for all the activities.
foreach (activity act in app) {
ThemeSetterActivity.setStyle(act.context);
}
thanks
As far as I know you can't do it that way.
What I do is have each activity keep track of what theme it is currently using.
Then in each activities on resume just check if the current theme is still what it should be, if not restart the activity.
An easy way to get that application wide is to just have your base activity class implement that behavior, and then have all activities extend that.
Apply a theme to an Activity or application
To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the tag to include the android:theme attribute with the style name. For example:
<application android:theme="#style/CustomTheme">
If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the tag instead.
Just as Android provides other built-in resources, there are many pre-defined themes that you can use, to avoid writing them yourself. For example, you can use the Dialog theme and make your Activity appear like a dialog box:
<activity android:theme="#android:style/Theme.Dialog">
use static field and store your theme id;
and in onCreate method use :
yourActivityObj.setTheme(R.style.AppTheme);
maybe id work;
I want to have a "change theme" feature in my app. If I call setTheme() in onCreate(), there is one problem.
The moment after I launch my app, a plain white background appears for a second (because I have set light theme in the manifest). After that, the complete layout of my Activity is displayed - it is either with white or black background, according to user's theme preference.
Is there any way I can change whether white or black background appears after launch?
Make sure you call setTheme() in onCreate() BEFORE calling setContentView(). Then if you want to dynamically change the theme again later, you should simply restart your activity.
If you are adding a theme to the entire program than you could start by doing:
In your manifest you add to your application tag that you are using a theme.
<application android:theme="#style/mythemename">
Then look at Theme XML to make sure that you have what you need declared in the appropriate places.
If it is just for a particular action you could add the activity tag
<activity android:theme="#android:style/Theme.propertyname">
You can also, if you want your theme to just change the background color, follow the same pattern with either the activity or application tag (what ever one you are using) and set the item name "colorbackground" to what you want.
You can also use Theme XML and remake what you want in your current theme and call that your custom theme using the method above.
I hope this helps and if not please let me know so I might be able to help better in the future.
Another way would be to have a kind of splash screen which will check for a preference variable for example and then decide whether to use the light or the dark theme. This way you could also use XML layouts.
EDIT: Yet another way is to have the all the layout defining stuff in the onCreate() method and then just trigger the onStart() method when ready.