how to use custom attributes that a library has?
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"
Custom View attributes are not accessible in xml android
The proper format for drawable resources is reference
, no integer
. Change your attribute definitions to something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="VolumnBar">
<attr name="disableProgressRes" format="reference" />
<attr name="enableProgressRes" format="reference" />
</declare-styleable>
</resources>
Create Custom Compound View in Android with Attributes
If we add new compound view code and its attributesinside project, we should add this at the beginning of layout:
xmlns:custom="http://schemas.android.com/apk/res/your_main_app_package
and if new compound view is inside a library project linked to our peoject, we should add this:
xmlns:custom="http://schemas.android.com/apk/res-auto
Link: https://stackoverflow.com/a/10217752/1152549
android custom view attributes not working after switch to gradle
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"
Defining custom attrs
Currently the best documentation is the source. You can take a look at it here (attrs.xml).
You can define attributes in the top <resources>
element or inside of a <declare-styleable>
element. If I'm going to use an attr in more than one place I put it in the root element. Note, all attributes share the same global namespace. That means that even if you create a new attribute inside of a <declare-styleable>
element it can be used outside of it and you cannot create another attribute with the same name of a different type.
An <attr>
element has two xml attributes name
and format
. name
lets you call it something and this is how you end up referring to it in code, e.g., R.attr.my_attribute
. The format
attribute can have different values depending on the 'type' of attribute you want.
- reference - if it references another resource id (e.g, "@color/my_color", "@layout/my_layout")
- color
- boolean
- dimension
- float
- integer
- string
- fraction
- enum - normally implicitly defined
- flag - normally implicitly defined
You can set the format to multiple types by using |
, e.g., format="reference|color"
.
enum
attributes can be defined as follows:
<attr name="my_enum_attr">
<enum name="value1" value="1" />
<enum name="value2" value="2" />
</attr>
flag
attributes are similar except the values need to be defined so they can be bit ored together:
<attr name="my_flag_attr">
<flag name="fuzzy" value="0x01" />
<flag name="cold" value="0x02" />
</attr>
In addition to attributes there is the <declare-styleable>
element. This allows you to define attributes a custom view can use. You do this by specifying an <attr>
element, if it was previously defined you do not specify the format
. If you wish to reuse an android attr, for example, android:gravity, then you can do that in the name
, as follows.
An example of a custom view <declare-styleable>
:
<declare-styleable name="MyCustomView">
<attr name="my_custom_attribute" />
<attr name="android:gravity" />
</declare-styleable>
When defining your custom attributes in XML on your custom view you need to do a few things. First you must declare a namespace to find your attributes. You do this on the root layout element. Normally there is only xmlns:android="http://schemas.android.com/apk/res/android"
. You must now also add xmlns:whatever="http://schemas.android.com/apk/res-auto"
.
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:whatever="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.example.mypackage.MyCustomView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>
Finally, to access that custom attribute you normally do so in the constructor of your custom view as follows.
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, 0);
String str = a.getString(R.styleable.MyCustomView_my_custom_attribute);
//do something with str
a.recycle();
}
The end. :)
Related Topics
Sethinttextcolor() in Edittext
Backup/Restore Sqlite Db in Android
How to Run a Specific Android App Using Terminal
Android Build Gradle Is Too Slow (Dependency Resolution)
Release APK Not Updating with JavaScript Code
Writing Some Characters Like '<' in an Xml File
Android: Wait on User Input from Dialog
How to Set The App Icon as The Notification Icon in The Notification Drawer
Android, How Do How to Get a List of All Files in a Folder
Set Visibility of Progress Bar Gone on Completion of Image Loading Using Glide Library
Add Viewpagerindicator to Android Studio
How to Detect Android Listview Scrolling Stopped
How to List All Files and Folders Locating on Sd Card
What Is Better: @Suppresslint or @Targetapi
Linearlayout Height in Oncreate Is 0
Recyclerview Smoothscroll to Position in The Center. Android