Which approach is better for creating an Android alias resource? - android

When studying about Android alias resources, I encountered different approaches for creating an alias and determining its destination type.
In the first approach, as described here, we determine the destination type via a type attribute inside an <item> tag. For instance, here we declare that the alias is pointing to a layout resource:
<resources>
<item name="main" type="layout">#layout/main_twopanes</item>
</resources>
In the second approach, as described here, we determine the destination via a specific tag (instead of <item>). For instance, here we declare that the alias is pointing to a drawable resource:
<resources>
<drawable name="icon">#drawable/icon_ca</drawable>
</resources>
Or, where things get more complicated, here we set the destination to be a layout:
<merge>
<include layout="#layout/main_ltr"/>
</merge>
What is the difference between these approaches?
Are they identical, or differ in a way that I'm missing?
When should I prefer one over the other?

Related

When to use attrs.xml, when dimens.xml?

I like to integrate ads with AppLovin into my Android App.
In their documentation they say:
Declare the base banner height of 50dp in res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">50dp</dimen>
</resources>
I also have a dimens.xml present with other <dimen... value's for my app. They work from there as well.
What is the difference between attrs.xml and dimens.xml? What to use in a situation like this?
attrs.xml is a file that allows you to define custom attributes for your views in XML layout files. These attributes can be used to customize the appearance and behavior of your views, and can be accessed programmatically in your Java code.
dimens.xml on the other hand, is a file that allows you to define dimension values for use in your app. These values can be used to set the size and layout of views in your XML layout files, and can also be accessed programmatically in your Java code.
In this situation, you should use dimens.xml to define the banner_height dimension, because you will use this value to set the height of the banner ad view in your layout.
You could also use attrs.xml to define the banner_height attribute, but since you are defining a dimension value, not an attribute, it would be better to use dimens.xml instead.
It doesn't actually matter - <attr> and <dimen> are both value resources, and you can put them in any file in the res/values folder. They'll all be combined into the same set of resources, so the actual file you use is up to you! So you can do this if you want:
# res/values/whatever.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="someDimension">48dp</dimen>
<attr name="someColour" format="color"></attr>
</resources>
and it'll work exactly the same as if you used attrs.xml and dimens.xml. It's just convention to put attrs and declare-styleable things in a file called attrs.xml, all your dimension resources in dimens.xml, all your strings in strings.xml...
But you don't have to do that, it's up to you! For example, you might want certain resource strings to be stored in a different file, for organisation (maybe they shouldn't be translated). Or maybe some values are common to all configs and you want those in a single file, and you want a separate file for the qualified stuff (that goes in folders based on API level, night theme etc)

Android Studio reusable xml value

I've got this super simple question I can't find an answer to.
I want to make a variable which I could then use for multiple elements.
Something like this in strings.xml:
<resources>
<string name="textSize">20sp</string>
</resources>
...
<EditText android:textSize="#string/textSize" />
But this does not work.
I was able to accomplish what I wanted the following way in themes.xml:
<style name="textSize">
<item name="android:textSize">20sp</item>
</style>
...
<EditText android:theme="#style/textSize" />
But it seems too complicated for just a single value, is there a better way?
As suggested in the comments, this looks like a dimen property rather than a string.
Just like strings.xml, you can have another file (usually dimen.xml) with dimensions. Your case could look like this:
<resources>
<dimen name="bigText">20sp</string>
</resources>
It also allows you to have different settings for different configurations (for example, screen sizes here: How to define dimens.xml for every different screen size in android?).
You can find the documentation here:
https://developer.android.com/guide/topics/resources/more-resources?hl=en#Dimension

Create a different directory other than res/values/styles.xml to define styles

As I asked in the title, is there any way to do so?
Now, when I put all styles into one file it looks a little crowded, I would like to separate styles.
For example:
res/values/styles_for_main_screen
res/values/styles_for_set_screen
And then in main_screen layout
<TextView
style="#styles_for_main_screen/text_view_custom_style">
</TextView>
This example obviously doesn't work, but it shows what I'd like to achieve.
I read in every tutorial that we need to put our custom styles into styles.xml file, but I wonder if there is a possibility to diversify styles in few xml files?
Every question I read was something like "how to do .... in styles.xml".
I can't find question similar to mine.
Example how it should be done, thanks #Frank N. Stein for the answer:
This how looks my custom xml res/values/styles_for_main_screen
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_back">
<item name="android:background">#E81C1C</item>
<item name="android:text">whatr</item>
</style>
</resources>
and then to retrieve this style I just write:
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="#style/custom_back"
/>
so the convention looks like:
style="#(what I want to retrieve)/(name of style)"
from the android developers site
In XML: #[package:]style/style_name
You can call your style files whatever you want (if you respect the naming conventions and you put them all in the values folder/s), as you do with strings and colors.
Therefore, YES! You can have multiple ones, if so you desire.
Obviously, you will NOT specify the path to each file.
Referencing the style/s by using R.style.your_new_style is enough.
Remember that, android scans the files found int the /values directory by reading their content. For styles, every <style name="styleName" > ... </style> will be parser and a style object reference will be created.
Then, as Frank said, Yes. You can use whatever file name to write your custom styles.

How create resource id without a resource

I need to associate several tags to a view so I use
view.setTag(id, tag_object)
Unfortunately Android requires to have the id as defined in a resource. However R file is auto generated of resource ids appearing in different resource files, so I do not know how to create an id detached from any resource. As work around I just use id of some resource but it isn't robust, because if I decide to remove the resource, the id can disappear. It is also reduces readability of the code having some weird id for addressing a tag. Perhaps I missed very simple trick as ids resource file.
There is a resource type "id" that lets you define arbitrary resource IDs:
http://developer.android.com/guide/topics/resources/more-resources.html#Id
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="foo"/>
<item type="id" name="bar"/>
</resources>
Will generate R.id.foo and R.id.bar.
You can simply declare Strings in your strings.xml file and use those id's. For readability purpose give them some good names. And don't use these strings somewhere else in your code or resources.

What is the purpose of layout.xml?

Why do people use layout.xmls in their resources like:
<resources>
<item name="main" type="layout">#layout/main_twopanes</item>
</resources>
while there are folders for alternative resources to use particular XML for particular configuration?
this is called Layout Aliases link here:
To avoid this duplication of the same file for tablets and TVs (and the maintenance headache resulting from it), you can use alias files. For example, you can define the following layouts:
And add these two files:
res/values-large/layout.xml:
<resources>
<item name="main" type="layout">#layout/main_twopanes</item>
</resources>
res/values-sw600dp/layout.xml:
<resources>
<item name="main" type="layout">#layout/main_twopanes</item>
</resources>
These latter two files have identical content, but they don’t actually define the layout. They merely set up main to be an alias to main_twopanes. Since these files have large and sw600dp selectors, they are applied to tablets and TVs regardless of Android version (pre-3.2 tablets and TVs match large, and post-3.2 will match sw600dp).

Categories

Resources