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 8146425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:59:35+00:00 2026-06-06T13:59:35+00:00

I am trying to click a button and add a resistor. So what I

  • 0

I am trying to click a button and add a resistor. So what I need is to invalidate the view when the button is clicked. But the invalidate that is inside MyView inside the method update() is not working. I have been trying to search for this problem but I have found nothing similar to what I am trying to do, or maybe this is not the way to do it.

DefaultActivity.java

public class DefaulActivity extends Activity {       

  MyView myView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);         

    setContentView(R.layout.main);
    myView = new MyView(this, null); 

    final Button bAddResistor = (Button) findViewById(R.id.bAdd);
    bAddResistor.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click              
            myView.update();                            
            Log.d("ButtonADD", "Button Add has been clicked");
        }
    });          
  } 
}

MyView.java

public class MyView extends SurfaceView implements SurfaceHolder.Callback{      


  Resistor myResistor;
  private ArrayList<Resistor> mElements = new ArrayList<Resistor>();


  public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);  
    getHolder().addCallback(this);  

  } 

  @Override
  protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    synchronized (mElements) {
        for (Resistor element : mElements) {
            element.doDraw(canvas);                
        }
    }   

  }

  public void update() {

       mElements.add(new Resistor(getContext(), (int) 10, (int) 10));
       invalidate(); //Does not work!
  }


  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {

    Canvas c = holder.lockCanvas();
    onDraw(c);      
    holder.unlockCanvasAndPost(c);      
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub  

  } 

}

Resistor.java

public class Resistor extends View{

  private Path mSymbol;
  private Paint mPaint;

  private int mX;
  private int mY;

  //...Override Constructors...    
  public Resistor(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public Resistor(Context context, int x, int y){
    super(context);
    mX = x;
    mY = y;
    init();
  }

  private void init() {
    mSymbol = new Path();
    mPaint = new Paint();

    mPaint.setAntiAlias(true);      
    mPaint.setStrokeWidth(2);
    mPaint.setColor(-7829368);
    mPaint.setStyle(Paint.Style.STROKE);

    //...Your code here to set up the path,
    //...allocate objects here, never in the drawing code.

    mSymbol.moveTo(0.0F, 0.0F);
    mSymbol.lineTo(0.0F, 50.0F);
    mSymbol.lineTo(16.666666F, 58.333332F);
    mSymbol.lineTo(-16.666666F, 75.0F);
    mSymbol.lineTo(16.666666F, 91.666664F);
    mSymbol.lineTo(-16.666666F, 108.33333F);
    mSymbol.lineTo(16.666666F, 124.99999F);
    mSymbol.lineTo(-16.666666F, 141.66666F);
    mSymbol.lineTo(0.0F, 150.0F);
    mSymbol.lineTo(0.0F, 200.0F);
    mSymbol.offset(mX, mY);

  }

  public void doDraw(Canvas canvas) {
    canvas.drawPath(mSymbol, mPaint);
  }
  • 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-06T13:59:36+00:00Added an answer on June 6, 2026 at 1:59 pm

    You should use a ViewGroup instead of a SurfaceView. Add the Resistor-Views as children to MyView, measure and layout them appropriately and the ViewGroup will automatically take care of drawing them.

    SurfaceView requires you to do your drawing in a seperate thread. You have to start that thread in surfaceCreated and keep redrawing the view as needed. For simple views like the one you posted it is perfectly acceptable to just do your drawing in the UI thread. No SurfaceView required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I was trying to do is that when I click the Add button
I am trying to click a button with Perl Mechanize, but there are some
I am trying to modify the script below to click a button that looks
I'm essentially trying to set it up so that when I click a button
I am trying to add a click event on the element that is return
I am trying to add elements from a textbox on a button click to
When I click on the Add button, the browser (chrome) crashed. I am trying
I am trying to add a radio button click event at runtime. Radiobutton button
I am trying to use some action when I click button in navigation controller.
I am trying to reset the UIPickerView on button click. My pickerview id is

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.