How to apply different theming to the single activity approach - android

I'm trying out the single activity approach together with the Navigation Component but I was wondering how to apply multiple themes to one activity.
Normally I would set a theme to an activity via the AndroidManifest.xml and all the fragment within that activity will use that same styling. Whenever I want to use another theme I would simply create another activity and apply the other styling. But since there only is one activity I would have to change the styling after the activity has been created. This is fine for styling used by Fragments. However styles for the StatusBar and NavigationBar are a lot harder because they affect the Activity.
Things I've tried:
Change the theme of the Activity after is has been created. Unfortunately these changes aren't applied directly and would require a restart of the activity (which is the one currently running).
Set styles like StatusBar and NavigationBar when creating the Fragment. This works, but it requires me to set the themes and styles using code instead of using the usual themes.xml and styles.xml.
Can anybody tell me what the intended solution is? Because of these limitations we started adding activities again to the navigation component whenever the theme needed to be changed.

Related

How to change Android App theme at runtime like Google Inbox App?

The inbox app features a navigation drawer. Upon clicking on any navigation drawer item a fragment is loaded (most probably) and during this transaction the app theme changes. Changing app theme requires setTheme() method to be called before setContentView(..) in the onCreate() method of the activity. The super fluid UI indicate the use of fragments so how is this achieved without recreating parent activity (otherwise there would have been a lag for sure).
The snooze fragment hase oragne like theme
The inbox fragment has blue like them
You can actually change the theme's style, but only before calling setContentView(#ResId int) method. Something like this perhaps?
getTheme().applyStyle(isDashUser ? R.style.redStatusBar : R.style.blackStatusBar, true);
setContentView(R.layout.my_activity);
If you look closely when changing the page there's a slight graduation between the two colours. This probably suggests they have a system separate from theming to re-colour all the UI elements.
One my apps has a very similar colour change feature, and I just have methods set up to manually re-apply the relevant colours to each UI element. Of course Google probably have some super slick way of doing it that they'll never share with anyone.

Android: Must a Fragment's Theme be the Same as the Main Activity/App Manifest?

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. :)

How to change a PreferenceFragment style/theme?

I have a PreferenceFragment and I want to change the background, text color, and a couple other things however I can't find a way to change the fragments style without affecting other fragments in my app.
I tried a solution mentioned at Applying custom theme to ActionBar in PreferenceFragment
which said to put getActivity ().setTheme (R.style.myCustomTheme); in onCreate however that affects all of my other fragments since they are using the same activity as the PreferenceFragment.
Is there a way to change only the PrefrenceFragment style without affecting the activity/other fragments?
Thanks!
EDIT: I guess I could make it a seperate activity and change the theme that way but I would really like to keep it a fragment if possible.

Change android theme runtime

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.

Multi theme support in android app

Did anyone implemented multi theme support for android app? Are there any common practices for this?
Thanks for any advice.
UPD: The main problem for now is that android's theme engine doesn't support selectors like in CSS. For example if I have two views with "background" attribute there's no way to make theme engine distinguish those ones and set different backgrounds. I can specify different style for each view but this approach lacks flexibility 'cause it's impossible to apply style for whole activity at once.
As far as I know, there is no way to set a theme to the whole application in on line of code. If you want to change the theme of an activity, you need call setTheme() in its onCreate() method, BEFORE calling setContentView. So to make it easier for you, you could do a switch on all your themes, and select one in regards of what the user has selected. Now, if you want it to apply easily to all your activities, you could make all your activities be a subclass of a custom Activity in which you would only set the theme. Activity <-- ThemeActivity <-- all your Activities

Categories

Resources