Drawing button on canvas

Problems with Canvas, OpenGL, etc...

Drawing button on canvas

Postby emil1 » Wed Jul 07, 2010 2:10 pm

Hello,

I must draw Button ( or any component View ) on canvas.
Anyone know, it is posible and what i must do??

I trying solution:
mybutton.draw(canvas) in override method onDraw but my button isn't drawn
emil1
Freshman
Freshman
 
Posts: 5
Joined: Wed Jul 07, 2010 1:58 pm

Top

Re: Drawing button on canvas

Postby ognian » Wed Jul 14, 2010 6:35 am

Hi,

Your Button must have passed measurement and layout before drawing. Just call measure() and layout() methods before draw().
ognian
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jul 14, 2010 6:28 am

Re: Drawing button on canvas

Postby emil1 » Wed Jul 14, 2010 3:02 pm

Thanks for your Replay but this solution doesn't work. Maybe I doing something wrong.
This is my code:
Code: Select all
public class Game extends View {
   Button _button;
   
   public Game(Context context) {
      super(context);
   
      _button = new Button(context);      
      _button.measure(50, 50);
      _button.layout(20,20,20,20);
}
   @Override
   protected void onDraw(Canvas canvas) {;
      _button.draw(canvas);
      postInvalidate();
   }

}
emil1
Freshman
Freshman
 
Posts: 5
Joined: Wed Jul 07, 2010 1:58 pm

Re: Drawing button on canvas

Postby ognian » Fri Jul 16, 2010 8:31 pm

Hi,

You missed to read the documentation how to use the measure() call. It takes arguments, generated by MeasureSpec (http://developer.android.com/reference/android/view/View.MeasureSpec.html).

Also, you shouldn't call layout() with hardcoded values, use getMeasuredWidth() and getMeasuredHeight() instead.

Edit: Performance tip:
calling postInvalidate() from onDraw() is unnecessary overhead. A simple invalidate() or it's variants with dirty rectangle will be better.
ognian
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jul 14, 2010 6:28 am

Re: Drawing button on canvas

Postby emil1 » Sat Jul 17, 2010 1:11 pm

thanks, its work great:)
Now I have another trouble, i can't change/set position my button.
I trying this method but doesn't work, my button stay on position (0,0):
Code: Select all
int width = _button.getMeasuredWidth();
int height = _button.getMeasuredHeight();
int left = 60;
int top = 60;
      
_button.layout(left, top, left + width, top + height);
emil1
Freshman
Freshman
 
Posts: 5
Joined: Wed Jul 07, 2010 1:58 pm

Re: Drawing button on canvas

Postby ognian » Sat Jul 17, 2010 7:14 pm

I'm glad it helped!

Offset the Button by setting some padding before layout():
http://developer.android.com/reference/ ... ,%20int%29
ognian
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jul 14, 2010 6:28 am

Top

Re: Drawing button on canvas

Postby Nits » Sat Dec 11, 2010 8:08 pm

Hello

this helped me. got succeeded in creating button on canvas but it is not supporting click event.

plz tell me
Nits
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Sep 13, 2010 10:27 pm

Re: Drawing button on canvas

Postby timothymilla » Tue Dec 21, 2010 6:22 am

hey, has there been a solution to registering button clicks for buttons made in java code for canvas/custom views? Cause I'm having this same problem and I've really hit a wall in app development :/
timothymilla
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Dec 21, 2010 6:19 am

Re: Drawing button on canvas

Postby vidp » Fri May 06, 2011 11:40 am

Dear All,
I'm trying to the same thing as timothymilla did.
But when I call setPadding(150, 100, 0, 0) only the text in button locate, I really need locate the button at the center of the canvas. How can I do?
Thanks for your help.
vidp
Freshman
Freshman
 
Posts: 4
Joined: Fri May 06, 2011 11:33 am

Re: Drawing button on canvas

Postby pbdeguzman29 » Fri May 20, 2011 11:23 am

I'm also experiencing the same problem as above. Please teach us how to set the button position on canvan. tnx!
pbdeguzman29
Once Poster
Once Poster
 
Posts: 1
Joined: Fri May 20, 2011 11:17 am

Re: Drawing button on canvas

Postby olivier6501 » Fri Jun 24, 2011 2:24 pm

Hi all,

I know this post is outdated but if some of you are still searching how to draw a view in a canvas setting a margin, here is a solution :

Code: Select all
           //We use a layout to contain the button (or any view you want to draw)
           LinearLayout ll = new LinearLayout(getApplicationContext());
           ll.setOrientation(LinearLayout.VERTICAL);

           //We set the layout parameters
           LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

           //SET THE MARGIN HERE
           layoutParams.setMargins(150, 150, 0, 0);

           //Declare a new view (here a button)
           Button button=new Button(getApplicationContext());
           button.setText("some text");
          
           //Add it to our linear layout
           ll.addView(button, layoutParams);
          
           //Measure and layout the linear layout before drawing it
           ll.measure(MeasureSpec.getSize(ll.getMeasuredWidth()), MeasureSpec.getSize(ll.getMeasuredHeight()));
           ll.layout(0, 0, MeasureSpec.getSize(button.getMeasuredWidth()), MeasureSpec.getSize(button.getMeasuredHeight()));
           //Finally draw the linear layout on the canvas
           ll.draw(canvas);


As you can see margin is not set directly in the button view because it doesn't support margins operations (only ViewGroup subclasses do : http://developer.android.com/reference/android/view/View.html).
Hope that'll help !
olivier6501
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jun 24, 2011 2:12 pm

Re: Drawing button on canvas

Postby sampath_sl » Wed Feb 22, 2012 12:30 pm

Sorry for reviewing an old thread. But I feel that the topic is not properly discussed and clearly understood. I've used the techniques described in the previous posts along with a SurfaceView, but was unable to render any standard controls including buttons or text boxes. I think this could not be done since the canvas is meant for pixel based drawing while controls are not. Described here: http://developer.android.com/guide/topics/graphics/2d-graphics.html

or... have I got everything terribly wrong? :oops:
sampath_sl
Developer
Developer
 
Posts: 35
Joined: Sun Apr 11, 2010 4:45 pm
Location: Colombo

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: Google [Bot] and 3 guests