erik
erik

Reputation: 4958

addView order not working

I am adding a cameraView, a glSurfaceView, and an ImageView to a relativeLayout in that order, but the result has them ordered from top bottom to top (cameraView, imageView, glSurfaceView) any specific reason? is it because the glSurfaceView redraws it's self internally?

code snippit for the adding:

rel.addView(glView);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,1);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params.rightMargin = 20;
        // add Camera to view 
        rel.addView(camPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        rel.addView(glView);
        rel.addView(imageView,params);

question really is.. how can i assure the imageView is always on top

I thought that the GLSurfaceView's onDrawFrame method was bringing the GlSurfaceView to the top of the view list, so i added a call back to a public method on the mainActivity from the onDrawFrame Method that looks like this:

public void ajustOverlay(){
        imageView.bringToFront();
        Log.v("parent: ", "ajust overlay");
    } 

That still doesn't seem to keep the imageView on top

Upvotes: 0

Views: 1794

Answers (3)

erik
erik

Reputation: 4958

Ok after trying the solutions above and various other solutions, and banging my head against concrete for almost a year now, i ifnally have it working.. the key was to ad the following line before adding the glView

glView.setZOrderMediaOverlay(true);
rel.addView(camPreview, camParams);
rel.addView(glView, glParams);
rel.addView(imageView, imageParams);

Upvotes: 0

jdynamite
jdynamite

Reputation: 41

I ran into the same ordering problem trying to place three regular LinearLayout objects side by side in a parent RelativeLayout. To correct the problem, you simply need to add one more parameter when you call the addView method. It is the second parameter of three and it represents the index of the order you want the views to be added.

So this should work for you:

rel.addView(camPreview, 0, camParams);
rel.addView(glView, 1, glParams);
rel.addView(imageView,2, imageParams);

Upvotes: 2

eyal-lezmy
eyal-lezmy

Reputation: 7116

You're adding your views without enough rules for a RelativeLayout, that's maybe the reason why the result is not what you expect. For example the RelativeLayout.BELOW rule have to be defined to be sure that a view is below another one.

First, you should try to build your layout in XML so you will be able to look at the result on Eclipse. And then apply programmatically all the rules that are inside your file or building your XML hierarchy easily thanks to a LayoutInflater. I really advise you the LayoutInflater solution if you can, for many reasons (maintenance, reformating, multiscreen handling, ...).

You should also read the complete documentation about RelativeLayout to format it correctly : here

Upvotes: 2

Related Questions