Im trying to invalidate my canvas when i click on a button. I have one layout with the buttons and under i have a canvas view. When i click on a button the circle should hide or be shown. In the code now I can invalidate my canvas only one time. When i press the button the first time it works. But when i press the second time it doesn’t work. Example: If i press hide, then show it works. But when i press the hide button again it doesn’t work. When I click on a button I want the CanvasView to invalidate everytime. Not only the first time.
public class CanvasWithButtonsActivity extends Activity {
boolean showCircle = true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout());
findViewById(R.id.buttonHide).setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
//HIDE
showCircle = false;
//How do i invalidate my canvas from here?
CanvasView cv = new CanvasView(getApplicationContext());
cv.invalidate();
}
});
findViewById(R.id.buttonShow).setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
//SHOW
showCircle = true;
//How do i invalidate my canvas from here?
CanvasView cv = new CanvasView(getApplicationContext());
cv.invalidate();
}
});
}
public RelativeLayout layout(){
RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT, 1));
mainLayout.setBackgroundColor(Color.WHITE);
View buttonLayout = LayoutInflater.from(getBaseContext()).inflate(R.layout.main, null);
buttonLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT, 1));
CanvasView cv = new CanvasView(getApplicationContext());
mainLayout.addView(cv);
mainLayout.addView(buttonLayout);
return mainLayout;
}
private class CanvasView extends View{
public CanvasView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas myCanvas){
Paint myPaint = new Paint();
myPaint.setColor(Color.BLUE);
if(showCircle == true)
myCanvas.drawCircle(myCanvas.getWidth()/2, 100, 20, myPaint);
}
}
}
If someone have a solution to this question it would solve many of my problems.
You seem to be creating a new canvas view instance in your button handler
You probably want to keep a reference to it, don’t you?
EDIT:
in your layout() method you create a view instance. cv here is a reference to it. it’s local for your method:
you want to make it global (i. e. class field). Basically a Java instance variable (like your boolean “showCircle ” flag). You can use it from your listeners code, the same way you change the boolean “showCircle” flag.
Also I’d recommend reading more about creating layouts using XML and locating views using findViewById method.
EDIT2: If you instance variable your code will look something like this. (Possible errors I was typing in a notepad):