Avoid using Resource IDs in const fields - AGP 8.0 - android

I've been getting this warning recently:
Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them in const fields
This applies mostly to custom views, where I have declared drawables or colors that might be needed.
What would be the alternative for declaring these fields?

Related

What makes buttons purple in Android studio? Plus other odd behaviours for a newbie

In web development, I see how tags have default themes defined in the browser, and I see how they're applied.
However, with Android Studio's themes, I'm really confused. I can define my custom themes using ?attr/myClass, apply it on widgets by android:theme="?attr/yClass", and then assign a colour to that attribute in my day or night theme files.
But, what baffles me, is—that purple. Where does it come from? When I set the theme header to
<style name="Theme.TestingThemes" parent="Theme.MaterialComponents.DayNight.NoActionBar">
purples come for the not night mode.
When I use other styles that come with "default" with Android Studio, I don't see exactly that purple.
Some themes allow me to set my own colours, but some others don't, like the one that I mentioned.
Worst of all that totally blows my mind is: when I open the theme files in app/res/values/themes/* and app/res/values/colors.xml*, I only see less than 10 themes defined. Yet I see Android Studio suggesting to me a long list of colour names! Where do these come from?
I only use Vim as my text editor. I'm drowning in Android studio. It's cool and powerful, but I require some baby-walking assistance.
A default new project created in Android Studio has a colors.xml resource provided in the project (res/values/colors.xml), where the purple_500 and purple_700 you described are defined.
Any other colors and themes you see that aren't in your own project's files are in the AppCompat and Material Components libraries (defined as project dependencies in default new projects), or they're built into Android itself.
In the Projects panel on the left in Android Studio, if you expand External Libraries, you can see all the code libraries that are imported for your project as dependencies (these are defined in app/build.gradle and downloaded from the Web automatically). Among these dependencies are AppCompat and possibly the Material Components libraries, with their own provided resources within.
You can't modify the contents of the libraries. You're intended to customize by extending (making child styles and themes).
If you want to see where a reference is defined in Android Studio (in XML or other languages like Kotlin and Java), you can Ctrl+Click and it will jump to the line that defines it in whatever file it's in.
I've been developing with Android for a year and have honestly never bothered using material buttons.
You can create your own drawable file for the background of the button and then add that drawable to the back of a regular button in a layout. Don't let things like this frustrate you; there are so many ways of achieving the same outcome in Android :D

How to set ListPreference defaultValue in xml based on OS version

I'm providing a ListPreference that has a different set of entryValues and entries based on the version of Android on the device. That's easy: the same string-array is defined in values/res.xml then overridden in values-v29/res.xml.
But I want the defaultValue on v29 and above to be a string that's only available on v29 and above. (The default for older versions is available on all; it's just not the default on newer devices.) How do I specify a defaultValue in a way that can be overridden by a resource in an .xml file in values-v29/? I basically need to add a level of indirection, but can't figure out how Android supports that.
I know I can set the default programmatically based on Build.VERSION.SDK_INT, but that's gross. Much better to keep everything in xml if possible.
Thanks,
--Eric House

How to add font Family attribute under Attributes in the graphical layout android studio

I use android studio to create an android app. I use as an OS Ubuntu 14.04.
In my app, I want to change the font, I follow the following step:
Right-click in the res folder and go to New > Android resource directory.
In the Resource type list, I select font, and then I click OK
I add my font files in the font folder
After that, in the layout xml I set the fontFamily attribute to the font file that I want to access as the following in textView component android:fontFamily="#font/cairo_extralight"
After that, the font doesn't change because we should change it also in the attributes of the graphical layout.But, when I go to the attributes in the graphical layout, I didn't found the fontfamily attribute under textAppearance property.
I want to know why the fontfamily attribute does not exist in the list of proprety and how can I add it.
Some common problems you should check:
android:fontFamily only works from API version 16. On Android versions below, the property will have no effect.
Support for using android:fontFamily with your own fonts was added in Support Library v26 and Android Studio 3.0 (together with Android Gradle Plugin version 3.0). If you are using older versions of any of these, it will not work.
If you have a custom TextView, make sure you extend AppCompatTextView. Otherwise no custom fonts will be used.
When using custom views, make sure that the attributes, theme, and style are correctly propagated (when overriding constructors for example).
Not all widgets support android:FontFamily. TextView, EditText, Button, and a few others do, but Checkbox and Switch don't. There you need to use setTypeFace in code instead.

Namespaces: how to know how and when to use them? i.e.: xmlns:andorid, xmlns:app, xmlns:tools

What is xmlns:android, xmlns:app, xmlns:tools and what's the basic difference between them? and When I should use it?
For android & app namespaces, use this link.
it is used for all attributes defined in your app, whether by your
code or by libraries you import, effectively making a single global
namespace for custom attributes - i.e., attributes not defined by the
android system.
In this case, the appcompat-v7 library uses custom attributes
mirroring the android: namespace ones to support prior versions of
android (for example: android:showAsAction was only added in API11,
but app:showAsAction (being provided as part of your application)
works on all API levels your app does) - obviously using the
android:showAsAction wouldn't work on API levels where that attribute
is not defined.
About tools;
Android Studio supports a variety of XML attributes in the tools
namespace that enable design-time features (such as which layout to
show in a fragment) or compile-time behaviors (such as which shrinking
mode to apply to your XML resources). When you build your app, the
build tools remove these attributes so there is no effect on your APK
size or runtime behavior.
There are also another usages of tools namespace which you can see in the strings or etc like:
<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>
Which helps to ignore errors.
Also, there is another which helps to define TargetedAPI:
tools:targetApi
And so much more.
Read: https://developer.android.com/studio/write/tool-attributes

When would I use android.R over my local R.?

If I want to set content view then I use:
setContentView(R.layout.test);
But when I set a theme, why do I have to add android to the beginning of R.style.x?
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.test);
The android API define a huge amount of standard resources like theme, drawable, attributes, ...
When you need to refer to one of those resources : you must use android.R
android.R is a reference to the Android R.java file. It is not custom to your app. It contains a reference to every style, resource, anything that you would ever use in an Android app.
On the other hand, R.x (without the android prefix) refers to your local app. It contains specific styles, resources, etc. that have been defined for your app, and is much more specific.
So as to your question of which to use:
Use android.R if you want to reference a standard style. This would be something like android.R.style.holo. It could also be animations, but keep in mind these are not custom, these are the building blocks.
Use R.x when you are declaring something local. This will be something that you have definded, such as R.style.MyTheme or R.layout.MyLayout.
You are not forced to use android.R.style.Theme_Light you can create your theme and use R.style.myTheme.
You use android.R.* when you want to use a layout/image/attribute/etc. already built inside Android without the need to rewrite it again.
You can use android.R.* like R.x everywhere you want (they are the same thing, the difference that R.x is build with your application while android.R is already here). You can remake things, but why do more work when Google already did it for you?

Categories

Resources