tags:

views:

86

answers:

1

Hello, How can I draw an dynamic view on top of mainscreen in android.

For example I display the main screen using setContentView(R.layout.main); This draws many widgets and includes an image defined in main.xml this way :

<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sinewave"/>

Now if I would like to draw rectangle on this image, how can I do it? Thank you for your time.

A: 

To draw something on screen instead of using a pre-defined Widget:

You create a custom view class.

public class yourCustomView extends View {
}

You add that custom view to your xml layout.

<view class="your.package.path.yourCustomView"
    android:id="@+id/myView"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    /**you can add other xml attributes that are defined for View**/
/>

You override the onDraw() method to make it draw stuff you want on its canvas.

@Override
protected void onDraw(Canvas canvas) {
    /**draw stuff here**/
}
FreewheelNat