I've been trying for some time already to put a Button on top of a View that I'm using with Canvas. I searched in several places (this seems to be a common problem) but I did not find an 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);
}
}
Another 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>