I'm developing an application in android studio. I currently have two parts, a MainActivity and a DrawingView. In the DrawingView is both a Canvas and a Button.
<com.jerem.simpledraw.DrawingView
android:id="@+id/DrawingView"
android:layout_width="wrap_content"
android:layout_height="554dp"
android:background="#ffffff"
android:paddingBottom="40dp"
android:paddingLeft="20dp"
app:exampleColor="#33b5e5"
app:exampleDimension="24sp"
app:exampleDrawable="@android:drawable/ic_menu_add"
app:exampleString="Hello, DrawingView" />
<Button
android:text="Refresh"
android:layout_height="wrap_content"
android:id="@+id/buttonRefresh"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:onClick="refreshCallback"/>
Above is the layout file for the drawing view. I want the button, when pressed, to call the method "refreshCallback", which is in the class DrawingView.
However, this warns me that it cannot find the method in the MainActivity.
I want to call a method in the DrawingView, not in the Main Activity. How can I fix this?
EDIT: I am updating to include the entire DrawingView class. I want to refresh the page.
package com.jerem.simpledraw;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class DrawingView extends View
{
//Constant
private int Colour = 0xFF000000;
private int screen_width;
private int screen_height;
//Drawing Var
private Canvas drawCanvas;
private Bitmap canvasBitmap;
private Path drawPath;
private Paint drawPaint;
private Paint canvasPaint;
public DrawingView(Context context, AttributeSet attributeSet){
super(context, attributeSet);
setupDrawing();
setupUI();
}
private void setupUI() {
}
private void setupDrawing() {
drawPath = new Path();
drawPaint = new Paint();
drawPaint.setColor(Colour);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(20);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
private void handelAction(int action, int x, int y){
switch(action) {
case 0:
drawPath.moveTo(x, y);
break;
case 2:
drawPath.lineTo(x, y);
break;
case 1:
drawCanvas.drawPath(drawPath, drawPaint);
break;
}
{
invalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event){
float x = event.getX();
float y = event.getY();
System.out.println(event.getAction());
handelAction(event.getAction(), (int) x, (int) y);
return true;
}
public void refreshCallback(View v){
System.out.println("Refresh Command Sent");
this.setupDrawing();
}
}