Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drawing on a view (or surface view, or anything really) within a layout

 
Zhiming Shi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done many tutorials (in my quest to find the answer to this question) so I'm basically familiar with how to draw basic shapes as well as bitmap images on a view using a canvas as well as on a surface view. The problem is, every tutorial I find is the same - it tells me to set content view to the view (or surface view).



But the whole point is that I want to draw on a view within a greater layout, so that my buttons, textboxes, etc are all visible next to my surface view (or other view within my main view) and still be able to draw on my view.

How do I do this?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the same(ish) way as doing as a content. A couple of things to thing about:
1) Make sure the view you intend to draw on has a predefined size. If not, it may (will likely) be shrunk to 0px wide and 0px tall.
2) You may find it easiest to position things with a RelativeLayout to avoid unwanted resizing
3a) You may find it easier to make the layout with the surface view in it, then get it out of the view using findViewById()
3b) Alternatively, you should put a placeholder FrameLayout where you want the surface view, then get the FrameLayout using findViewById() and adding your surface view to the frame

If you are having problems, show the layout, a simple version of the code, and a screenshot of the results, and details about the difference between expectations and results so we can help you with the issue.
 
Zhiming Shi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As you can see, in the above code the tutorial says to change the content view to myView, but I want the content view to be the main layout.

The problem is, once I've changed the content view to the main layout, I have no idea how to "connect" that MyView class to my View, which is called "viewMap". I just want the drawing that is happening in the myview class to happen to my view object, "viewMap".

When I figure out how to do that, I'll take your advice on the other things, Steve Luke, thanks!

 
Zhiming Shi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made a small breakthrough, but every time I click the button my app crashes.




At the very least I've "connected" my View to the MyView class in a way that seems to be basically correct...I just don't know what I'm missing? How can I get it to draw on the view and not crash when the button is pressed?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zhiming Shi wrote:I've made a small breakthrough, but every time I click the button my app crashes.
What error message do you get in LogCat when the app crash. LogCat should have the stack trace which will pinpoint where the error occurs and why.
 
Zhiming Shi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aigh, I'm a bit of a noob when it comes to Android programming. My app relies on GPS to function so I find it faster to just debug by installing it on my phone each time and then running it rather than using the emulator. My emulator is extremely slow and I'm not sure how to get the GPS and saving to SD card things to work properly when using the emulator. Using my phone, however, I don't know how to get the logcat information.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing you need to do is enter Developer Mode (this is typically done in the Settings menu but varies by Android version and model so search to find out how to do it). then turn on USB debugging. When debugging is on, run the app from your IDE using the phone, then change to the LogCat view.
 
Zhiming Shi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is great, I can't believe I didn't know about it!
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic