Rafael Sanchez
Rafael Sanchez

Reputation: 414

Pass data to custom View class

I want to pass some data to a custom class that extends android.view.View. However, I get a warning message saying that :

Custom view LinePlot is missing constructor used by tools: (Context) or (Context,AttributeSet) or (Context,AttributeSet,int)

However, I run the code and all seems to work smoothly.

Thanks!

import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

import java.util.ArrayList;

public class LinePlot extends View {

    private ArrayList<Float> mPoints;
    private int dx;
    private int dy;

    Paint paint=new Paint();

    public LinePlot(Context context,int dx_plot, int dy_plot, ArrayList<Float> points) {

        super(context);
        mPoints=points;
        dx=dx_plot;
        dy=dy_plot;
    }


@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    // plotting my data here
}  


}

Upvotes: 6

Views: 9578

Answers (4)

niranjan_b
niranjan_b

Reputation: 142

so a couple of steps have to followed here -

(i) in values/attrs.xml add the following code -

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NinjaView">
       <attr name="addButtonBackgroundDrawable" format="integer"/>
    </declare-styleable>
</resources>

(ii) call your custom view in you layout.xml file. For e.g. -

<com.example.act.ui.utils.NinjaView
                        android:id="@+id/ninja_view_product_desc"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        custom:addButtonBackgroundDrawable="@drawable/add_button_back_about_product"/>

(Don't forget to declare your custom namespace : xmlns:custom="http://schemas.android.com/apk/res-auto")

(iii) In your custom view's class you'll have to add a couple of things. Example -

public class NinjaView extends LinearLayout {

@BindView(R.id.button_add_to_cart_product) Button mAddToCart;

public NinjaView(Context context) {
    super(context);
    initializeNinjaView(context);
}

public NinjaView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeNinjaView(context);
    setAddButtonBack(context, attrs);
}

public NinjaView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initializeNinjaView(context);
    setAddButtonBack(context, attrs);
}

private void initializeNinjaView(Context context) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.ninja_view, this);
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    ButterKnife.bind(this);
}

private void setAddButtonBack(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NinjaView, 0, 0);
    Drawable backgroundDrawable;
    try {
        backgroundDrawable = typedArray.getDrawable(R.styleable.NinjaView_addButtonBackgroundDrawable);
    } finally {
        typedArray.recycle();
    }
    if (backgroundDrawable != null) {
        (findViewById(R.id.button_add_to_cart_product)).setBackground(backgroundDrawable);
    }
}

Upvotes: 5

AmmY
AmmY

Reputation: 1859

you need to add constructors with parameters (Context), (Context,AttributeSet) and (Context,AttributeSet,int)

Upvotes: 0

Nowshad
Nowshad

Reputation: 294

In your LinePlot class add constructors:

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

Upvotes: 0

Alexander
Alexander

Reputation: 48232

The warning means you won't be able to use your custom view in any xml layout.

If you are not intended to do that it's still good to implement those constructors for your custom view like this:

CustomView(Context ctx) {
   super(ctx)
}

Any additional attributes are normally passed as custom attributes not as constructor parameters. Read on custom view attribytes in the docs or elsewhere http://www.tutorialspoint.com/android/create_custom_attributes_for_custom_component.htm

Upvotes: 1

Related Questions