0

I am developing a game, and I have a class that extended View(Not a xml file). I want to add Button to my custom view, But my extended view has not addView method. How can I do this?

1 Answers1

0

Your class has to extend ViewGroup, not View.

If you want to keep your class extend View you have the possibility to create an object which extends the ViewGroup (for example: LinearLayout, RelativeLayout...). Then put your costum view and the button into this ViewGroup.

TheOnlyJakobob
  • 541
  • 3
  • 14
  • If I do that, Does any problem occur? –  Jun 27 '14 at 07:57
  • i guess not, because a ViewGroup extends the class View. So you can use the same methods like in View – TheOnlyJakobob Jun 27 '14 at 07:59
  • ViewGroup is abstract, and you will be forced to override onLayout to assign a position to every child of your ViewGroup – Blackbelt Jun 27 '14 at 08:03
  • how can I give cordinate to the button? –  Jun 27 '14 at 08:03
  • googling is a simple solution. if your sdk is greater than 10 you can use button.setX / setY. if not you can use: button.getLayoutParams().topMargin = XXX. but u need an relative layout – TheOnlyJakobob Jun 27 '14 at 08:06
  • I should I use onLayout like this object.onLayout(true,0,0,Screen width,ScreenHeight); ??? –  Jun 27 '14 at 08:08
  • google and trying out helps more: http://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically – TheOnlyJakobob Jun 27 '14 at 08:10
  • Could I have your email address or messenger id to more help? cause i'm developing game! –  Jun 27 '14 at 08:12
  • you dont need my help. the internetm stackoverflow and the android documentations offer you a better and faster help – TheOnlyJakobob Jun 27 '14 at 08:20