Android admob xml issue - android

I have a simple problem with admob here...
I've tried all resources to add admob on my android app
but somehow, when i try to add this code to the layout xml:
<Linearlayout
......./>
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
/>
it says, The prefix "myapp" for attribute "myapp:backgroundColor" associated with an element type "com.admob.android.ads.AdView" is not bound
what's wrong?
I've add the attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.admob.android.ads.AdView">
<attr name="backgroundColor" format="color" />
<attr name="primaryTextColor" format="color" />
<attr name="secondaryTextColor" format="color" />
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" />
</declare-styleable>
</resources>
and also the xmlns:myapp="http://schemas.android.com/apk/res/com.suit.AdmobTest" in my manifest...
THanks!

You must add the myapp namespace to the root element of your layout file. In you case to the LinearLayout element. The element then has to namespace references. The standard Android reference xmlns:android="http://schemas.android.com/apk/res/android" and your myapp reference xmlns:myapp="http://schemas.android.com/apk/res/com.suit.AdmobTest".
This should do the job.

Related

how to use custom attributes that a library has?

i added a project library to my project, it has some custom attributes defined in its attrs.xml how can i use these attributes ?
Library code:
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NumberProgressBar">
<attr name="progress" format="integer"/>
<attr name="max" format="integer"/>
<attr name="progress_unreached_color" format="color"/>
<attr name="progress_reached_color" format="color"/>
<attr name="progress_reached_bar_height" format="dimension"/>
<attr name="progress_unreached_bar_height" format="dimension"/>
<attr name="progress_text_size" format="dimension"/>
<attr name="progress_text_color" format="color"/>
<attr name="progress_text_offset" format="dimension"/>
<attr name="progress_text_visibility" format="enum">
<enum name="visible" value="0"/>
<enum name="invisible" value="1"/>
</attr>
</declare-styleable>
<declare-styleable name="Themes">
<attr name="numberProgressBarStyle" format="reference"/>
</declare-styleable>
</resources>
My xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/lessens_listview_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:drawableLeft="#drawable/bbb"
android:drawablePadding="5dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="26dp" />
<com.daimajia.numberprogressbar.NumberProgressBar
android:id="#+id/number_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/lessens_listview_textview"
android:layout_marginLeft="12dp"
custom:progress_reached_bar_height="5dp"/>
</RelativeLayout>
here i get error in custom:progress_reached_bar_height="5dp"
as the library's Readme says i can use attributes like this but i cant.
library Readme:
<com.daimajia.numberprogressbar.NumberProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:progress_unreached_color="#CCCCCC"
custom:progress_reached_color="#3498DB"
custom:progress_unreached_bar_height="0.75dp"
custom:progress_reached_bar_height="1.5dp"
custom:progress_text_size="10sp"
custom:progress_text_color="#3498DB"
custom:progress_text_offset="1dp"
custom:progress_text_visibility="visible"
custom:max="100"
custom:progress="80"
/>
Depends how you declare the namespace.
In your case you have declared:
xmlns:widget="http://schemas.android.com/apk/res-auto"
that means you can call the attributes using widget as prefix:
widget:progress_reached_bar_height="5dp"
If you want to use the custom prefix change the declaration in this way:
xmlns:custom="http://schemas.android.com/apk/res-auto"

"Attribute "title" has already been defined" after adding after adding android-support-v7-appcompat

