First, I was a little fuzzy on including an external jar. I think I should be able to just include it in the library, but I seem to also have to include it in the main application. Is this because libraries are just build constructs, so the library inclusion of a jar is ignored when the main app is built? Turns out Android doc addresses this here: "You can develop a library project that itself includes a JAR library, however you need to manually edit the dependent application project's build path and add a path to the JAR file."
Meanwhile, back to my layout problem. Here is my Admob layout code.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.rellimsoft.android.remata.base"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<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"
/>
</LinearLayout>
I get this error: "No resource identifier found for attribute 'backgroundColor' in package 'com.rellimsoft.android.remata.base'". Same for all the "myapp" attributes. I examined this and wonder if referring to an apk in a library is a problem since libraries don't generate apk's (xmlns:myapp="http://schemas.android.com/apk/res/com.rellimsoft.android.remata.base"" ).
Turns out it's not an Admob problem, but an Eclipse problem when generally referring to attributes in a library (see this post on Stackoverflow). Sounds like it can't really be done cleanly in Eclipse.
Ideas.
- Is the apk a problem? Prob not. Comment on Stackoverflow seem like it works if built properly, more of an Eclipse problem.
- Refer to dependent app in library. Hate this!! Can reduce hit by at least having dependent app interface to get info, but still needed in resource layout file.
- Conflate view in Java code. Use interface that main app can implement. Need to check if this can be done. Still not great, but maybe can avoid the absolutely wrong library referring to dependent code.
Solved it!
First, helpful start on creating views in UI. Turns out I don't even need to conflate view in code or parent code. Just add attributes in Java. Here is my new layout, minus the offending attributes.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.rellimsoft.android.remata.base"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.admob.android.ads.AdView
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Here is the corresponding Java code in my onCreate method (mAddView defined as class global).
private AdView mAdView = null;
...
mAdView = (AdView)findViewById(R.id.ad);
mAdView.setBackgroundColor(0x000000);
mAdView.setPrimaryTextColor(0xFFFFFF);
mAdView.setSecondaryTextColor(0xCCCCCC);
This methodology is a simple, well-structured solution for Admob. It probably is good for many cases. It will be bad if there are huge layouts that need to be included in code, since doing so is easier in xml layouts. However, the only problem is dealing with attributes defined in the current namespace, I wouldn't think that would affect layouts over a large scale. This solution also allows getting rid of data in attr.xml.
This comment has been removed by the author.
ReplyDelete