so I recently migrated to gradle now my custom view attributes return null
my project looks like this
--custom_icon_view // library that holds the custom view with custom attributes --my application // this is the main app that actually uses the custom view
in my layout I have the namespace defined like this :
xmlns:iconview="http://schemas.android.com/lib/be.webelite.iconview"
because using apk/res-auto retuns an error saying attibutes could not be identified
this is how I try to get the icon name defined in xml, this used to work perfectlly but now it doesnt. and all I changed was migrating to gradle.
final TypedArray a = context.obtainStyledAttributes(attrs,be.webelite.iconview.R.styleable.icon);
icon_name = a.getString(be.webelite.iconview.R.styleable.icon_name);
so I'm guessing my gradle.build files are causing a problem?
I have the library set to
apply plugin: 'android-library'
end the main app gradle.build as
apply plugin: 'android'
this has been giving me headache for 2 days now :( any help/hints are very apperciated.
here are my gradle files
http://pastie.org/private/h57o8nanydosq0dtm6eiq
and here is the folder structure
http://pastie.org/private/nvbzomx2zeagdpzt8qqjsq
this is how I declare my view in the xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<!-- tried res-auto didn't work -->
xmlns:iconview="http://schemas.android.com/apk/lib/be.webelite.iconview"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_gray">
<be.webelite.iconview.IconView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
iconview:icon_name="entypo_search"
android:textSize="25dp"/>
attrs.xml in IconView>res>values directory
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="icon">
<attr name="name" format="string" />
</declare-styleable>
</resources>
If you are extending Gradle, e.g. by writing a plugin for another ecosystem, declaring custom attributes could be an option if you want to support variant-aware dependency management features in your plugin. However, you should be cautious if you also attempt to publish libraries.
As explained in the section on variant aware matching, attributes give semantics to variants and are used to perform the selection between them. As a user of Gradle, attributes are often hidden as implementation details. But it might be useful to understand the standard attributes defined by Gradle and its core plugins.
Because multiple values for an attribute can be compatible with the requested attribute, Gradle needs to choose between the candidates. This is done by implementing an attribute disambiguation rule. Attribute disambiguation rules have to be registered via the attribute matching strategy that you can obtain from the attributes schema.
Now, to begin adding custom attributes to your custom views, you have to first add a new file your “values” directory and name it “attrs.xml”. Inside this xml file, inside tags, add a “declare-styleable” tag with attribute “name” as MyCustomView (your custom view class name).
Can't really see what's wrong in your project. Here is how I use custom view & attrs in mine :
In my library project :
attrs.xml :
<declare-styleable name="CustomFontSize">
<attr name="typeFace" format="string" />
</declare-styleable>
in my custom class :
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontSize);
if (a == null) {
return;
}
CharSequence s = a.getString(R.styleable.CustomFontSize_typeFace);
if (s != null) {
// do something
}
In my Main Project here an example of one of my layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/border_margin_pulltorefresh"
android:paddingRight="@dimen/border_margin_pulltorefresh"
android:paddingBottom="@dimen/divider_height">
<com.custom.view.TextViewFont
style="@style/DateStyle"
android:id="@+id/news_date"
android:shadowColor="@color/white"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
custom:typeFace="@string/font_roboto_condensed_bold"/>
</LinearLayout>
Hope it will help ...
Edit :
in my build.gradle of my "main project"
dependencies {
compile project(":MyLibraryProject")
}
And here the build.gradle of my library :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:support-v4:19.0.0'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile project(':WebediaCore:dependencies:DataDroid')
compile project(':WebediaCore:dependencies:ViewPagerIndicator')
compile project(':WebediaCore:dependencies:ActionBar-PullToRefresh')
compile project(':WebediaCore:dependencies:Android-Universal-Image-Loader')
}
android {
compileSdkVersion 19
buildToolsVersion '19'
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
}
EDIT1 :
Try to use this namespace :
xmlns:custom="http://schemas.android.com/apk/res-auto"
and replace :
iconview:icon_name="entypo_search"
by :
custom:name="entypo_search"
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