9

I have a custom view that extends one of the framework classes. Most Views in Android have some default attributes defined for them (such as Button being clickable, which is set by android:clickable="true").

How do I provide application-wide defaults for my custom view?

Karakuri
  • 36,506
  • 12
  • 75
  • 103
  • http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml – Ixx May 17 '12 at 18:05
  • I'm not asking about how to define custom attributes. I already do that. What I want is to have some **defaults** defined for those attributes so the view can be added to a layout file without having to set some extra properties. – Karakuri May 17 '12 at 18:18

3 Answers3

15

I solved my own problem thusly:

res/values/attrs.xml:

<resources>
    <attr name="customViewStyle" type="reference" />
</resources>

res/values/styles.xml:

<resources>
    <style name="AppTheme" parent="@android:style/Theme.Light">
        <item name="android:background">@drawable/bg_light</item>
        <item name="customViewStyle">@style/CustomView</item>
    </style>

    <style name="CustomView">
        <item name="android:clickable">true</item>
        <item name="android:focusable">true</item>
        <item name="android:focusableInTouchMode">true</item>
    </style>
</resources>

Then all you do is set the theme in the manifest to AppTheme. This also works with standard widgets using android:[widget]Style in place of customViewStyle in the definition of AppTheme.

Karakuri
  • 36,506
  • 12
  • 75
  • 103
  • 4
    How does this get applied to your view though? Where is customViewStyle linked to your View implementation? – cottonBallPaws Nov 13 '15 at 14:34
  • 4
    @cottonBallPaws, the following pointed me in the right direction: http://stackoverflow.com/a/31505624/5166250 – Travis Nov 18 '15 at 22:15
1

You could additionally create a style for this layout and set the style where you use the view.

source: http://developer.android.com/guide/topics/resources/style-resource.html

soost
  • 91
  • 3
  • 3
    Using the style tag is just a shortcut for setting setting individual attributes in the layout file - that's not what i'm trying to accomplish. I want to have some default appearance/attributes, and then whoever is using my custom view can use the style tag to further modify it. Think of it this way: When you add a TextView to a layout, it has defaults for text size, text color, etc. Those have to come from somewhere, because you don't have to specify a style or anything. Attributes you change or style you reference will be applied over those defaults. – Karakuri May 17 '12 at 18:25
0

I think you will need to override the View constructors that take an AttributeSet argument, and build your own modified AttributeSet to pass to the superclass constructor.

EDIT: Even easeer, just have your View subclass constructor call setClickable etc to change the defaults to what you want.

Lawrence D'Oliveiro
  • 2,640
  • 1
  • 12
  • 11
  • 4
    This is what i did originally. The problem is, if another developer uses my custom view and puts clickable="false" in his xml, then my call to `setClickable()` undermines his programming. (Maybe he plans to make it clickable only after the user has done something, like checked a checkbox.) Even though **I** expect it to be clickable, others may not, so I'd like to provide some default behavior without resorting to internal calls like that. Otherwise I need a way to read all the attributes for the view, not only the custom ones I've defined in attrs. – Karakuri May 18 '12 at 05:50