syaak
syaak

Reputation: 11

Android Canvas from external class will not show in Fragment

Im making an android app, and I have created a class with a canvas in it to draw a rectangle. I want to call that class from another (its a fragement) and show the rectangle. Here is what I have:

This is my RectShape class:

public class RectShape extends View{

public RectShape(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    Rect ourRect = new Rect();
    ourRect.set(3, 0, canvas.getWidth() - 3, 150);

    Paint postColor = new Paint();
    postColor.setColor(Color.WHITE);
    postColor.setStyle(Paint.Style.FILL);

    canvas.drawRect(ourRect, postColor);
    }
}

This is my fragment class:

public class FragmentFeed extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View V = inflater.inflate(R.layout.fragment_feed_layout, container, false);



    RectShape shape = new RectShape(getActivity());



    return V;

    }
}

Im not getting any errors.. the shape is just not showing up.. Is there anything wrong with it? I dont want to call the shape from within the same class.

Upvotes: 1

Views: 107

Answers (1)

Manish
Manish

Reputation: 1215

You have to add RectShape shape = new RectShape(getActivity()); to the fragment. Currently you are just initialising it. Add the view to fragament and it will display.

Upvotes: 1

Related Questions