3

I need some help, I've been browsing for a while, but none of the topic I've found could resolve my problem. Hope you'll help me !

Here we go : I have a custom view, let's call it CustomView. It has some custom attributes too, defined in an attrs.xml file, like below :

<declare-styleable name="CustomView">
    <attr name="customBackgroundColor" format="color"/>
    <attr name="customTextColor" format="color"/>
    <attr name="customWhatever" format="dimension"/>
</declare-styleable>

This view is part of a library I want to create so I can use it in multiple projects.

The funny part comes here : Actually, I'll use this view really often, so I want to define a style in styles.xml to define its property, so I don't have to go in every xml layout files where I use this view to edit its attributes. Something like this :

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="customViewStyle">@style/myCustomViewStyle</item>
</style>

<style name="myCustomViewStyle" parent="CustomViewStyle">
    <item name="customBackgroundColor">@color/red</item>
    <item name="customTextColor">@color/blue</item>
    <item name="customWhatever">@dimen/someHeight</item>
</style>

So my question is : How do I define this "customViewStyle" key, and if it's possible, how to get informations from it ?

Bonus : I've been able to create this "customViewStyle" key by doing this :

<attr name="customViewStyle" format="reference"/>

But I haven't found yet how to exploit it.

1 Answer 1

2

How do I define this "customViewStyle" key?

<attr name="customViewStyle" format="reference"/>, outside of any <declare-styleable> tags.

But I haven't found yet how to exploit it.

Make a style that has the properties you want. You have already done that with MyCustomViewStyle entry in your post, but you've given it a parent style that doesn't exist (as far as I can tell). I would probably use `parent="android:Widget" unless something else is more appropriate.

I presume your custom view class already reads attributes from XML using TypedArray:

TypedArray a = context.obstainStyleAttributes(attrs, R.styleable.CustomView,
        R.attr.customViewStyle, 0);

Replace the last argument with your default style you just defined:

TypedArray a = context.obstainStyleAttributes(attrs, R.styleable.CustomView,
        R.attr.customViewStyle, R.style.MyCustomViewStyle);
Sign up to request clarification or add additional context in comments.

1 Comment

"you've given it a parent style that doesn't exist" Yeah I know, I just wanted to show what I'd like my code to look like, but what you said next just did hat I wanted. You sir deserve a cookie ! Thanks bro, now I can go back to work !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.