Adding a view to a viewgroup

by Carl » Tue, 07 Jul 2009 20:31:13 GMT

Sponsored Links
 'm trying to do some simple UI stuff in a game (Basketball).

I've created my own Court class (which extends ViewGroup) and I want
to add a Ball (extends View) to the court.

Unfortunately my ball doesn't show up on the court, and in fact the
court doesn't draw either.

What have I done wrong?


import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class test extends Activity
/** Constants */
final int FPAR = LinearLayout.LayoutParams.FILL_PARENT;

private FrameLayout main;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
this.addContentView(new Court(this), new LayoutParams(FPAR,


import android.content.Context;
import android.view.ViewGroup;

public class Court extends ViewGroup
// court dimensions
private int height = 0;
private int width = 0;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

// ball
Ball ball;

public Court(Context context)

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

// fix the sizes here
height = this.getMeasuredHeight();
width = this.getMeasuredWidth();
this.layout(0, 0, width, height);

// create a new ball
ball = new Ball(this, 100, 100, 20, Color.RED);

// put a ball on the court

public void onDraw(Canvas canvas)
Rect rect = new Rect(this.getLeft(), this.getTop(), width,
canvas.drawRect(rect, paint);
// tell the ball to draw itself

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


import android.view.MotionEvent;
import android.view.View;

public class Ball extends View
private int x;
private int y;
private int r;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

public Ball(Court court, int x, int y, int r, int color)
this.x = x;
this.y = y;
this.r = r;
this.layout(this.x, this.y, this.x + (r * 2), this.y + (2 * r));

private OnTouchListener ballTouchListener = new OnTouchListener()

Adding a view to a viewgroup

by Jack Ha » Wed, 08 Jul 2009 00:17:29 GMT

 our Court.onDraw() function will not get called by default for
efficiency. You need to call setWillNotDraw(false) to enable it.

Jack Ha
Open Source Development Center
obilestick together

The views, opinions and statements in this email are those of
the author solely in their individual capacity, and do not
necessarily represent those of T-Mobile USA, Inc.

On Jul 7, 5:30am, Carl <> wrote:

Sponsored Links

Adding a view to a viewgroup

by Carl » Wed, 08 Jul 2009 06:33:35 GMT

 xcellent, thanks.

On 7 July, 17:17, Jack Ha <> wrote:

Adding a view to a viewgroup

by Romain Guy » Wed, 08 Jul 2009 06:48:35 GMT

 lso, you should not call  this.layout(0, 0, width, height) from
onMeasure(), it will be done for you.

On Tue, Jul 7, 2009 at 3:33 PM, Carl<> wrote:

Romain Guy
Android framework engineer

Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them


Other Threads

1. Node.setNodeValue(String) not setting value


Did anyone manage to successfully set a node's value ?

Considering a Document "d":

returns "xpto_text"



returns "xpto_text"

...while it is a type 3 (TEXT) node.

Am I doing something wrong here ?

Thanks in advance,

Filipe Azevedo


2. Correct Cookie handling for form-based authentication

Hi guys,

currently I'm developing an app that logs into a website using form
based auth. Before I implemented that using the internal Http client,
I have been using the HttpUrlConnection, but due that fact that my
software didn't work in 2G networks I've changed my implementation to
use the internal http client.

Does anyone of you know about an OpenSource App that does the same
that I want? Like logging into a website (getting and setting Cookies)
and so on? If yes, I could have a look at that. I mean the thing is...
I need to find out whether the login worked or not.

Or does anyone know how it works correctly? Maybe some of you guys
have already implemented something like that?

This is my current implementation that does not work:

In the httpHandler() I set the cookies as member variable
(setCookiesFromHeader() and also put them into the header if they
exist (prepareConnection).

It would be really great if someone could help me. Really would like
to release the version of my app, since a lot more things have
changed. And this issue keeps me stuck... :(

I also wasn't able to a "good" tutorial about Cookie Handling. :(

Thank you very much in advance, guys.



3. Android Software Engineer

4. Questions about the accessibility API

5. v1.6 sdk bit fluky!

6. Problem compiling drivers/switch/switch_gpio.c in arm platform

7. webview javascript call from ajax resp java func