user940016
user940016

Reputation: 2938

Is there a way to make a view draw on top of all other views?

I was wondering if there's a way to make a view draw on top of all other views, even if it belongs to a view group that's covered by other views? Thanks.

Upvotes: 2

Views: 2924

Answers (3)

wayne_bai
wayne_bai

Reputation: 1228

I have same question with you before and now I got the answer from this thread. What is android.widget.FrameLayout

Android will place application to a framelayout(its id is android.R.id.content), we could call it as ContentView. You can see it by 'hierarchyviewer', it's a tool under /android-sdk/tools/ allow to check the view hierarchy. You could see that your application is placed in a FrameLayout.

You could remove any view from its current viewgroup and add to the ContentView.So it will place on top of all other views.

You can do it following:

//Remove view from current viewgroup
WebView mWebView = (WebView)findViewById(id);
ViewGroup currentParent = (ViewGroup)mWebView.getParent();
currentParent.removeView(mWebView);

//Add the view to ContentView(android.R.id.content)
FrameLayout contentView = (FrameLayout)this.getDecorView().findViewById(android.R.id.content);
FrameLayout.LayoutParams fl = new FrameLayout.LayoutParams(MATCH_PARENT,MATCH_PARENT);
contentView.addView(mWebView,fl);

Hope this helpful.

Upvotes: 0

nmw
nmw

Reputation: 6754

Use ViewGroup.bringChildToFront(View child). Though this doesn't have the desired effect in a LinearLayout, as the Z order is directly linked to the sort order, and so in that case it just moves the View to the bottom.

Upvotes: 0

user940016
user940016

Reputation: 2938

I found a solution to my situation, but it still has an unwanted effect of drawing some black pixels around the views that are drawn outside of their bounds. Seems like I'll have to move these views to a parent view that covers the area I need after all... Anyway, here's what I did in case someone wants to try it:

@Override protected void dispatchDraw(Canvas canvas)
{
    Rect clip=new Rect(); getDrawingRect(clip);
    int saveCount=canvas.save(Canvas.CLIP_SAVE_FLAG);
    canvas.clipRect(clip,Region.Op.REPLACE);
    super.dispatchDraw(canvas);
    canvas.restoreToCount(saveCount);
}

The function belongs to the view group that contains the child views I want to draw outside of the original bounds enforced by their parent. The clip area can be changed to anything if you want to use different bounds for the views.

Upvotes: 2

Related Questions