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

Issue with an API calls Windows Server

asp.net web-services iis-7 windows-server-2008 amazon-web-services

I wrote a web app that makes API calls to a web service. This works fine on the development server and when I run the app on my Windows 7 machine. I can't get this to run on Windows Server 2008. I ...

Correct and elegant formatting/syntax of a Javascript snippet

javascript jquery syntax

The question I would like to ask is if the Javascript snippet below is well formatted and structered in terms of Javascript/jQuery syntax, readability and speed.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.