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
Related
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
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?
I am creating a custom view which extends RelativeLayout. I inflate a layout which I created using xml and contains a single element. Afterwards I add a non fixed amount of other child views to the layout in an init method called from the constructor. I need to have access to the layout_width parameter from the MainActivity xml file, as I arrange the children views in a circle with an offset which should differ according to the width given on the MainActivity xml.
I can't seem to be able to get the width given on the XML via parameter layout_width.
OnMeasure seems to get it but it is called later.
In simpler words, I want to place the children views differently depending on the width of the 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
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.