Search code examples
androidandroid-canvasandroid-custom-view

Custom View with animation,color and border containing inner textview


Referring to below image, I want that view with yellow background and right border to slide in from right with some text. So this requirements include:

  • creating a custom view (which will need canvas I guess)
  • giving it width, height, position, color, border, border-color, etc.
  • animation to slide from right
  • textview within this custom view to display text

I referred this tutorial to understand how to include view with a canvas in layout. But instead of hard coded point positions, I tried following:

public MyView(Context context) {
    super(context);
        WindowManager wm = (WindowManager) DashBoardActivity.mContext
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        width = size.x;
        height = size.y;

        width -= 10;
        height -= 50;
        myPath = new Pt[6];
        myPath[0] = new Pt(width, height);
        myPath[1] = new Pt(width - 400, height);
        myPath[2] = new Pt(width - 500, height - 100);
        myPath[3] = new Pt(width - 400, height - 200);
        myPath[4] = new Pt(width, height - 200);
        myPath[5] = new Pt(width, height);
}

But view is not getting positioned as per the expectation. I tried with different hard-coded positions, but either it's getting displayed at wrong position or not getting displayed at all.

Also, to show the text, I added textview in the layout itself but it is getting displayed at top left of the screen instead of the position shown in below image.

How I can achieve this ? Any suggestions appreciated.

Image


Solution

  • You dont need any custom view for this, you can set shape or 9patch image as background to the textview and animate that textview using object animator. Border in shape xml