I need your assistance with the following issue in android dev.
For some reason when I extend ActioBarActivity the cut/copy/paste buttons are displayed correctly when I press the EditText component (see image 1).
Unfortunately, if I extend Activity, the toolbar is not displayed correctly. You can see on image 2, that the buttons are present but they are white and the user cannot see them. I have investigated but I'm not sure how to fix this.
If you need I can share the code but I think this can be easily reproduced.
Thanks in advance.
Paul
Try using this in the definition of your app theme:
<item name="actionModeBackground">#color/actionBarBgColor</item>
Create a custom style and specify the background to the color you want, and in your theme xml resource file, between the "style" element, set the "android:actionBarStyle" to the custom style you just created, it'll change the action bar color for you.
Updated: I just used the default folder for my theme resource (Note: the file name is arbitrary), if you want to create a theme resource for v-11 (API level 11) and up, create a new theme resource file (again, file name is arbitrary) with a "-v11" suffixed to the name of the file.
This is the default theme resource file since it inherits properties and attributes from the Theme.AppCompat.Light.DarkActionBar, as you can notice, in this file, you MUST not use the "android" namespace. If you have another theme resource file, like in the picture above, Android Studio automatically created the stylex.xml(v21) which is for Android version 21 and up devices. You MUST add the "android" namespace to each and every attribute such as the actionBarStyle would become android:actionBarStyle, but you should now that, the ActionBar has been deprecated since version 21, API level 5.
Also, the displayOptions attribute tells Android about how the title area is shown. These are availabe values:
none
useLogo
showHome
homeAsUp
showTitle
showCustom
disableHome
Note: the background attribute ONLY accepts reference value, not hard-coded value.
Here is the result
Related
Its not so long since I started to program in Android Studio. I was wondering why everything that I'm adding (like button, the actionBar...) has already a Lilac predefined color and how I can change this color. When I tried to change the color of the button in xml of my activity nothing happened.
android:background="#color/teal_200"
Can you help me please to know where I can change this default color and why the
android:background="#color/teal_200"
didn't worked?
Thank you!
These elements will likely be in an activity and that activity will have a theme, which you can check by looking at the android:theme of the activity (or the application, if the activity doesn't have one) in AndroidManifest.xml.
If you've started a new project from a template, this theme will likely be in res/values/themes.xml and it will define colours that will be used wherever that theme is applied.
Different elements take different colours from the theme and you need to check the documentation for each one to understand where their colours come from and how to change them.
For example, if you're using a contained MaterialButton, its documentation is here. There you can see that it takes its background colour from the colorPrimary of the theme by default and this can be overridden by setting the app:backgroundTint of the button. Therefore, if you wanted to change the colour of all the contained buttons in activities that use that theme, you'd need to change the colorPrimary of the theme. You could also change the colour of individual buttons by setting the app:backgroundTint of each button.
Note that several UI elements also use the colours set in the theme (like colorPrimary) and they'll change if you change those values. There's more info about what the colours in the theme are used for here, here and here and more general info about themes and styles here.
please change in colors.xml in your resource file
Open values -> colors.xml and add the color you want and set it in the layout XML file
This problem is highly related to the following question: Why are all the dialogs are using the "old" Android 2.x style?
I'm using Delphi Berlin (10.1), where this problem has been fixed already:
But, as soon as a TStyleBook is dropped to the form and assigned to it (e. g. by right clicking a component and then "edit custom style"), the dialog boxes (all of them) turn to the dark style again:
The automatically created "trimmed" style is still the "light" version ("AndroidL Light"). And yes, I checked that function "GetNativeTheme()" in FMX.Helpers.Android.pas assigns the Result, I even copied the file and added it to the project. Removing the assignment from Form1.StyleBook show the native styled dialogs again.
How can this be solved while still keep using a TStyleBook?
I found the problem: Function GetNativeTheme() was searching with TStyleManager.FindStyleDescriptor() in the assigned style for a TStyleDescription object entry, to look if one of the strings "[LIGHTSTYLE]" or "[DARKSTYLE]" is used for the target platform. In a full style file this object entry is normally present, but when starting to edit a custom style of a component inside the IDE, a TStyleBook component is created with just a small style. It cannot be seen inside the IDE, but this small style does not contain the TStyleDescription object entry, so the dark/light theme can not be found out. And by default, the GetThemeFromDescriptor() returns 0, which seems to correspond to the dark/old style.
I solved the problem by double-clicking the TStyleBook component, save the style to a file (*.style), opened the file in a text editor and simply manually added the TStyleDescription object entry. Saved and loaded back to the IDE. Now all dialogs use the correct light style theme.
Here again as text:
object TStyleDescription
StyleName = 'Description'
Author = 'Embarcadero Technologies Inc.'
AuthorURL = 'www.embarcadero.com'
PlatformTarget = '[ANDROID][ANDROIDL][LIGHTSTYLE][DEFINEFONTSTYLES]'
MobilePlatform = True
Title = 'AndroidL Light'
Version = '1.0'
end
In addition to StanE's answer:
Instead of adding a StyleDescription to all custom styles you can also modify the GetThemeFromDescriptor() function in FMX.Helpers.Android to return a default value of TJAlertDialog.JavaClass.THEME_HOLO_LIGHT.
I have an app widget with a white background. Some of the text that displays uses ?android:textColorPrimary, some uses ?android:textColorSecondary, and some use colours I've defined.
For some reason though, when I run my app on pre Nougat (24 or lower), the colours are white such that the text is invisible on the white background, but anything 24 and higher shows as black or grey. The colours I've defined are always ok.
What's also interesting is that the code in the app widget is almost identical to the actual app (both displaying a list of items) and the app version (even on these older API's) uses dark colours but the widget for some reason selects white colours for the text.
If I trace through the XML code in the styles I get to this:
<!-- The most prominent text color. -->
<attr name="textColorPrimary" format="reference|color" />
What does this mean? How does it know what the most prominent text colour is? Why is it different in the widget vs the app, and why only on older API versions?
Is there anything I can do to fix this so that it's consistent? I'm using the same theme. Why would this happen?
Note: as far as just setting a specific colour, this is not what I'm asking. Sure I could just set the text to black or something but I want to use these styles so that in the future the colour can change as necessary, especially since I'm using the day/night theme. Maybe it's related to that? Ok I tried a normal theme but the problem persists (text is invisible on the widget - only the widget - on older than API 24).
Please let me know if anything is unclear and I'll update the question. Thanks.
What does this (textColorPrimary) mean?
This means, that the value specified in the current theme's android:textColorPrimary would be applied. So, if you have declared a TextView in xml and have applied android:textColor="?android:textColorPrimary" to it, then this attribute would be fetched from the theme of the current context with which this layout is being inflated.
How does it know what the most prominent text colour is?
It is fetching that value from the theme that you have applied to your activity or from the context with which the view is being inflated (see ContextThemeWrapper and android:theme). It may differ from platform version to platform version. Depending on the theme you are using, it may differ, see themes.xml.
You can override that attribute in your theme:
<style name="AppTheme" parent="...">
...
<item name="android:textColorPrimary">#color/someColor</item>
</style>
Now, you have successfully overridden android:textColorPrimary attribute, so hereafter any view that is being inflated with a context of this theme would see this overridden value when referring to ?android:textColorPrimary.
Why is it different in the widget vs the app, and why only on older API versions?
Your widget may have been inflated with a particular theme, while app has a different theme. Had they the same theme - those attributes would be same.
Well, ?android:textColorPrimary and ?android:textColorSecondary are attributes and they are resolved by system. It's ok for them to be different on different platforms because you refer to Android attributes.
If you want to define them by yourself you need to create yourown theme and put the values there.
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.
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