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

  • Home
  • SEARCH
  • 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 8196793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:35:58+00:00 2026-06-07T05:35:58+00:00

I have a class that extends view , that defines a custom drawing (a

  • 0

I have a class that extends view, that defines a custom drawing (a resistor). I want to click a button and add that view to the main layout.
so that I see the resistor, and if I click again it will add another resistor and so on.
but I don’t know the best way to approach this problem. I have looked at a lot of questions referring to layoutinflater, but none of them inflate a custom view class (maybe I am looking for the wrong thing), is always a xml file. So my question is: How can I add multiple ResistorViews to my layout, so that the user can interface (move, delete,highlight, etc) with them?

This is what I have tried:

Activity Class:

public class CircuitSolverActivity extends Activity {       

@Override    
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button bAddResistor = (Button) findViewById(R.id.bAdd);        
    final LinearLayout mLayout = (LinearLayout)findViewById(R.layout.main);
    final ResistorView mResistor = new ResistorView(this, 100, 100);
    bAddResistor.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {               

            mLayout.addView(mResistor);                 

        }
    });
  }    

}

ResistorView class:

public class ResistorView extends View{

    private Path mSymbol;
    private Paint mPaint;

    int mX, mY;

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

    public ResistorView(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);       

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

    }

    @Override
    protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);
    canvas.drawPath(mSymbol, mPaint);       
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"        
    android:orientation="vertical"
    android:id="@+id/main">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/bAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add RES" />


</LinearLayout>

Thanks.

EDIT: SOLVED *Thanks again for the help.*

  • 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-07T05:35:59+00:00Added an answer on June 7, 2026 at 5:35 am

    Inflating (in terms of Android views) is strictly for XML. If you’re dynamically creating and adding a view object in code, then that’s not inflating. What you’re doing in your code right now is pretty close. The only problem being that you instantiate the view once, whereas it sounds like you want to add a new one every time you click. Try moving the instantiation into the click handler:

    public class CircuitSolverActivity extends Activity {       
    
    @Override    
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        final Button bAddResistor = (Button) findViewById(R.id.bAdd);        
        final LinearLayout mLayout = (LinearLayout)findViewById(R.id.main);
        bAddResistor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {               
                final ResistorView mResistor = new ResistorView(CircuitSolverActivity.this, 100, 100);
                mLayout.addView(mResistor);                 
            }
        });
      }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom-made view that extends the View class. I would like 2
I have a class that extends View. I override the onDraw method and allow
I have a class that extends android.app.Dialog, the layout is done in an xml
I have a class that extends AsyncTask , which fetches the gps cordinates from
I have a class that extends Application. The class is fairly extensive. When the
i have a class that extends another. when i iterate the current object i
I have a class that extends AsyncTask. In the doInBackground() method, I connect to
I have a class that extends JPanel. On the panel I have added an
In my Android app, I have a class that extends Thread that runs when
I am facing a problem. I have a Class that extends listactivity and its

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.