0

I'm designing my own tab control. I've created 2 custom views. The first is called MyTab and derives from LinearLayout. The second is called MyTabBar and derives from LinearLayout.

The constructor of both look something like this:

public MyTab(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.layout_my_tab, this);
}

MyTabBar has a member variable which is an ArrayList of MyTabs. I have a public function called addTab(String strTabName) to add a new MyTab to the MyTabBar. My question is, when I do the following:

MyTab tab = new MyTab(...);

inside of MyTabBar, how do I obtain the Context and AttributeSet required to be passed to MyTab?

Or should I be inflating it somehow?

2
  • you shouldn't inflate in a view's constructor, i don't think this is what you want to do Commented Sep 26, 2013 at 20:01
  • the inflater.inflate method returns a view that you are not using, you are not storing it and you can't return it in a constructor, you are confusing inflation and construction Commented Sep 26, 2013 at 20:04

1 Answer 1

2

The view's constructor with a AttributeSet parameter is used only when you inflate the view from an xml file, otherwise write a constructor having only a Context parameter, and use super(context); to get the context from MyTabBar, use this.getContext();

Sign up to request clarification or add additional context in comments.

Comments

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.