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
Related
I am trying to build a soundboard app that plays a sound whenever you tap on a custom view. In order to organize my UI, I used a layout file.
When I extended View, I found that when I tried to inflate my layout, it wouldn't work.
//Didn't work
View v = View.inflate(context, R.layout.sound_view_layout, null);
When I extended LinearLayout, however, it did work.
//Worked
View v = View.inflate(context, R.layout.sound_view_layout, this);
My question is,
how come I needed to extend a Layout class in order for the program to work, and what are the benefits of doing so?
Well you can only inflate a layout. If you want to just create a View, use new CustomView(context).
Ignoring that- the main reason to extend a layout is if you want to add children to it programmatically, or if its a collection of views already (for example you may want to create a view CaptionedPhoto that has an image on top of text. One way to do this would be to make CaptionedPhoto extend LinearLayout and hold an image view and a text view).
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
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
I am working on creating a custom view in android. I want to create an autohide custom view control.
This control will be holding other UI elements mostly buttons or icons. It has a small button which is mandatory, clicking which will slide the control in or out thus changing its visibility.
one should be able to add other buttons or icons to this control
The control can be placed only at the borders, which needs to be specified while creating the view.
I don't know how to start with it
Should I be extending the View class or ViewGroup class.
have a look at this
and then you have to add a dynamic layout to this drawer
I used a RelativeLayout and added a Button to the View.
When I call expandView() or collapseView(), I call mybutton.setVisibility() and let RelativeLayout know it has changed with this.requestLayout().
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.