Where are admob resource identifiers defined? - android

In the old versions of admob SDK, you had to manually add a attrs.xml file where the admob attributes where defined (adUnitId,adSize etc..) But it is not necessary anymore in the current version of the SDK. The only thing you need to do in your XML layout file is to declare the admob namespace with xmlns.
So my question is: how did they manage to do that? I'm just curious about how they declared or defined these attributes; and I could actually use this "trick" to use with my customs views that have their own attributes in the future.
Thanks :)

At a high level, AdMob is calling AttributeSet.getAttributeValue(String namespace, String name) and similar methods. It passes in the namespace ("http://schemas.android.com/apk/lib/com.google.ads") that it expects. This doesn't need to be a valid url - it should just be a unique one that doesn't conflict with other namespaces.

Related

Why does the designer use "#+id" instead of "#id" for constraints?

I've always understood #+id to indicate the creation of a new ID (generally used with android:id), and #id to reference it elsewhere, as explained at length in a popular question here and in the official documentation:
The at-symbol (#) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace...
That this is not enforced is a bit odd; you can just use #+id everywhere, multiple times with the same id, and it does not seem to be an error or problem (unless you use the same id with multiple android:id declarations).
When using the graphical designer in Android Studio with constraint layout (it could in fact be with everything, I have not checked), whenever a constraint is added it will declare them like this:
app:layout_constraintTop_toBottomOf="#+id/rlistfrag"
Where rlistfrag is assigned to another element with android:id in the same file -- if it didn't exist already, the designed could not have created the constraint, so there can be no contextual ambiguity.1 According to the docs this amounts to twice declaring "a new resource name that must be created and added to our resources".
It seems the semantics here are not, by omission at least, exactly as described in the docs. Why does the designer do this and what are those omitted semantics?
Or could there? What all this implies to me is that elements may be processed in any order so the point is whenever an id is first encountered it will be created, even if it is not associated with an existing element.
There's a bit in the doc guide on layout resources that confirms the id will only be set the first time:
The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist.
Which need not mean the elements aren't processed in order.
It is applied in all layout components. whenever you want to mention "id" you have to use "#+id"

amazon ads xml layout

I am trying to include the ads by making them part of my xml layout, I noticed on this quickstart guide: https://developer.amazon.com/sdk/mobileads/quick-start.html it says that you should have the namespace as "xmlns:Amazon="http://schemas.android.com/apk/res/"". However when I tried this intelij says that the namepace is never used and the ad never shows up in the layout. I looked in the sample provided in the sdk and for their namespace they didn't use their package name but instead: "xmlns:Amazon="http://schemas.android.com/apk/lib/com.amazon.device.ads"". So I am wondering what the correct namepace is that I should be using
I noticed on this quickstart guide it says that you should have the namespace as "xmlns:Amazon="http://schemas.android.com/apk/res/""
I looked in the sample provided in the sdk and for their namespace they didn't use their package name but instead: "xmlns:Amazon="http://schemas.android.com/apk/lib/com.amazon.device.ads""
That first statement is not correct. In the quickstart guide it actually says that the namespace should be:
xmlns:Amazon="http://schemas.android.com/apk/res/<type your package name>"
Note the last part. That's important, because that's exactly the difference you're basing this question on.
As with any custom view on Android, if you want to use one or more attributes that aren't covered by the Android namespace, you have to declare their location first. Normally, that means you copy the line above and append the package name of your project. The Amazon prefix can be pretty much anything you like, but it makes sense to give the namespace a name that is related to its definition.
In this case, declaring the Amazon namespace, enables the usage of the Amazon:adSize attribute in the layout file. Without that declaration, your IDE will not know where to look for the adSize attribute.
That being said, if you don't use any custom attributes, then there is no point in declaring the namespace, as you won't be using it. That's basically what IntelliJ is telling you. In other words: if you're not using Amazon:adSize in your layout, you can safely remove the Amazon namespace declaration.

what does the "#+android:id/title" mean?

In normal, we should use #+id/ to define an id and use #id to reference an id. Today I found #+android:id/title in apps/settings/res/layout/preferenc_progress.xml.
How to understand it and how to use it?
It is used for resources that are shipped with the SDK.
You can take a look at them by browsing to
[PATH TO ANDROID SDK]/platforms/android-[VERSION]/data/res
By using the android in android.R.whatever you just specify the R file to look up. For more information you should read Accessing Platform Resources.
That belongs to the app preferences activity screen definition.
title and summary are standard Android fields of a TextView preference item.
I think it does the same thing. It's just a more formal way of saying it by specifying where the namespace is.
I've never met this way of giving id, but in theory this means adding new id title to android package. So you'll be able to use it in your code like android.R.id.title. But I'm not sure resource compiler will really create any id in android package. I think it can be used only with predefined ids. But I'll give you more precise answer later, when I'll be able to check it.
EDIT: I've checked it and found some differences. Firstly, if you define Android's id using #+android:id/some_id, which is already present in SDK, this id will not be defined in your R.java file. If it's not present in SDK, it will be defined in R.java, but with different kind of value. Secondly, if you'll try to convert id from its string representation to int value, Resources.getIdentifier() method will return 0 in case of #+android:id format.
This means it will create an id in your resource file.

Difference between "#id/" and "#+id/" in Android

What is the diffirence between the #id/ and #+id/?
In #+id/ the plus symbol + instructs to create a new resource name and add in to the R.java file but what about #id/? From the documentation of ID: when referencing an Android resource ID, you do not need the plus symbol, but must add the android package namespace, like so:
android:id="#android:id/list"
But in the image below Eclipse doesn't suggest any kind of #android:id/.
Are #id/ and #android:id/ the same?
you refer to Android resources , which are already defined in Android system, with #android:id/.. while to access resources that you have defined/created in your project, you use #id/..
More Info
As per your clarifications in the chat, you said you have a problem like this :
If we use android:id="#id/layout_item_id" it doesn't work. Instead #+id/ works so what's the difference here? And that was my original question.
Well, it depends on the context, when you're using the XML attribute of android:id, then you're specifying a new id, and are instructing the parser (or call it the builder) to create a new entry in R.java, thus you have to include a + sign.
While in the other case, like android:layout_below="#id/myTextView" , you're referring to an id that has already been created, so parser links this to the already created id in R.java.
More Info Again
As you said in your chat, note that android:layout_below="#id/myTextView" won't recognize an element with id myTextViewif it is written after the element you're using it in.
the + sign is a short cut to add the id to your list of resource ids. Otherwise you need to have them in a xml file like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="my_logo" type="id"/>
</resources>
In Short
android:id="#+id/my_button"
+id Plus sign tells android to add or create a new id in Resources.
while
android:layout_below="#id/my_button"
it just help to refer the already generated id..
Its very simple:
"#+..." - create new
"#..." - link on existing
Source: https://developer.android.com/guide/topics/resources/layout-resource.html#idvalue
The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's R.java file that refers to the EditText element. With the resource ID declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.
From: https://developer.android.com/training/basics/firstapp/building-ui.html
From the Developer Guide:
android:id="#+id/my_button"
The at-symbol (#) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:
android:id="#android:id/empty"
There's a bug with Eclipse where sometimes if you just created a new #+id/.., it won't be added immediately to the R.java file, even after clean-building the project. The solution is to restart Eclipse.
This I think should be solved as soon as possible, because it may (and from experience, will) confuse some developers into thinking that there's something wrong with their syntax, and try to debug it even if there's really nothing to debug.
Android uses some files called resources where values are stored for the XML files.
Now when you use #id/ for an XML object, It is trying to refer to an id which is already registered in the values files. On the other hand, when you use #+id/ it registers a new id in the values files as implied by the '+' symbol.
Hope this helps :).
#id/ and #android:id/ is not the same.
#id/ referencing ID in your application, #android:id/ referencing an item in Android platform.
Eclipse is wrong.
Difference between #+id and #id is:
#+id is used to create an id for a view in R.java file.
#id is used to refer the id created for the view in R.java file.
We use #+id with android:id="", but what if the id is not created and we are referring it before getting created(Forward Referencing).
In that case, we have use #+id to create id and while defining the view we have to refer it.
Please refer the below code:
<RelativeLayout>
<TextView
android:id="#+id/dates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/spinner" />
<Spinner
android:id="#id/spinner"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="#id/dates"
android:layout_alignParentRight="true" />
</RelativeLayout>
In the above code,id for Spinner #+id/spinner is created in other view and while defining the spinner we are referring the id created above.
So, we have to create the id if we are using the view before the view has been created.
If the view item performs the same operation, you can use the #+id for each entry in any layout because during the compilation of multiple #+id/foo the R.java file only creates one enumeration. So for example, if I have a save button on each page that performs the same operation, I use android:id="#+id/button_save" in each layout. The R.java file only has one entry for the button_save.
Difference between “#+id/” and “#id/” in Android
The first one is used for to create the ID of the particular ui component and the another one is used for to refer the particular component

android:id what is the plus sign for

The document says "#[+]id/myid" as the definition for android:id, from the notation it indicates that the plus is optional. But nowhere can I find a definition of what the plus sign means or not supplying it would mean.
What does it mean and why is it there?
#+id/foo means you are creating an id named foo in the namespace of
your application. You can refer to it using #id/foo. #android:id/foo
means you are referring to an id defined in the android namespace.
This namespace is the namespace of the framework. In this case, you
need to use #android:id/list and #android:id/empty because these are
the id the framework expects to find (the framework knows only about
the ids in the android namespace.)
Taken from one of the "Android Developer | Google Groups" discussions.
The documentation says,
The plus-symbol (+) means that this is
a new resource name that must be
created and added to our resources (in
the R.java file). There are a number
of other ID resources that are offered
by the Android framework. When
referencing an Android resource ID,
you do not need the plus-symbol, but
must add the android package
namespace, like so:
android:id="#android:id/empty"

Categories

Resources