0gravity
0gravity

Reputation: 2762

How to inflate a custom view class?

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.*

Upvotes: 5

Views: 10021

Answers (3)

kabuko
kabuko

Reputation: 36302

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

Upvotes: 4

Shachar Shemesh
Shachar Shemesh

Reputation: 8563

You can refer to "this" of an external class by adding its name. Suppose you have 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's this.

Hope this helps.

Shachar

Upvotes: 0

Gautam
Gautam

Reputation: 7958

Save the context as a private instance variable like private Context context; and Init the context like context=this; in the constructor of the activity. The add the custom view using the context Variable in the onclick callback method

bAddResistor.setOnClickListener(new View.OnClickListener() {

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

Upvotes: 2

Related Questions