1

I have the following question, if I have a class extending a LinearLayout like:

public class ExtendedSeekbarLayout extends LinearLayout { ..}

and I would like to pass additional Arguments to my Layout, how do I do this? I know that I could have the following constructors like:

 public ExtendedSeekbarLayout (Context context) {
    super(context);

}

public ExtendedSeekbarLayout (Context context, AttributeSet attributeSet) {
    super(context, attributeSet);

}

public ExtendedSeekbarLayout (Context context, AttributeSet attributeSet, int defStyle) {
    super(context, attributeSet, defStyle);

}

but I would like to have something like:

 public ExtendedSeekbarLayout (Context context, AttributeSet attributeSet, int defStyle, int position) {
    super(context, attributeSet, defStyle);
    init(position);

}

I'm not sure if this is possible, if not, which would be the way to go then?

Thanks a lot and cheers, pingu

1 Answers1

5

This constructor that you shared should work exactly as you expect.

public ExtendedSeekbarLayout (Context context, AttributeSet attributeSet, int defStyle, int position) {
    super(context, attributeSet, defStyle);
    init(position);
}

Btw you don't necessarily need to have this constructor, as long as you call

super(context);

You can do this in case of programmatically instantiating a view:

public ExtendedSeekbarLayout (Context context, int position) {
    super(context);
    init(position);
}

But if you are talking about sending a custom value from xml, where you don't actually call a constructor, then you should look at this answer: https://stackoverflow.com/a/7608739/2534007

Community
  • 1
  • 1
Mohib Irshad
  • 1,940
  • 23
  • 18
  • ok, but in case of using an xml it seems like that the custom value has to be predefined, but in my case I would like to be able to set an arbitrary value for it. In case of using the programmatically approach I know that context would be getActivity().getBaseContext() but I'm not sure what to pass to the attributeSet and to the defStyle. – user3914939 Jun 03 '15 at 14:04
  • In xml, only attributes are predefined, values for it can actually be whatever you want. for e.g. you can define position attribute and the value for it can be anything you want. You can put any value when instantiating in xml and process the value programmatically. for a better reference, see this: http://jeffreysambells.com/2010/10/28/custom-views-and-layouts-in-android-os-sdk and in case of doing programmatically, see my edited answer. – Mohib Irshad Jun 04 '15 at 04:37