I am creating an Android SDK as jar. It contains some custom views with custom parameters. I want to create a drop-in solution, where developers don't need to do anything other than dropping the jar in their libs folder. I cannot go with a real library project, it's a business requirement.
Everything actually works fine, and this is not my first android project, that ships as jar. But in this one, I need to have custom attributes for custom views. This means that Android needs to be aware of the attribute set supported by the views trough an xml schema.
The easy solution is to have the users put a predefined attr.xml in they resources folder. However I have seen that libraries like admob work without a custom attr.xml. For example by admob you declare:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<com.google.ads.AdView android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="AD_UNIT_ID_GOES_HERE"
ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE"
ads:loadAdOnCreate="true"/>
</LinearLayout>
but you don't need to add an attr.xml in your application. If i try to use it like they do (i have the view in a jar) and have the same layout as above with my own custom attributes, then aapt complains:
I have already looked into admobs jar file, and i cannot find anything special in the com.google.ads package, that looks like an xml definition. Any idea how they managed to do this / how does aapt know which attributes are supported by admob's view?
Thanks!
It's not neseccary to create attr.xml in order to use custom attributes. You can use the folowing methods to get attr value by its package and name:
Here is a simple example how to use them:
Layout file:
<com.example.HelloAndroid.StubView
xmlns:stub="http://schemas.android.com/apk/lib/com.example.HelloAndroid"
android:id="@+id/stub"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
stub:title="title value"
stub:subtitle="subtitle value" />
StubView.java:
public class StubView extends View {
public StubView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public StubView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final String packname = "http://schemas.android.com/apk/lib/com.example.HelloAndroid";
if (attrs != null) {
final String title = attrs.getAttributeValue(packname, "title");
final String subtitle = attrs.getAttributeValue(packname, "subtitle");
Log.d("Test", "Title " + title);
Log.d("Test", "Subtitle " + subtitle);
}
}
}
You can decompile AdMob jar and see that they use the same approach.
EDIT:
In case you are getting No resource identifier found for attribute 'XXX' in package 'com.XXX.XXX' error, make sure your namespace doesn't look like http://schemas.android.com/apk/res/your.package.name. apk/res is the most important because in this case appt will check if mentioned attributes were really declared in the attrs.xml. You can just use http://schemas.android.com/apk/lib/your.package.name namespcae in order to avoid this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With