Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which constructor to be called for view?

Tags:

I have my custom view extended from View. There are 3 view constructors:

  1. View(Context context, AttributeSet attrs, int defStyle)
  2. View(Context context, AttributeSet attrs)
  3. View(Context context)

From my activity I call std.setContentView(R.layout.main). The second constructor is getting called in my view. Why the second one? How to know in advance which one will be called and why?

like image 473
kompotFX Avatar asked Nov 13 '11 18:11

kompotFX


People also ask

How do you call a constructor class?

Constructor chaining can be done in two ways: Within same class: It can be done using this() keyword for constructors in the same class. From base class: by using super() keyword to call the constructor from the base class.

Can a constructor be called directly?

No, you cannot call a constructor from a method. The only place from which you can invoke constructors using “this()” or, “super()” is the first line of another constructor. If you try to invoke constructors explicitly elsewhere, a compile time error will be generated.

How do I call a parameterized constructor from another class in C#?

To call one constructor from another within the same class (for the same object instance), C# uses a colon followed by the this keyword, followed by the parameter list on the callee constructor's declaration. In this case, the constructor that takes all three parameters calls the constructor that takes two parameters.

What is constructor explain with example?

A constructor is a special type of member function that is called automatically when an object is created. In C++, a constructor has the same name as that of the class and it does not have a return type. For example, class Wall { public: // create a constructor Wall() { // code } };


1 Answers

From the Android developer site under documentation for View:

public View (Context context)

Simple constructor to use when creating a view from code.

So this constructor is what you can use to create a View in Java. It will not be called when you inflate from XML.

public View (Context context, AttributeSet attrs)

Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file. This version uses a default style of 0, so the only attribute values applied are those in the Context's Theme and the given AttributeSet.

The method onFinishInflate() will be called after all children have been added.

So this constructor will be called when you inflate a View from XML when you don't specify a style.

public View (Context context, AttributeSet attrs, int defStyle)

Perform inflation from XML and apply a class-specific base style. This constructor of View allows subclasses to use their own base style when they are inflating. For example, a Button class's constructor would call this version of the super class constructor and supply R.attr.buttonStyle for defStyle; this allows the theme's button style to modify all of the base view attributes (in particular its background) as well as the Button class's attributes.

You should implement all of these constructors, but you can put all of the work in the third one by calling this(context, null) and this(context, attrs, 0) for the first two, respectively.

like image 126
skynet Avatar answered Sep 20 '22 13:09

skynet