Android GLSurfaceView as subview - android

I'm very new to an Android platform and currently trying figure out why things work this way:
If I create GLSurfaceView and add it using the setContentView or addContentView to an activity everything works fine (Renderer.onDrawFrame is called as expected). However if I add my GLSurfaceView to another ViewGroup (which is then added using setContentView) nothing is rendered.
So what is the difference between addView and addContentView ?
In the end I need to create a custom view with OpenGL rendering in background and some controls on top.
Also what is the point of separating View and ViewGroup? Why not to join them (like it is done in CocoaTouch) ?

setContentView(View) sets the parent View for an Activity (or dialog etc...).
addView(View) adds a View to a ViewGroup.
View and ViewGroup are mostly different. View is a more specific single entity. It should be used more on one facet of what you are trying to do. ViewGroup, however, focuses more on the whole and acts (is) a container and layout organizer for a collection of Views.
Can you post all the code related to the GLSurfaceView and how you are setting it as contentView?

Related

Is it possible to enable touch events for a View inside a ViewGroup but outside its bounds?

I'm currently trying to design a ViewGroup (specifically a subclass of FrameLayout) that can layout any number of subviews and enable them for drag/drop outside the layout group. It's almost identical to a LinearLayout:
Curently I am able to drag the views outside the ViewGroup and draw them, however after letting go of the view it can't be re-selected and further dragged around.
I'm wondering if there is a way to do this in a way that isolates the logic to the Layout subclass and doesn't involve needing to do much/any extra stuff in consuming fragments/view groups.
I've tried overriding getHitRect(Rect) in my FrameLayout subclass but it never seems to be called. dispatchTouchEvent(MotionEvent) is of course not called either, presumably because the parent ViewGroup has decided not to deliver the touch to it because it was outside the bounds. I've tried implementing a TouchDelegate as well, but I think that needs to be set on the parent view, which means needing to know about this and doing this additional step when using this Layout.
Any ideas on if/how this is possible? On iOS it can be implemented fairly easily by overriding hitTest: to take into account the frames of the child views. Is there a similar method like this on Android?
Your help is greatly appreciated!
Without any code it is very hard to see what you are doing, but best guess would be that each view should a child view of the relative layout.

What are the benefits of extending LinearLayout over View?

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).

Android : drawing on activity with other views

I am new to android and trying to make something like the fingerlock security system of the android. The problem is that I have found a lot of reference on drawing onto a plain background (that sets different content view and allows drawing onto that), but not regarding drawing onto the same activity layout.
To draw manually on a View, you need to override its onDraw method. For this you need a custom View (often extending from an existing View type).
If you are using an xml to set the Activity's layout in setContentView() then you need to add the above custom view to this xml. Then, you can draw onto the same activity layout.
setContentView is noting but the activity view. once the view is set in main activity (max in oncreate method) it can update from multiple classes according to your logics.. i think before trying this you have to understand the view and its hierarchic properties properly

Custom viewgroup with animated view

I made a custom viewgroup, and I need to use view animation to rotate some views...
But they are misbehaving, no matter if I put the startanimation inside onDraw, onLayout or onMeasure, my views flicker (you see them briefly rotated, and then briefly non-rotated, and that pattern repeats).
How I fix that?
see Android transfer animation of view in custom viewgroup and Animating Views
view.animate().rotation(180);
Does your animations work properly in a non-custom viewgroup?
I tried to use a custom ViewGroup in order to create an animated background, however, just like you, I had some strange behaviour. I solved it by simply using a FrameLayout and inserted two fill_parent views: a custom one and a LinearLayout. Then the LinearLayout which came on top of my custom view was working as expected.

Android: Show custom UI View in Eclipse layout viewer

Does anyone know how I can get my custom view object to appear in Eclipse's Android layout tool when I put the parameters in my layout file?
If you don't want to put any code here, just a link to a resource would be great too. I'm not having any luck searching for what I want.
Thanks.
Having your view displayed in the Layout View in Eclipse is not straight forward. From what I've seen (it's not exhaustive at all), using a completely custom View / Surface View with the rendering placed in a separete thread does not work.
But if you're building a custom View based on an existing one (for example inheriting from an EditTExt) and only overriding the onDraw method, then the layout manager will display your view as intended. The the parameters inherited from the Android View (here EditText) will apply to your custom View. Custom parameters won't have effects (I think).

Categories

Resources