I have a class that extends View and have the onDraw method implemented in my xml file I have a FrameLayout with my view and a RelativeLayout with a button
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<canvas.pruebas.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<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"
>
<Button
android:id="@+id/btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="15sp"
/>
</RelativeLayout>
</FrameLayout>
in my Activity class I want to implement when I click the button redraw the canvas in MyView class
public class CanvasActivity extends Activity {
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//redraw canvas
}
});
}
}
How Can I do that?
use invalidate().
First change your xml slightly to add an id:
<canvas.pruebas.MyView android:id="@+id/mycanvasview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
then in code,
...
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get your view
canvas.pruebas.MyView myCanvas = (canvas.pruebas.MyView) findViewById(R.id.mycanvasview);
//redraw canvas
myCanvas.invalidate();
}
});