Android - Set a view to be on top of items drawn with canvas

george picture george · May 28, 2012 · Viewed 12.2k times · Source

I have an android app where the user paints, moves and reshapes some objects over a photo. In this page my screen layout consists of the photo that is loaded and below it (in portrait view) some buttons. My view looks exactly as I want it with the xml below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/linear" >

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_alignParentBottom="true">   

            <ImageView
                android:id="@+id/draw"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:clickable="true"
                android:src="@drawable/draw"/>

            <ImageView
                android:id="@+id/delete"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:clickable="true"
                android:layout_toRightOf="@+id/erase"
                android:src="@drawable/delete"/>

            <ImageView
                android:id="@+id/done"
                android:layout_width="80dp"
                android:clickable="true"
                android:layout_height="50dp"
                android:layout_toRightOf="@+id/delete"          
                android:src="@drawable/done"/>         

        </LinearLayout>     



    <ImageView
        android:id="@+id/photo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_above="@id/buttons"/>


</RelativeLayout>

The problem is that I want the buttons to be always on top of the painted objects. Now if the user paints a line and then moves it in a way that one edge will go lower than the image, then this line will be over the buttons. It will be untouchable at that point because the canvas is set to the bitmap that has my image, but it will be visible. I would like it to disappear in the way it disappears if part of the line gets out of screen.

How can I implement this? Is there some attribute that can ensure that these buttons are always over the painted objects? Thank you in advance!

Answer

Harshal Kshatriya picture Harshal Kshatriya · Jun 15, 2012

If you've @Override onDraw or onLayout method, try bringing the

<LinearLayout
    android:id="@+id/buttons">

in the front by using v.bringToFront() where v will be your LinearLayout.