I have been banging my head on this all day long, but it seems I just cannot figure it out...
OK, so I add a project that was all fine and working.
In order to "rejuvinate" it I decided to add action bar with the appcompat library (as described here.
Since I have added the library I cannot compile the project anymore and I keep getting the following error :
C:\Users\Me\Documents\Dev\MyProject\res\values\widget_attributes.xml:9: error: Attribute "title" has already been defined
The widget_attrributes.xml looks like this :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="display" format="string" />
<attr name="lineWidth" format="dimension" />
<attr name="lineColor" format="color|reference" />
<attr name="fillColor" format="color|reference" />
<declare-styleable name="DigitalDisplay">
<attr name="title" format="string" />
<attr name="unit" format="string" />
<attr name="display" />
<attr name="valueFormat" format="string" />
<attr name="showUnitInValue" format="boolean" />
<attr name="showUnitInTitle" format="boolean" />
<attr name="valueIsTime" format="boolean" />
<attr name="titleTxtAppearance" format = "reference" />
<attr name="valueTxtAppearance" format = "reference" />
<attr name="unitTxtAppearance" format = "reference" />
</declare-styleable>
.../...
</resources>
and I use the notation below when using my own widgets :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:droidfa="http://schemas.android.com/apk/res/com.droidfa"
android:layout_width="match_parent"
android:layout_height="match_parent" >
I must be doing something wrong, but I cannot figure out what...
Any help or hint will be much appreciated.
Thanks in advance,
JM
Ok, in case someone reads this one day, the only way I could get out of there was by renaming my own attribute...

admob error due to attrs.xml in android

i tried using Admob in my app by running it in a device and it showed nothing and i found the followig error in logcat
04-07 10:49:05.570: ERROR/Ads(1084): AdView missing required XML attribute adUnitId.
in attrs.xml file i have the following lines
<?xml version="1.0" encoding="utf-8"?>
<declare-styleable name="com.google.ads.AdView">
<attr name="backgroundColor" format="color" />
<attr name="primaryTextColor" format="color" />
<attr name="secondaryTextColor" format="color" />
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" />
</declare-styleable>
What is my error...
I have totally 4 Activities in my App and i am trying to add the admob in my 4th activity. whether i have to add the above said lines with that activity or anything else pls help me friends...
Should be ( with '<'resources'>' ):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.admob.android.ads.AdView">
<attr name="backgroundColor" format="color" />
<attr name="primaryTextColor" format="color" />
<attr name="secondaryTextColor" format="color" />
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" />
</declare-styleable>
</resources>
i followed as said here and it get worked.
(long time before i asked this ques, i dint get success with any of the answer and i left it, when i tried now it works...)

Same-named attributes in attrs.xml for custom view

I'm writing a few custom views which share some same-named attributes. In their respective <declare-styleable> section in attrs.xml I'd like to use the same names for attributes:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView1">
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
...
</declare-styleable>
<declare-styleable name="MyView2">
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
...
</declare-styleable>
</resources>
I'm getting an error saying that myattr1 and myattr2 are already defined. I found that I should omit the format attribute for myattr1 and myattr2 in MyView2, but if I do that, I obtain the following error in the console:
[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute
Is there a way I could accomplish this, maybe some sort of namespacing (just guessing)?
Solution: Simply extract common attributes from both views and add them directly as children of the <resources> node:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
<declare-styleable name="MyView1">
<attr name="myattr1" />
<attr name="myattr2" />
...
</declare-styleable>
<declare-styleable name="MyView2">
<attr name="myattr1" />
<attr name="myattr2" />
...
</declare-styleable>
</resources>
I am posting this answer as the above-posted solution didn't work out for me in Android Studio. I need to share my custom attributes among my custom views so I tried the above solution in Android Studio but had no luck. So I experiment and go a way to do it. Hope it might help someone looking for the same problem.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- parent styleable -->
<declare-styleable name="MyView">
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
</declare-styleable>
<!-- inheriting parent styleable -->
<!-- also note "myBackgroundColor" belongs to child styleable"MyView1"-->
<declare-styleable name="MyView1" parent="MyView">
<attr name="myattr1" />
<attr name="myattr2" />
<attr name="myBackgroundColor" format="color"/>
</declare-styleable>
<!-- inheriting parent styleable -->
<!-- same way here "myfonnt" belongs to child styelable "MyView2" -->
<declare-styleable name="MyView2" parent="MyView">
<attr name="myattr1" />
<attr name="myattr2" />
<attr name="myfont" format="string"/>
...
</declare-styleable>
</resources>
This works for me completely.
We need to make a Parent styleable and then we need to inherit that parent styleable. For example, as I have done above :
Parent styleable name MyView and inherited this to my other styleable like MyView1 and MyView2 respectively.
As Priya Singhal answered, Android Studio requires the common attribute names to be defined within their own style name. They can't be at the root any more.
However, there are a couple other things to note (which is why I am also adding an answer):
The common styles don't need to be named the same thing as a view. (Thanks to this answer for pointing that out.)
You don't need to use inheritance with a parent.
Example
Here is what I did in a recent project that has two custom views that both share the same attributes. As long as the custom views still have the names for the attributes and don't include a format, I can still access them as normal from code.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- common attributes to all custom text based views -->
<declare-styleable name="TextAttributes">
<attr name="text" format="string"/>
<attr name="textSize" format="dimension"/>
<attr name="textColor" format="color"/>
<attr name="gravity">
<flag name="top" value="48" />
<flag name="center" value="17" />
<flag name="bottom" value="80" />
</attr>
</declare-styleable>
<!-- custom text views -->
<declare-styleable name="View1">
<attr name="text"/>
<attr name="textSize"/>
<attr name="textColor"/>
<attr name="gravity"/>
</declare-styleable>
<declare-styleable name="View2">
<attr name="text"/>
<attr name="textSize"/>
<attr name="textColor"/>
<attr name="gravity"/>
</declare-styleable>
</resources>
Streamlined example
In fact, I don't even need to put the attributes under a custom name. As long as I define them (give them a format) for at least one custom view, I can use them anywhere (without the format). So this also works (and looks cleaner):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="View1">
<attr name="text" format="string"/>
<attr name="textSize" format="dimension"/>
<attr name="textColor" format="color"/>
<attr name="gravity">
<flag name="top" value="48" />
<flag name="center" value="17" />
<flag name="bottom" value="80" />
</attr>
</declare-styleable>
<declare-styleable name="View2">
<attr name="text"/>
<attr name="textSize"/>
<attr name="textColor"/>
<attr name="gravity"/>
</declare-styleable>
</resources>
For a large project, though, this could get messy and defining them at the top in a single location might be better (as recommended here).
Thanks Lewis
I had the same problem , and your inheritance solution gave me the hint for doing it like below and it works fine.I just declared common attributes at the above and rewrite it in the body of style declaration again without formatting.
I hope it helps someone
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- common attributes -->
<attr name="myattr1" format="string" />
<attr name="myattr2" format="dimension" />
<!-- also note "myBackgroundColor" belongs to child styleable"MyView1"-->
<declare-styleable name="MyView1" >
<attr name="myattr1" />
<attr name="myattr2" />
<attr name="myBackgroundColor" format="color"/>
</declare-styleable>
<!-- same way here "myfonnt" belongs to child styelable "MyView2" -->
<declare-styleable name="MyView2" parent="MyView">
<attr name="myattr1" />
<attr name="myattr2" />
<attr name="myfont" format="string"/>
...
</declare-styleable>
Just in case someone still stuck with this problem after tried available solution. I stuck with add subtitle attribute with string format.
My solution is remove the format.
before:
<attr name="subtitle" format="string"/>
after:
<attr name="subtitle"/>

What is correct xmlns url for AdMob in Android Layout XML

I am testing the AdMob for Android SDK. I can't set the admob:testing="true" because the admob attribute is unknown.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:admob="http://schemas.android.com/apk/res/org.ifies.android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF000000"
android:clickable="true"
>
<com.admob.android.ads.AdView
android:id="#+id/admob"
android:visibility="visible"
android:layout_width="fill_parent"
android:layout_height="100px"
android:background="#FF0000"
admob:testing="true"
/>
</LinearLayout>
My Eclipse is complaining that admob:testing="true" would have an unknown prefix. Anyone got an idea on how to fix this?
Sub question: Anyone got an idea how to change the height of the ad? It seems to be fixed to 48px, which is not looking good on any DROID phone...
This took me a while to figure out, so I am posting it here:
Since AdMob 4.1 this has changed, so the previous answer is correct for versions <=4.04. Now you need to use:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
No place for your own package in the new schema and no need for an attrs.xml file.
Details here: http://groups.google.com/group/google-admob-ads-sdk/browse_thread/thread/3b885d3fe5bb21a5?pli=1 (Tim's post, it's the 8th from the top)
Might be worth checking that the error isn't due to something outside the file:
Make sure the package name you are using in your schema URI, "org.ifies.android", matches the value of the package attribute on the manifest element in AndroidManifest.xml.
Make sure res/values/attrs.xml has the attributes specified:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.admob.android.ads.AdView">
<attr name="testing" format="boolean" />
<attr name="backgroundColor" format="color" />
<attr name="textColor" format="color" />
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" />
<attr name="isGoneWithoutAd" format="boolean" />
</declare-styleable>
</resources>

Categories

Resources