1

I've looking for a way to declare a parameterized view with in a layout. For example if I have a class NumericTextView<T> extends TextView I want to be able to declare it in an xml layout. The following doesn't seem like it will work because of the way the layout is parsed.

<NumbericTextView<Integer>/>

But could I potentially have something like this?

<NumbericTextView type:Integer/>

If that still won't work is there anything wrong with using some custom FrameLayout that switches on a parameter to add the correct NumbericTextView<T> child?

StackJP
  • 1,450
  • 2
  • 16
  • 32

1 Answers1

1

You'll want to do something like:

<com.my.ui.NumericTextView
    android:id="@+id/MyTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

see android custom text view can not be added in layout file

and this for custom attributes: http://prasanta-paul.blogspot.com/2010/05/android-custom-textview.html

Community
  • 1
  • 1
theelfismike
  • 1,621
  • 12
  • 18
  • I understand I need the full package path to add the view via xml and how to create custom attributes. The problem I'm having is specifying the parameterized element in an xml layout. So in code I can use `new NumericTextView()` but I don't know if its possible to do that in the layout itself – StackJP Feb 06 '13 at 18:17
  • If you use the custom attributes like in the second link, you can pass that into the constructor of the views. The layout would look like: 'my:type="integer"'. That would give you some of that freedom, but wouldn't allow true parameterized classes. – theelfismike Feb 06 '13 at 18:26
  • I kind of figured that this wasn't something that was supported. I'll probably have to work-around it with a wrapper layout with custom attributes. I'll leave this open for awhile in case someone else has another solution otherwise I'll mark this as the answer – StackJP Feb 06 '13 at 19:36