Ondraw method not calling in custom view

I am making custom view using custom ViewGroup and custom View in Android.

public class Custom_ViewGroup extends ViewGroup
{
public Custom_ViewGroup(Context context)
{
    super(context);
    addView(new OwnView(context));
}

public Custom_ViewGroup(Context context,AttributeSet attrs) 
{
    super(context, attrs);
    addView(new OwnView(context));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
    // TODO Auto-generated method stub
}

class OwnView extends View
{
    public OwnView(Context context)
    {
        super(context);
        System.out.println("on constructor");
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        System.out.println("ondraw child");
    }
  }
}

onDraw() method of OwnView class is not calling. constructor of OwnView class called. I have used invalidate() method after adding view, but it did not work.

Answers


This is your problem

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
    // TODO Auto-generated method stub
}

Your view is never laid-out, so it will not draw. You need to implement the onLayout method properly. Also, if your ViewGroup contains only a single view, consider using FrameLayout instead of ViewGroup.


Need Your Help

How do I create an X509 Certificate in PHP without the clients private key?

php cryptography certificate openssl

I have a server which has its own cert and a client who is trying to have the server generate a new cert for him (and sign it ofc). The client has given the server his public key and the server is

Add default value for unknown

android sql sqlite

I have a list of users and a list of vaccines, I'd like to keep track of which vaccines a user has so I have a vaccine_users table as well. I want to display a list of checkboxes in a view, name of