Android Add UI Elements to a CustomView class - android

I have created a CustomView class, a class that extends View.
In this view I would like to be able to add UI elements like a Linear Layout with Buttons, TextViews etc.
How should I do this in the most efficient way?

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

setContentView as a Class That Extends SurfaceView

I need to set my content view as a class that extends SurfaceView but when I do so, I can't access the ImageViews, TextViews, etc. defined in my main.xml file. If I tried getting an ImageView for example, it'll be null. So how can I have the elements defined in my main.xml layout in my SurfaceView class?
SurfaceView is a View not a ViewGroup. This means you cannot have children for it in traditional way. You need to draw the children yourself.
This example gives you more details on how to use SurfaceView: http://www.edu4java.com/en/androidgame/androidgame2.html

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

inserting a view inside of a view in android

how do you place a view inside of a view in android xml? I have come from a web development background so the closest example I can think of is placing div's inside of div's, something like this for example
You can't place Views inside Views (i.e. ImageView inside an ImageView).
You can place Views inside ViewGroups (i.e. an ImageView inside a LinearLayout).
Please check here for more info: http://developer.android.com/guide/topics/ui/index.html and here: http://developer.android.com/resources/tutorials/views/index.html
Hope this helps!
You can't place views inside another view.
A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties
some viewgroups are
AbsoluteLayout, AdapterView, FragmentBreadCrumbs, FrameLayout, GridLayout, LinearLayout, RelativeLayout, SlidingDrawer, Toolbar, TvView, CardView, etc

Custom layout manager

I'm planning to build my own custom layout manager.
If there is any other way to build one apart from extending the View class, because this is the only approach I know for now and would like to know if there is any other way to do it?
LinearLayout extends the ViewGroup class
http://developer.android.com/reference/android/widget/LinearLayout.html
TableLayout extends LinearLayout
http://developer.android.com/reference/android/widget/TableLayout.html
I assume the source of both these classes are available at https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/.
I would have had a look at the classes above and seen if I can extends LinearLayout or if I should extends ViewGroup.

Categories

Resources