novice
novice

Reputation: 51

passing variable from activity to custom created view

I am trying to pass a String variable from my activity to my custom created view class. I have tried Bundle but it does not seem to work.I want to pass the value of the input of the EditText called 'destination'in my MainActivity to my DrawView class. What I am trying to achieve is depending on the user input in the activity class the DrawView class will draw the shapes in a different position Any help would be appreciated.

This is the code for my activity class

MainActivity.class

public class MainActivity extends ActionBarActivity {
    static EditText destination = (EditText)findViewById(R.id.roomdinput);
    String roomName;
    //DrawView drawView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final Button floorPlan = (Button)findViewById(R.id.floorPlanButton);        



floorPlan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){


    roomName = destination.getText().toString();
    Bundle myb = new Bundle();
    myb.putString("key", roomName);
    Intent a = new Intent(MainActivity.this, DrawView.class);
    a.putExtras(myb);
    startActivity(a);
    startActivity(new Intent("com.example.helloworld3.FLOORPLAN"));



}   

});

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

And this is the code for my DrawView Class

DraView.class

public class DrawView extends View {
    Paint paint = new Paint();
    float ux, dx, rx,lx;
    String roomName2;
   Bundle myb2 ;


    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.RED);
    }

    @Override
    public void onDraw(Canvas canvas) {
            canvas.drawLine(90, 250 , 90, 400, paint);
            canvas.drawLine(20, 0, 0, 20, paint);
            canvas.drawCircle(150, 400, 30, paint);
    }

}

Upvotes: 2

Views: 969

Answers (2)

CROSP
CROSP

Reputation: 4617

As far as you have DrawView extended from View , you can simply use setTag method to pass object to view. Why not to use this useful feature ?

SomeCustomObject obj = new SomeCustomObject();
myDrawView.setTag(obj);

and in view you can get tag using getTag method.

SomeCustomObject = (SomeCustomObject)getTag();

Of course you can declare method or constructor in your DrawView class

Like

public class DrawView extends View {
    SomeCustomObject obj ;
    Paint paint = new Paint();
    float ux, dx, rx,lx;
    String roomName2;
   Bundle myb2 ;
// Custom constructor
public DrawView(Context context,SomeCustomObject object) {
        this(context);
        this.obj = object;
    }

// Custom method
public void setObject(SomeCustomObject object) {
     this.obj = object;
}
    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.RED);
    }

    @Override
    public void onDraw(Canvas canvas) {
            canvas.drawLine(90, 250 , 90, 400, paint);
            canvas.drawLine(20, 0, 0, 20, paint);
            canvas.drawCircle(150, 400, 30, paint);
    }

}

If you have your DrawView in another activity and you want to pass data to it, firstly you should pass it via Intent , than get object from intent in second activity , and after that pass this object to your DrawView. If you need an example, just say.

Upvotes: 3

Bruce
Bruce

Reputation: 8849

Simply call view object and setContentView

drawView = new DrawView(this);
setContentView(drawView);

Additionally if you want to pass parameters pass through creating setter methods in your View

Upvotes: 0

Related Questions