Button on top of a View with Canvas

Asked

Viewed 110 times

0

I’ve been trying for some time to put a Button on top of a View I’m using with Canvas. I looked in several places (this seems to be a common problem) but found no answer.

When I try to run the program below, it gives the error:

java.lang.ClassCastException: android.view.View cannot be cast to com.examples.danilofernandes.canvasonrelativelayout.RandomShapeView

Any idea how to do what I need?

Thank you very much

public class MyActivity extends Activity {

private RandomShapeView mDrawingArea;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    mDrawingArea = (RandomShapeView) findViewById(R.id.drawing_area);
    mDrawingArea.setBackgroundColor(Color.GREEN);

}

}

Other class

public class RandomShapeView extends View {

Paint paint;

public RandomShapeView(Context context) {
    super(context);

    paint = new Paint();
    paint.setColor(Color.BLACK);

}

@Override
protected void onDraw(Canvas canvas) {

}

}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<Button
    android:id="@+id/button"
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<View
    class="com.examples.danilofernandes.canvasonrelativelayout.RandomShapeView"
    android:id="@+id/drawing_area"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>


</RelativeLayout>

1 answer

1


Exchange the <View> for <com.examples.danilofernandes.canvasonrelativelayout.RandomShapeView> in the XML file of your layout.

  • Thanks, it worked. Just needed to implement a constructor, which is exactly what was said in this topic. http://stackoverflow.com/questions/8352249/error-in-creation-of-custom-view

Browser other questions tagged

You are not signed in. Login or sign up in order to post.