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...)
Related
I've written a number picker that supports Integer and Float - therefore I've written a generic base class and define two derived typed class like NumberPickerInt and NumberPickerFloat. Those custom views do share a set of common xml settings but also a set of settings, that should be named the same but have different types, like following example shows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="mnp_prefix" format="string|reference" />
<attr name="mnp_suffix" format="string|reference" />
<attr name="mnp_editTextStyle" format="reference" />
<attr name="mnp_buttonWidth" format="dimension" />
<attr name="mnp_iconUp" format="reference" />
<attr name="mnp_iconDown" format="reference" />
<attr name="mnp_iconUpLarge" format="reference" />
<attr name="mnp_iconDownLarge" format="reference" />
<attr name="mnp_longPressRepeatClicks" format="boolean" />
<attr name="mnp_style" format="enum">
<enum name="input" value="0" />
<enum name="scroll" value="1" />
</attr>
<declare-styleable name="MaterialNumberPickerInteger">
<attr name="mnp_valueInt" format="integer" />
<attr name="mnp_stepSizeInt" format="integer" />
<attr name="mnp_stepSizeLargeInt" format="integer" />
<attr name="mnp_minInt" format="integer" />
<attr name="mnp_maxInt" format="integer" />
<attr name="mnp_prefix" />
<attr name="mnp_suffix" />
<attr name="mnp_editTextStyle" />
<attr name="mnp_buttonWidth" />
<attr name="mnp_iconUp" />
<attr name="mnp_iconDown" />
<attr name="mnp_iconUpLarge" />
<attr name="mnp_iconDownLarge" />
<attr name="mnp_longPressRepeatClicks" />
<attr name="mnp_orientation" />
<attr name="mnp_style" />
<attr name="android:background" />
<attr name="android:orientation" />
</declare-styleable>
<declare-styleable name="MaterialNumberPickerFloat">
<attr name="mnp_valueFloat" format="float" />
<attr name="mnp_stepSizeFloat" format="float" />
<attr name="mnp_stepSizeLargeFloat" format="float" />
<attr name="mnp_minFloat" format="float" />
<attr name="mnp_maxFloat" format="float" />
<attr name="mnp_prefix" />
<attr name="mnp_suffix" />
<attr name="mnp_editTextStyle" />
<attr name="mnp_buttonWidth" />
<attr name="mnp_iconUp" />
<attr name="mnp_iconDown" />
<attr name="mnp_iconUpLarge" />
<attr name="mnp_iconDownLarge" />
<attr name="mnp_longPressRepeatClicks" />
<attr name="mnp_orientation" />
<attr name="mnp_style" />
<attr name="mnp_commas" format="integer" />
<attr name="android:background" />
<attr name="android:orientation" />
</declare-styleable>
<attr name="pickerStyle" format="reference" />
</resources>
Question
As you can see, I've defined shared attributes like mnp_prefix and mnp_suffix which is easy because they are strings for both views. As you can image, a number picker has a value, so I defined a mnp_valueInt for the integer number picker and mnp_valueFloat for the float number picker - what I would prefer though is to define mnp_float in both views but with different formats - inter or float, depending on the picker type. BUT if I do this, I get compilation errors because mnp_value is defined multiple times. Is there a more beautiful solution than mine for this problem?
I just updated to Android Studio 3.6.1 and the most up-to-date gradle versions and now my project won't build with the error
.gradle/caches/transforms-2/files-2/<some-hash-number>/navigation-common-1.0.0-alpha01/res/values/values.xml:16:5-21-25: AAPT: error: resource attracts/type not found
I attempted to clear my cache and rebuild, but it didn't help. So I also tried a Invalidate Caches and Restart, and that also didn't change anything. Not sure what else to try...
EDIT: Added Values.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NavAction">
<attr name="android:id"/>
<attr format="reference" name="destination"/>
<attr format="boolean" name="launchSingleTop"/>
<attr format="boolean" name="launchDocument"/>
<attr format="boolean" name="clearTask"/>
<attr format="reference" name="popUpTo"/>
<attr format="boolean" name="popUpToInclusive"/>
<attr format="reference" name="enterAnim"/>
<attr format="reference" name="exitAnim"/>
<attr format="reference" name="popEnterAnim"/>
<attr format="reference" name="popExitAnim"/>
</declare-styleable>
<declare-styleable name="NavArgument">
<attr name="android:name"/>
<attr name="android:defaultValue"/>
<!--free format since in future it could be Parcelable-->
<attr name="type"/>
</declare-styleable>
<declare-styleable name="NavDeepLink">
<attr format="string" name="uri"/>
<attr name="android:autoVerify"/>
</declare-styleable>
<declare-styleable name="NavGraphNavigator">
<attr format="reference" name="startDestination"/>
</declare-styleable>
<declare-styleable name="Navigator">
<attr name="android:id"/>
<attr name="android:label"/>
</declare-styleable>
</resources>
I had the same problem after upgrading to Android Studio 3.6. To fix it, I had to add the format of my custom view attributes in my attrs.xml file. E.g.
<declare-styleable name="TheCustomView">
<attr name="custom_view_attribute" />
</declare-styleable>
became:
<declare-styleable name="TheCustomView">
<attr name="custom_view_attribute" format="string" />
</declare-styleable>
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...
I am trying to declare properties for to different classes some of the properties have the same name but different types on different classes. Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="net.firouz.mastergardner.IntEditView">
<attr name="caption" format="string" />
<attr name="min_val" format="integer" />
<attr name="max_val" format="integer" />
</declare-styleable>
<declare-styleable name="net.firouz.mastergardner.FloatEditView">
<attr name="min_val" format="float" />
<attr name="max_val" format="float" />
</declare-styleable>
</resources>
but eclipse complains that the attributes max_val and min_val have already been defined. How can I fix this.
Thank you
Sam
You can go with either of below 2 solution
<?xml version="1.0" encoding="utf-8"?>
<attr name="min_val" format="float" />
<attr name="max_val" format="float" />
<resources>
<declare-styleable name="net.firouz.mastergardner.IntEditView">
<attr name="caption" format="string" />
<attr name="min_val" />
<attr name="max_val" />
</declare-styleable>
<declare-styleable name="net.firouz.mastergardner.FloatEditView">
<attr name="min_val" />
<attr name="max_val" />
</declare-styleable>
</resources>
or
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="net.firouz.mastergardner.IntEditView">
<attr name="caption" format="string" />
<attr name="min_val_i" format="integer" />
<attr name="max_val_i" format="integer" />
</declare-styleable>
<declare-styleable name="net.firouz.mastergardner.FloatEditView">
<attr name="min_val_f" format="float" />
<attr name="max_val_f" format="float" />
</declare-styleable>
</resources>
android generated only one class for all attributes "R.attr", and as min_val and max_val were already defined as integer eclipse complain next time it sees its declaration as float.
Add your attributes directly as children of the node:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="min_val" format="float" />
<attr name="max_val" format="float" />
<declare-styleable name="net.firouz.mastergardner.IntEditView">
<attr name="caption" format="string" />
<attr name="min_val" />
<attr name="max_val"/>
</declare-styleable>
<declare-styleable name="net.firouz.mastergardner.FloatEditView">
<attr name="min_val" />
<attr name="max_val"/>
</declare-styleable>
</resources>
Else you have to use different attribute names for your float and integer attribute
I found a better answer with the help of this posting.
Apparently attributes can have multiple formats which makes the following code valid and can work for me.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="min_val" format="float|integer|reference" />
<attr name="max_val" format="float|integer|reference" />
<declare-styleable name="IntEditView">
<attr name="Caption" format="string|reference" />
<attr name="min_val" />
<attr name="max_val" />
</declare-styleable>
<declare-styleable name="FloatEditView">
<attr name="min_val" />
<attr name="max_val" />
</declare-styleable>
</resources>
Thank you everyone
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"/>