tags:

views:

54

answers:

2

If I wanted to reference an int from another class how would I go about doing that?

public class Zoom extends View {
    private Drawable image;
    public int zoomControler = 20;

    public Zoom(Context context) {
        super(context);
        image=context.getResources().getDrawable(R.drawable.icon);
        setFocusable(true);      
    }

    @Override            
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        image.setBounds((getWidth ()/2)-zoomControler,
                        (getHeight()/2)-zoomControler,
                        (getWidth ()/2)+zoomControler,
                        (getHeight()/2)+zoomControler);
        image.draw(canvas);
    }
}

class HelloOnTouchListener implements OnTouchListener{
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        return true;
    }
}

In this case I want to reference the zoomControler from the first class in the second HelloOnTouchListener class.

+5  A: 

You need to create a getter in the first class something like.

public int getZoomController()
{
    return zoomController;
}

And in your listener it would be.

((Zoom) arg0).getZoomController();
jsmith
thanks!!!! jsmith it works like a charm
+1  A: 

While @jsmith's answer is the recommended approach, the fact that the zoomControler (sic) attribute is public means that you can also do this:

int z = ((Zoom) arg0).zoomControler;

or even

((Zoom) arg0).zoomControler = z;

However, accessing attributes like this is bad style, and even exposing the attributes is bad style. You should probably change zoomControler to private so that other classes have to access it via getters and setters.

Stephen C