How to a add button to view - android

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?

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.

Related

Add custom Views to a View without extending layout?

I have a class that extends View and allows me to plot data. I want to make another class that extends View that contains two of these plotting views. In my particular case, I am unable to extend a layout such as LinearLayout and add views to it. My parent class has to extend View. Is it possible to build a View that consists of other custom Views without extending a layout?
Here's your answer. You can create a Custom Compound View.
http://code.tutsplus.com/tutorials/creating-compound-views-on-android--cms-22889

How to add an external View to a HorizontalScrollView in main activity

Is it possible to add to a horizontal ScrollView a View which has been created separately in the main Activity?
I don't have reputation to comment but each View Class has addView() function where you can add the view dynamically. So it should be possible as long as you have the view which you want to add.

How can I add a layout to my customized view

I have a class that extends view and in it I am creating my customized view. At certain step I want add a layout created here in tis question to my customized view.
I also referred to some examples but but they seem have the same problem reversed, they have a layout and want to add a customized layout to it.
How can I add a layout to my customized view?
Maybe your class should extends ViewGroup instead of View.
With a ViewGroup you will be able to use addView() to add inflated layout in your custom view.
For more info on addView take a look at Android doc

Difference between View and Subview - Android

What is the difference between a view and a subview in Android?
there is no such thing called a 'subview', its just used to refer to a view inside another view.
View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). and if we insert a view inside the another the its become Subview like a linear Layout containing a button view, here button is a subview
Like Vinay said, there is no such a thing. But you have ViewGroup that contains other Views. For example, LinearLayout, RelativeLayout etc are derived from that class.
If you wish, you can read more about it here:
http://developer.android.com/reference/android/view/ViewGroup.html

Is it possible to place an imageView in a class that extends View?

Is it possible to place an imageView in a class that extends View in android?
If possible please give me a sample code.
Thanks.
If you want to create a custom view that contains an ImageView (and presumably some other items), you probably just want to extend a ViewGroup, such as a RelativeLayout rather than directly extending View.
See the "Compound Controls" section of the Custom Components page on the Android Developers' site.

Categories

Resources