How to change a PreferenceFragment style/theme? - android

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.

Related

How to apply different theming to the single activity approach

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.

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.

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.

How to add a Cast (Chromecast) button to a ListActivity

I have a ListActivity and I would like to add a Cast button to the menu of this activity. In order to do this, the activity has to extend ActionBarActivity (instead of ListActivity) and use a different style in the AndroidManifest.xml (Them.AppCompat as opposed to AppTheme) which make the list look very different.
Also, my menu had several items, but now only the Cast button is showing up.
Any suggestions on how to maintain the ListActivity properties, but still add a Cast button to the menu?
Thank You,
Gary
A couple of ways, depending on how much flexibility you have and how much work you are willing to do:
See if you can change your activity to inherit from FragmantActivity or ActionBarActivity; then you can still have your list there but as a, say, fragment (e.g. ListFragment). If you can make such a change, Cast button can be added very easily.
If you cannot change that, then you need to have a fully custom solution; you need to add the cast icon/menu and then manage that yourself, you might want to look at this sample project.
As for the theme, in the first option above, you probably want to have your theme inherit from the AppCompat and then customize that accordingly.

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