Has anyone found a solution to this? I am struggling, using the same code,
but still the button is always drawn on the top left corner of the parent
view.

please help on this,

regards
Yorgos

On Wed, Jan 19, 2011 at 8:35 AM, sach <sach.le...@gmail.com> wrote:

> I am using a class "BoardView" which extends to View class.In onDraw()
> I want to positioned two buttons with image on them.
> I searched on forum,but not clearly got the answer.Right now I am able
> to draw a button on canvas but can't positioned it on desire
> location.This is code I am using.
>
> public BoardView(Context context) {
>
>                       Bitmap  image
> =((BitmapDrawable)imageDrawable).getBitmap();
>                       Button  undo_Btn = new Button(context);
>                        undo_Btn.measure(150, 75); //size of view
>                        int width = undo_Btn.getMeasuredWidth();
>                        int height = undo_Btn.getMeasuredHeight();
>                        int left = 100;
>                        int top = 100;
>                       undo_Btn.layout(left, top, left + width, top +
> height); //
> position relative to parent
>
>
> undo_Btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon));
>                        undo_Btn.setVisibility(VISIBLE);
>                        undo_Btn.setId(10);
>                        undo_Btn.setPadding(50,250,0,0); //Not getting this
> padding;no
> effect of this
> }
>
> @Override
>                protected void onDraw(Canvas canvas) {
>                        // TODO Auto-generated method stub
>                        super.onDraw(canvas);
>                        Drawable board =
> getResources().getDrawable(R.drawable.board);
>                        Bitmap bitmap2 =
> ((BitmapDrawable)board).getBitmap();
>                        canvas.drawRect(0, 0, getWidth(), getHeight(), new
> Paint());
>                        canvas.drawBitmap(bitmap2, 0, 0, new Paint());
>
>              //Here drawing the button on canvas,it shows on top left
> corner not on desired location
>                        undo_Btn.draw(canvas);
>                    invalidate();
>                }
>
>
> @Override
>                protected void onMeasure(int widthMeasureSpec, int
> heightMeasureSpec) {
>                        // TODO Auto-generated method stub
>                        super.onMeasure(widthMeasureSpec,
> heightMeasureSpec);
>                        setMeasuredDimension(measureWidth(widthMeasureSpec),
>                                        measureHeight(heightMeasureSpec));
>                }
>                 private int measureWidth(int measureSpec){
>                  int preferred = image.getWidth()*3 ;
>                  return getMeasurement(measureSpec, preferred);
>                 }
>
>         private int measureHeight(int measureSpec){
>                 int preferred = image.getHeight()+20 ;
>                 return getMeasurement(measureSpec, preferred);
>          }
>
>         private int getMeasurement(int measureSpec, int preferred){
>            // System.out.println("in getMeasurement");
>                // System.out.println("measureSpec:->"+measureSpec +
> "preferred:->"
> + preferred);
>                 int specSize = MeasureSpec.getSize(measureSpec);
>           //  System.out.println("specSize:->"+specSize);
>                 int measurement = 0;
>                  switch(MeasureSpec.getMode(measureSpec))
>                  {
>                      case MeasureSpec.EXACTLY:
>                          // This means the width of this view has been
> given.
>                          measurement = specSize;
>                          break;
>                      case MeasureSpec.AT_MOST:
>                          // Take the minimum of the preferred size and what
>                         // we were told to be.
>                         measurement = Math.min(preferred, specSize);
>                         break;
>                     default:
>                         measurement = preferred;
>                         break;
>                 }
>
>                 return measurement;
>             }
>
>        }
>
>
> How to place it on desired position on canvas.When we use   Button
> undo_Btn = new Button(context); does this button require any extra to
> set its dimension so that it will get proper view when screen
> dimension changes.?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to