-3

I want to make a custom view by extending View class. then I defined all three constructor.(below my custom view code)

public class CustomButton extends Button
{
    public CustomButton(Context context)
    {
        super(context);
    }

    public CustomButton(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public CustomButton(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, R.attr.testStyle);
    }
}

Xml Code :

   <com.example.viewlifecycledemo.CustomButton
    style="@style/test"
    android:layout_width="100dp"
    android:layout_height="100dp" />

Only ViewLifeCycleClass(Context context, AttributeSet attrs) is called, but I applied style, so ViewLifeCycleClass(Context context, AttributeSet attrs, int defStyleAttr) this method should be get call.

can anybody tell me, why two parametrized called?

This link is not working for me : Android Custom View Constructor

Community
  • 1
  • 1
Akashsingla19
  • 662
  • 1
  • 7
  • 17

3 Answers3

0

Your code should look like this:

public class CustomButton extends Button
{
    private LayoutInflater mInflater;
    public CustomButton(Context context)
    {
        super(context);
    }

    public CustomButton(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public CustomButton(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }
}

The issue here is that you are calling R.attr.testStyle in your third custructor, instead of defStyleAttr. Since you are using custom style, your third constructor is running but you are passing the wrong argument on super(). To know which exacly constructor is being called each time, read this answer.

Community
  • 1
  • 1
Menelaos Kotsollaris
  • 4,517
  • 6
  • 50
  • 61
0

Here's what the documentation says about creating a custom view:

When a view is created from an XML layout, all of the attributes in the XML tag are read from the resource bundle and passed into the view's constructor as an AttributeSet. Although it's possible to read values from the AttributeSet directly, doing so has some disadvantages:

*Resource references within attribute values are not resolved

*Styles are not applied

http://developer.android.com/training/custom-views/create-view.html

It sounds like assigning the style in your XML will always result in the 2-parameter constructor being called. I'm not sure how to force the 3-parameter constructor to be called unless you do so explicitly in your code. I don't know if this code will work for you since I haven't tried to instantiate my custom views with the 3 parameter constructor, but maybe part of it will help you?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout yourLayout = (FrameLayout) findViewById(R.id.yourLayoutId);
    CustomButton yourButton = new CustomButton(this, attrs, R.style.yourStyleId);
    yourLayout.addView(yourButton);
    setContentView(yourLayout);
}
Community
  • 1
  • 1
Chamatake-san
  • 541
  • 3
  • 10
0

The answer is that the constructor with three parameters is never called by Android. You can only call it yourself for example in one of the other constructors.

Rafal Roszak
  • 510
  • 4
  • 12