0

I'm creating my own custom LinearLayout, and extending it's class I need to add children to the layout using the addview method,

I need to have access on the tag, which is set after the constructor, so please I need to know the best practice for doing something like this

here's my code:

public class MyLayout extends LinearLayout {

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


        MyTag tag = (MyTag) getTag();


        // Parent layout
        setOrientation(VERTICAL);
        setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        setBackgroundColor(tag.getBgColor());
    }

}
  • getTag() will always return null in constructor. (you can't set it, until construct object) – Jin35 Oct 12 '12 at 13:41
  • Okay guys I know the issue and need another way or method that will be called once on layout display ... I checked onLayout method by think it's called several times and caused the code to run many times ... need a more professtional way – Mohamed Wagdy Khorshid Oct 12 '12 at 14:57

1 Answers1

0

I need to have access on the tag, which is set after the constructor, so please I need to know the best practice for doing something like this

If you set the tag after the constructor is called then your code will not work, the tag will be null. If you plan to use the custom LinearLayout only in code(not in the xml layout) then you always have the possibility of adapting your constructor to take an extra MyTag object:

// ...
public MyLayout(Context context, MyTag theTag) {
    super(context);
    MyTag tag = theTag;
//...

Of course this will work if you can build a valid MyTag object at the moment the constructor is called(when you instantiate a new object of type MyLayout). Anyway, if your code is so dependent on the MyTag object you should always have a default one in the MyLayout class so you can use it when the tag hasn't been set yet.

user
  • 86,916
  • 18
  • 197
  • 190
  • Dear, that's the implementation I'm running now, but need a more clean and organized way, I need not to fill the constructor with other business .. and btw if you have an example for inflating and xml instead will be cool – Mohamed Wagdy Khorshid Oct 12 '12 at 14:48
  • @Smartdog There is no other way. You're basically trying to use a field of a class in the constructor before it was initialized. `getTag` will return `null` in the constructor because the tag isn't yet set and it will be null until you use `setTag` or pass it directly in the constructor(like in my answer). In the xml layout, the only possible value for a tag is a `String` object, so your custom object will not work. – user Oct 12 '12 at 14:53
  • I know it well and that's why I'm trying to put my code in another function so that the setTag() will be called later in my code ... as I mentioned in the other comment, I tried the onLayout method but it's called regularly by android framework which will not be nice, if you have another method that is called once before inflating, it will be perfect method for me – Mohamed Wagdy Khorshid Oct 12 '12 at 15:22