how to use custom attributes that a library has? - android

i added a project library to my project, it has some custom attributes defined in its attrs.xml how can i use these attributes ?
Library code:
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NumberProgressBar">
<attr name="progress" format="integer"/>
<attr name="max" format="integer"/>
<attr name="progress_unreached_color" format="color"/>
<attr name="progress_reached_color" format="color"/>
<attr name="progress_reached_bar_height" format="dimension"/>
<attr name="progress_unreached_bar_height" format="dimension"/>
<attr name="progress_text_size" format="dimension"/>
<attr name="progress_text_color" format="color"/>
<attr name="progress_text_offset" format="dimension"/>
<attr name="progress_text_visibility" format="enum">
<enum name="visible" value="0"/>
<enum name="invisible" value="1"/>
</attr>
</declare-styleable>
<declare-styleable name="Themes">
<attr name="numberProgressBarStyle" format="reference"/>
</declare-styleable>
</resources>
My xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/lessens_listview_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:drawableLeft="#drawable/bbb"
android:drawablePadding="5dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="26dp" />
<com.daimajia.numberprogressbar.NumberProgressBar
android:id="#+id/number_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/lessens_listview_textview"
android:layout_marginLeft="12dp"
custom:progress_reached_bar_height="5dp"/>
</RelativeLayout>
here i get error in custom:progress_reached_bar_height="5dp"
as the library's Readme says i can use attributes like this but i cant.
library Readme:
<com.daimajia.numberprogressbar.NumberProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:progress_unreached_color="#CCCCCC"
custom:progress_reached_color="#3498DB"
custom:progress_unreached_bar_height="0.75dp"
custom:progress_reached_bar_height="1.5dp"
custom:progress_text_size="10sp"
custom:progress_text_color="#3498DB"
custom:progress_text_offset="1dp"
custom:progress_text_visibility="visible"
custom:max="100"
custom:progress="80"
/>

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"

Related

Add custom font to attrs.xml

I have an android application where I make use of a custom font, let's call it my_font.ttf which I have saved in my font folder in the res package.
I also have a custom number picker where I need to set the font for the values in the picker. This is my attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NumPicker">
<attr name="max" format="integer" />
<attr name="min" format="integer" />
<attr name="selectedTextColor" format="color" />
<attr name="selectedTextSize" format="float" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="typeface" format="enum">
<enum name="font" value="0"/>
<enum name="sans" value="1"/>
<enum name="serif" value="2"/>
<enum name="monospace" value="3"/>
</attr>
</declare-styleable>
</resources>
I want to know how I can add my_font.ttf into this attrs.xml so that I can use it in my layout file to set the font. Like those serif, sans, monospace ones work as they are built in fonts, but I am not sure how to use my font.
Here is my layout file which makes use of the custom NumPicker:
<com.myproject.slidertest.selectors.numberpicker.NumPicker
android:id="#+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:layout_gravity="center_horizontal"
app:typeface="serif"<!-- I want to be able to change this font to my font-->
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:selectedTextColor="#color/color_blue"
app:textColor="#color/colorTextDefault"
app:max="50"
app:min="10"
/>
Any help or advice will be highly appreciated.
Add another attr
<attr fontTextAppearance format="reference">
In the custom view, if the typeface type is font, look for the typefaceFont reference.
val textAppearance = ta.getResourceId(R.styleable.NumPicker_fontTextAppearance, -1)
if (textAppearance != -1) {
textView.setTextAppearance(textAppearance)
}
<com.myproject.slidertest.selectors.numberpicker.NumPicker
android:id="#+id/numPicker"
style="#style/NumPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fontTextAppearance="#style/NumPickerFont"
/>
<style name="NumPickerFont">
<item name="android:textColor">#color/white</item>
<item name="android:textSize">40sp</item>
<item name="android:textAlignment">viewStart</item>
<item name="android:letterSpacing">0.18</item>
<item name="android:fontFamily">#font/some_font</item>
</style>

declare properties with same name but different types

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

Android admob xml issue

