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.
Related
I have just made my game app into a library so that I can incorporate it into a variety of "wrapper" projects, each with their own manifest file. This is so that I can easily maintain a variety of different versions e.g. free, paid, alternative markets etc.
I also wanted each wrapper to be able to affect the value of various boolean flags like include_adverts or allow_feature_x. I thought that a good way to do this (correct me if this is a dumb idea) would be for each wrapper project to have its own set of strings defined in its own strings.xml. So it could have things like:
<string name="allow_feature_x">true</string>
But now I have a problem scooping these strings from within the library.
I tried this:
boolean allow_feature_x = my_str2bool(getString(R.string.allow_feature_x));
But I get a allow_feature_x cannot be resolve error.
Can this strings.xml thing be made to work? Or was my scheme fundamentally flawed from the start?
This is exactly what I do for many of my projects.
Just make sure your library project has all the default set of strings defined in it, otherwise you won't be able to reference them, since the library does not know about the "parent".
Then override them in your "parent" projects. You only need to override the ones that are different from default, otherwise it will take the value from the library project.
Hope that makes sense.
Edit: By the way you can reference booleans like so:
<bool name="allow_feature_x">true</bool>
and then access getResources().getBoolean(R.bool.allow_feature_x);
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.
Suppose I have an Activity, for which I set the content view from a XML file.
Let's say I have a button, acceptButton, in the Java code.
acceptButton =(Button)findViewById(R.id.acceptBtn);
In the above code, the Java name and the XML id are different. Is there a "best practice" or standard for naming widgets? For example, should the XML id be called acceptButton too, instead of acceptBtn? It seems trivial, but it annoys me what other people do, when they give the Android name and XML id completely different names.
There is no recommendation from Google about naming resources in xml files. Also checking Code Style Guidelines for Contributors you find no mention about it, so the best would be taking a look in the SDK where you can see Google is not consistent in identifier naming.
Across the SDK you can find conventions as follow:
mHeaderText = (TextView) view.findViewById(R.id.headerText);
mHeaderText = (TextView) view.findViewById(R.id.header_text);
mHeaderText = (TextView) view.findViewById(R.id.HeaderText);
What I recommend is to be consistent per project. Decide for one naming convention and use it across the entire project.
It is never mandatory that Java name and XML id should be same. And I don't think that there are any such naming standards provided by Google. But it is seems very good practice if both of them are same. By keeping them same, we can easily identify the control by name only when there are more than one controls.
Other way is you can define a naming style of java name and XML id and follow same for all the controls. But it is developer or project specific.
It is never mandatory as Nirav said but having same name for id and Variable is good practice i always do this. This thing saves your time you don't need to remember different name for Id in xml and variable in code and also you just copy and paste after defining it in xml to code.
I would interpret it more colsely to Java conventions: "AcceptBtn" to point a class, "acceptBtn" to point an instance, "accept_btn" - others. "id" is more like an instance, definetely not a class. At least others. But I would prefer it as an instance.
R.id.AcceptBtn // As a class.
R.id.acceptBtn // As an instance.
R.id.accept_btn // Other cases.
Also as per Steve McConnel's "Code Complete" acceptBtn is more preferable to be written as acceptButton. It is more readable and less writable, but we read code more often than write it. So readable code saves more time.
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.
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"