How to obtain styled attributes anywhere outside custom view? - android

Custom attributes are great, but all tutorials mention usage in custom views where you have AttributeSet parameter ready.
Contents of my attrs.xml:
<declare-styleable name="StyledDialogs">
<attr name="sdlDialogStyle" format="reference" />
</declare-styleable>
<declare-styleable name="DialogStyle">
...
</declare-styleable>
I'm struggling how to access those attributes in any class outside custom view.

After a few hours, I have figured out a way which works:
final TypedArray a = mContext.getTheme().obtainStyledAttributes(null, R.styleable.DialogStyle, R.attr.sdlDialogStyle, 0);

Related

Pass AttributeSet as parameter in custom view constructor in AppWidget for android

I need to pass AttributeSet as a parameter in custom View constructor. Here is the attribute that I need to pass:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DonutChart">
<attr name="radiusDonut" format="dimension"/>
<attr name="donutTextSize" format="dimension"/>
</declare-styleable>
</resources>
I need to do this with code, in order that I will add my custom view to RemoteView in my AppWidget. Does anyone has any idea how can I achive this?
You should be able to use XmlPullParser to get the attributes from your XML resource and then pass those as follows, replacing res_id with the ID of the file above.
XmlResourceParser resourceParser = activity.getResources().getXml(R.xml.res_id);
AttributeSet attrs = Xml.asAttributeSet(resourceParser);
CustomView view = new CustomView(activity, attrs);

how to get reference to color attribute defined in xml layouts?

Update: There were 2 custom attributes defined in CustomTextView.. If both are defined in xml it works fine.. If first is missing it does not give any value for 2nd also...
<com.mycompany.projectname.MyCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:extraColor="?someColor"
/>
Here someColor is another color attr which varies for different themes..
I need value of extraColor custom attribute in MyCustomView class...
Currently obtaining it as below:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, defStyleAttr, 0);
a.getColorStateList() does not work here...
CustomTextView defined as:
<declare-styleable name="CustomTextView">
<attr name="state" format="boolean" />
<attr name="extraColor" format="reference|color" />
</declare-styleable>
Update: There were 2 custom attributes defined in CustomTextView..
If both are defined in xml it works fine.. If first is missing it does not give any value for 2nd also...
Could you try that one..
a.getColor(R.styleable.CustomTextView_extraColor, Color.WHITE)
use only either reference or color
<declare-styleable name="CustomTextView">
<attr name="extraColor" format="reference" />
</declare-styleable>
And then if it is reference get it like,
a. getColorStateList(R.styleable.CustomTextView_extraColor);
if it is color then,
a. getColor(R.styleable.CustomTextView_extraColor, Color.WHITE);
In addition to what others have said, it looks like the naming is inconsistent (copy+paste error?): in most of the code you posted, your refer to a CustomTextView, but in the XML, you use MyCustomView.
Try changing your XML:
<com.mycompany.projectname.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:extraColor="?someColor"
/>

Custom Attribute Error - Android Studio 1.2

In my Android project I have a couple of custom components that use custom attributes.
The attrs.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources >
<declare-styleable name = "TextBox">
<attr name = "font" format = "string"/>
</declare-styleable>
<declare-styleable name = "ButtonBox">
<attr name = "font" format = "string"/>
</declare-styleable>
</resources>
I am pulling in the attributes just fine in the custom component, but when I go to run the code I see the following error.
Error: Found item Attr/font more than one time
Error: Execution failed for task ':app:mergeDebugResources'.
It shouldn't make a difference that there are similar attribute names in two different declare-styleable resources correct?
If you have any help it would be greatly appreciated, thank you!
As you can see here, the attr itself can have multiple properties and can be defined only once and you can configure multiple details inside it.
So you should give it different names or, since they have the same properties, use only one declare-styable for both.
Check out this link too, there's a good example.
Here is how it should be:
<?xml version="1.0" encoding="utf-8"?>
<resources >
<declare-styleable name="Box">
<attr name="font" format="string"/>
</declare-styleable>
</resources>
You can use Box on text, button, etc.
Android — Custom Views: same Attribute on multiple Custom Views
You may have tried something like the code below in your attrs.xml:
'''
<resources>
<declare-styleable name="MyComponent1">
<attr name="myAttr" format="string" />
</declare-styleable>
<declare-styleable name="MyComponent2">
<attr name="myAttr" format="string" />
</declare-styleable>
</resources>
'''
But this does not work, and we get an Error:
Found item Attr/myAttr more than one time.
How do native views handle that?
Native Views have the same attribute names and don’t suffer from this problem.
Let’s sneak into their code and see how these attributes are declared.
Looking into their code, we can see that they create the attr tag outside declare-styleable, and then, inside of it you just need to reference it only by its name, there is no need to declare format again.
Our code should be just like that:
<resources>
<attr name="myAttr" format="string" />
<declare-styleable name="MyComponent1">
<attr name="myAttr" />
</declare-styleable>
<declare-styleable name="MyComponent2">
<attr name="myAttr" />
</declare-styleable>
</resources>
can also read this fantastic medium paper :
same Attribute on multiple Custom Views

Can't use globally defined custom attribute

So I have defined a custom attribute:
<resources>
<attr name="filterColor" format="color"/>
</resources>
I did not declare it in an to reuse it in other custom views.
However, I am not able to use it like so
<SomeView
app:filterColor="#color/my_color"/>
Nor am I able to obtain it from AttributeSet
final TypedArray a = getContext().obtainStyledAttributes(attrs, new int[]{R.attr.filterColor});
It's driving me crazy - why are we allowed to declare global custom Attributes then if we can't use them?
If nothing else, your XML appears to be missing the <declare-styleable> element:
<resources>
<declare-styleable name="ColorMixer">
<attr name="color" format="color" />
</declare-styleable>
</resources>
(pulled from this library project)
The name in declare-styleable should be the class name of the view that will use the attribute (SomeView in your case).
This is covered in the documentation.

Show my custom layouts in the "tools palette" when editing layouts

Is it possible to show custom widgets and layouts that you've created in your projects as part of the tools palette in the layout editor?
There is a section called "Custom & Library Views" that is empty with a refresh button.
Custom Layouts Requirements
For a custom layout to appear in the tools palette it must conform to the following:
You must implement a constructor that takes a Context and Attributes as parameters.
You must define custom attributes using the <declare-styleable> tags in the XML.
Here is an example of the constructor.
class PieChart extends View {
public PieChart(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
Here is an example of the attributes.
<resources>
<declare-styleable name="PieChart">
<attr name="showText" format="boolean" />
<attr name="labelPosition" format="enum">
<enum name="left" value="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>
Further reading on Creating Custom Views.

Categories

Resources