title: How do you set the size and position of a ViewGroup that is inside a RelativeLayout? (All Java, no XML)
body: <p>I've been trying to understand how to draw graphics on Android using a hierarchy of custom views, custom viewgroups, and relativeLayouts. I'm having a hard time figuring out how to have the parents tell the children where to position themselves and at what size. I think it is working when the custom ViewGroup tells its children to be a certain size, but I can't figure out how to have the relativeLayout tell its ViewGroup children what size and position to be. Right now, the following code draws a circle in the top and center of the screen. (This is Android 3.0 on the Xoom). The size of the circle is 295x295, so I'm assuming this is somehow the default size and position of a layout? Can anyone give me the code that would be needed so that I could programatically pick the size and location of the viewgroup that contains the view that contains the circle? The edges of the circle on the four sides are getting trimmed off just so, so I know that the relativelayout is cutting off the rest of the viewgroup from printing. If I have the ViewGroup measure() the View at a smaller size, it will print it at that size up in the left corner of that same rectangle in the top center of the screen.</p>
  
  <p>My Activity's onCreate:</p>
  
  <pre><code> public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      CustomViewGroup custVG = new CustomViewGroup(this);
      RelativeLayout rel = new RelativeLayout(getApplicationContext());
      rel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  
      rel.addView(custVG);
  
      RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,  
                      LayoutParams.WRAP_CONTENT);  
      params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  
      rel.addView(new CustomViewGroup(getApplicationContext()),1,
              new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
  
      setContentView(rel);
  
  }
  </code></pre>
  
  <p>My Custom made ViewGroup:</p>
  
  <pre><code>CustomView cust;
  int _height;
  int _width;
  
  public CustomViewGroup(Context context) {
      super(context);
      cust = new CustomView(context);
  
      Canvas canvas = new Canvas();
  
      this.addView(cust,new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
  }
  
  @Override 
  public void onDraw(Canvas canvas){
      this.drawChild(canvas,cust,0);
  }
  
  
  
  
  @Override
  protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
  
      cust.layout(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());
  }
  
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
  
      //Measure the only child
      this.measureChild(cust,widthMeasureSpec, heightMeasureSpec);
  
      _height = View.MeasureSpec.getSize(heightMeasureSpec);
      _width = View.MeasureSpec.getSize(widthMeasureSpec);
      setMeasuredDimension(_height, _width);
  }
  </code></pre>
  
  <p>My Custom View</p>
  
  <pre><code>public CustomView(Context context){
      super(context);
  
  }
  
  @Override
  public void onDraw(Canvas canvas) {
  
      float noteRadius = _width/2;
  
      Paint paint = new Paint();
      paint.setAntiAlias(true);
      paint.setColor(Color.YELLOW);
      paint.setStyle(Paint.Style.STROKE);
      float strokeWidth = paint.getStrokeWidth();
      paint.setStrokeWidth(strokeWidth);
  
      canvas.drawCircle(_height/2, _width/2, noteRadius, paint);
  
  }
  
     @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  
          _height = View.MeasureSpec.getSize(heightMeasureSpec);
          _width = View.MeasureSpec.getSize(widthMeasureSpec);
          Log.i("CustomView onMeasure","Width: " + _width + " Height: " + _height);
  
          setMeasuredDimension(_height, _width);
      }
  </code></pre>
  
  <p>Sorry this is long. Thanks!</p>