Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • What is happening?
  • How should I pass my data to my class?
  • Why can't I use a custom constructor?

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
}  


}
like image 749
Rafael Sanchez Avatar asked Oct 18 '22 09:10

Rafael Sanchez


1 Answers

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);
    }
}
like image 174
niranjan_b Avatar answered Nov 15 '22 05:11

niranjan_b