In an Android application I am writing, the styling for my various TextView elements is defined in an xml resource. I would like enable the user to change certain styles such as android:typeface using a PreferenceActivity.
Is there a way to modify a style resource programmatically such that all associated widgets will update correctly? If not, must I manually select all widgets by id and change their styling?
Don't know whether you've found it but the Cubewallpaper sample uses preferences and that appears to do it all by hand in the onSharedPreferenceChanged event.
You can keep the preference keys in a resource file but that's about it it would appear.
Related
Assume that, If we not pre-defined a theme, some style, some color in XML resources files. Do we able to create our theme at runtime programmatically and use that theme afterward?
I mean:
If we just defined a default app theme, Can we change the value of theme attrs to be a value that not defined in resource xml files programmatically? (like set the colorPrimary to be #7e57c2 in some class but effect whole app) or change the reference of it to another color source like local storage or some external files.
If we store values(colorPrimary: "#7e57c2") in local storage or sharedPref and we use a custom view instead of the default. Can we get that color and use it in our custom view class?
I wondering that If I didn't define several themes in resource files but just one default theme and then I fetched the theme's values from the backend. What is the proper way to do to use that value instead of some defined value?
P.S. I'm not intending to switch between pre-defined style at runtime. I want to know how to create a new style or edit the default value and use it afterward at runtime.
Thank you.
My experience up until now when dealing with styles has been to create a style.xml file and create the properties I want for the style. If I want my style to be based on an existing style, I use the parent attribute. I then specify the style inside of my layout file on the controls that I want to apply the style to.
Where I am at a loss is when I want to use system styles and only update certain properties. I am wondering whether I can leave the layout files alone and not bother applying any styles to the controls. Instead, I would somehow update the property of the system style and that would update everywhere in my app where that style is already being used by default.
More specifically, I want to change the background color of the Actionbar but haven't found a way of doing it other than the way I described above.
You're probably looking for themes, which are collections of styles, applied either globally throughout the application, or for each Activity in particular. Start with this document and investigate further.
A style defined in XML resources cannot be "applied" at runtime.
So, given a style name, I would like to read the style items one by one, interpret them and apply them programmatically to selected widgets.
Because sometimes, a user may want to change the style of some widgets at runtime an I want that style to be exactly as the one defined in the resources.
The only reason I want to do it this way is to preserve consistency between the style defined in the XML resource and the style I appply at runtime.
How can I read the style items? I know one way: to parse the XML file myself, but maybe there are some built in methods to directly read the style I am not aware of.
I would like to set some specific system settings of android within my application.
Is it possible to get the text and maybe also a description of the settings resources by code?
For instance I would like to allow to change this setting:
android.provider.Settings.System.SCREEN_BRIGHTNESS
In the android settings it appears Brighness. This resource is localized as well, so I do not want to do the same thing within my appliacation.
I would like to create a button and insert the text dynamically, if possible.
If someone has a hint were to look at, please help.
Best Regards,
Patrick
android.provider.Settings.System.SCREEN_BRIGHTNESS In the android
settings it appears Brighness. This resource is localized as well, so
I do not want to do the same thing within my appliacation.
See this: Settings.System. If you want the value localized, use an integer resource in folders with language and/or locale qualifiers. See Integer Resource and Alternative Resources
I would like to create a button and insert the text dynamically, if
possible.
Define as much of the layout as possible in xml, calling setContentView() on the name of the xml file. The button can then be referenced after the call with (Button)findViewById() using the id as given in xml. You can then set text dynamically and modify the widget further in code. See Layouts and TextView setText, which works for Button since Button extends TextView.
Hope this helps.
I noticed the UI color (eg Button background/text color) all changes from device to device, based on the current theme that is being used in a device.
What is the best practice to apply custom UI colors for Android app, so that I have same color scheme for my app in all Android devices. I can set text/background color on a UI item. I'm wondering if there is a single place where I can define all the colors which will override the current theme applied on the phone.
thx.
Yes, there is a single place where you can define these values for your app. See Styles and Themes in the Android docs for how it works.
A style is just a mapping of values to predefined names. If you find yourself repeating a number of common attributes in your layouts, you can factor that out into a style. For example, you might have a special button style that defines a specific background and text color.
A theme is a sort of meta-style. It can be applied to an Activity or even a whole application through your AndroidManifest.xml. Among other things it defines the default styles for widgets and values that control other parts of the look and feel for your UI.
When you're trying to blend in with the system in an otherwise custom UI for your app, you can query the current theme for values. Just like you use the # reference syntax #android:drawable/foo when referring to a system resource, you can use the syntax ?android:attr/foo when you want to use the value stored in the system theme attribute foo.
In your case, if you want to change the primary text color across your app, apply a custom theme that sets the attribute textColorPrimary. If you just want to be sure that an element of your app is using the primary text color as defined by the device your app is running on, you can set android:textColor="?android:attr/textColorPrimary". The same principles apply elsewhere as well.
If you want to see what attributes are used in the system, they are defined as part of the Android framework in this file: frameworks/base/core/res/res/values/attrs.xml. Look at the children of the XML element <declare-styleable name="Theme"> at the top. To see examples of what the system sets these to, see themes.xml in the same directory. Finally, not all of these attributes are public - non-public attributes cannot be set by an app, they're implementation details of the Android framework. See public.xml for the complete list of which attributes are available for use in apps.
Best practice is to apply a custom theme to your application, and override as much of the default properties as you need.
Almost everything can be changed, except
The Menu
Some properties of AlertDialog (these can be changed using a custom dialog)
OS provided views such as the Quick Search Bar (QSB)
If you like the look of the default SDK resources then you can find these in sdk_folder/platforms/android-9/data/res/ (replace 9 with the SDK version you want the resources from) - copy the ones you want into your App and reference those.
You can take a look at the theme the SDK uses:
themes.xml
styles.xml