I have a simple problem with admob here...
I've tried all resources to add admob on my android app
but somehow, when i try to add this code to the layout xml:
<Linearlayout
......./>
<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"
/>
it says, The prefix "myapp" for attribute "myapp:backgroundColor" associated with an element type "com.admob.android.ads.AdView" is not bound
what's wrong?
I've add the attrs.xml:
<?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>
and also the xmlns:myapp="http://schemas.android.com/apk/res/com.suit.AdmobTest" in my manifest...
THanks!
You must add the myapp namespace to the root element of your layout file. In you case to the LinearLayout element. The element then has to namespace references. The standard Android reference xmlns:android="http://schemas.android.com/apk/res/android" and your myapp reference xmlns:myapp="http://schemas.android.com/apk/res/com.suit.AdmobTest".
This should do the job.

Same-named attributes in attrs.xml for custom view

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"/>

Format attribute value "android:drawable" not valid

I'm trying to create custom attributes to my button but I dont know which format I must use to images in attributes declaration...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TCButton">
<attr name="Text" format="string"/>
<attr name="BackgroundImage" format="android:drawable" />
</declare-styleable>
</resources>
Error is in the format="android:drawable"...
You can use format="integer", the resource id of the drawable, and AttributeSet.getDrawable(...).
Here is an example.
Declare the attribute as integer in res/values/attrs.xml:
<resources>
<declare-styleable name="MyLayout">
<attr name="icon" format="integer" />
</declare-styleable>
</resources>
Set the attribute to a drawable id in your layout:
<se.jog.MyLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
myapp:icon="#drawable/myImage"
/>
Get the drawable from the attribute in your custom widget component class:
ImageView myIcon;
//...
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyLayout);
Drawable drawable = a.getDrawable(R.styleable.MyLayout_icon);
if (drawable != null)
myIcon.setBackgroundDrawable(drawable);
To see all options possible check the android src here
I think it will be better to use it as a simple reference:
<declare-styleable name="TCButton">
<attr name="customText" format="string"/>
<attr name="backgroundImage" format="reference" />
</declare-styleable>
And set it in your xml like this:
<your.package.name.TCButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:customText="Some custom text"
custom:backgroundImage="#drawable/myImage"
/>
And in your class set the attributes like this:
public TCButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MembershipItemView, 0, 0);
String customText;
Drawable backgroundImage;
try {
customText = a.getString(R.styleable.TCButton_customText);
backgroundImage = a.getDrawable(R.styleable.TCButton_backgroundImage);
} finally {
a.recycle();
}
if(!TextUtils.isEmpty(customText)) {
((TextView)findViewById(R.id.yourTextView)).setText(customText);
}
if(null != backgroundImage) {
((ImageView)findViewById(R.id.yourImageView)).setBackgroundDrawable(backgroundImage);
}
}
PS:
Don't forget to add this line for the root element of the layout you are using your custom view in
xmlns:custom="http://schemas.android.com/apk/res-auto"
If you don't set this, you won't be able to access your custom attributes.
From AOSP code, I found how google engineers declare ImageView#src attr.
<declare-styleable name="ImageView">
<attr name="src" format="reference|color" />
<attr name="scaleType">
<enum name="matrix" value="0" />
<enum name="fitXY" value="1" />
<enum name="fitStart" value="2" />
<enum name="fitCenter" value="3" />
<enum name="fitEnd" value="4" />
<enum name="center" value="5" />
<enum name="centerCrop" value="6" />
<enum name="centerInside" value="7" />
</attr>
<attr name="adjustViewBounds" format="boolean" />
<attr name="maxWidth" format="dimension" />
<attr name="maxHeight" format="dimension" />
<attr name="tint" format="color" />
<attr name="baselineAlignBottom" format="boolean" />
<attr name="cropToPadding" format="boolean" />
<attr name="baseline" format="dimension" />
<attr name="drawableAlpha" format="integer" />
<attr name="tintMode" />
</declare-styleable>
Above code is a sample and it can cover most case in our development.

Categories

Resources