Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7868539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:02:11+00:00 2026-06-03T01:02:11+00:00

Im trying to invalidate my canvas when i click on a button. I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:02:13+00:00Added an answer on June 3, 2026 at 1:02 am

    You seem to be creating a new canvas view instance in your button handler

    CanvasView cv = new CanvasView(getApplicationContext());
    cv.invalidate();
    

    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:

    CanvasView cv = new CanvasView(getApplicationContext());
    mainLayout.addView(cv);
    mainLayout.addView(buttonLayout);
    

    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):

    public class CanvasWithButtonsActivity extends Activity {
        boolean showCircle = true;
        private CanvasView mCanvasView;
        /** 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?
                        if (mCanvasView != null) {
                            mCanvasView.invalidate();
                        }
                    }  
            });
    
            findViewById(R.id.buttonShow).setOnClickListener(
                    new OnClickListener() {      
                    public void onClick(View v) {
                        //SHOW
                        showCircle = true;
                        //How do i invalidate my canvas from here?
                        if (mCanvasView != null) {
                            mCanvasView.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));
            mCanvasView = new CanvasView(getApplicationContext());
            mainLayout.addView(mCanvasView);
            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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to draw a complex view, defined by an xml layout
Trying to figure out how to do this. I have the style but I'd
I've a servlet app deployed in side oc4j. I am trying to invalidate the
i am trying to setVisible a play icon on click of listview item just
Hi I am trying to draw one line graph and for that I am
How can invalidate focus from parent frame? I have a JFrame ( parentFrame ),
I'm trying to create a animated custom view in Android but I'm having trouble
I'm trying to have an image scale to a certain size depending on the
I am trying to display a polygon on a mapview in android. I have
I am trying to perform basic task of rotating a canvas 20 times a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.