How to inflate a custom view class?

I have a class that extends viewthat defines a custom drawing (resistor). I want to click a button and add viewto main layout. so I see a resistor, and if I click again, it will add another resistor and so on. but I don’t know how best to approach this problem. I looked at a lot of issues related to layoutinflater, but none of them inflate a custom view class (maybe I was looking for the wrong thing) is always a file xml. So my question is: how do I add a few ResistorViewsto my layout so that the user can interact with them (move, delete, select, etc.)?

Here is what I tried:

Action 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>

Thank.

: * . *

+5
3

( Android) XML. , . , , . , , , , . :

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);                 
        }
    });
  }    
}
+4

, private Context context; context=this; . Variable onclick

bAddResistor.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {               
     final ResistorView resistor = new ResistorView(context, 100, 100);
     mLayout.addView(resistor);
    }
});
+2

You can refer to the "this" of the outer class by adding its name. Suppose you have a class A inside which you define a class B. Inside B, “this” refers to B, but you can also type “A.this” and get A this.

Hope this helps.

Shahar

0
source

All Articles