Search code examples
javaandroidandroid-activityandroid-view

How to transfer data between an Activity and a View class?


I want to transfer 2 float values and 1 boolean value from my MainActivity class to MyCanvas class (which is a class extends View)? Is this possible?

I know this a newbie question, but everything that I found told to use Intent and Bundle, or to use only Bundle.setArguments(), but apparently, none of them work for a View class.

Thank you.

EDIT 1

This is my MainActivity

public class MainActivity extends AppCompatActivity {

    private MyCanvas myCanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myCanvas = (MyCanvas) findViewById(R.id.canvas);
    }

    public void btnCalcularOnClick(View v) {

        TextView xResultado = (TextView) findViewById(R.id.xResultado);
        TextView yResultado = (TextView) findViewById(R.id.yResultado);

        EditText txtX = (EditText) findViewById(R.id.txtX);
        EditText txtY = (EditText) findViewById(R.id.txtY);

        //Comeco da Matematica

        float x = Float.parseFloat(txtX.getText().toString());
        float y = Float.parseFloat(txtY.getText().toString());

        float xResult = 5 * x;
        float yResult = 35 * y;

        boolean buttonState = true;
    }
}

MyCanvas class is like that

public class MyCanvas extends View {
    Paint myPaint;

    public MyCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        myPaint = new Paint();
    }

    @Override
    public void onDraw(Canvas myCanvas) {
        super.onDraw(myCanvas);
        myPaint.setColor(Color.BLACK);
        myPaint.setStrokeWidth(3);

        float cx, cy;
        boolean buttonState2;
    }
}

In this case, I want to transfer:

  • xResult (MainActivity) -> cx (MyCanvas)
  • yResult (MainActivity) -> cy (MyCanvas)
  • buttonState (Main Activity) -> buttonState2 (myCanvas)

Solution

  • You can use setters to set the desired values in your MyCanvas class.

    Create methods in your MyCanvas class like this.

    public class MyCanvas extends View {
    
        private float cx, cy;
        private boolean buttonState2;
    
        ...
    
        public void setResults(float xResult, float yResult) {
            cx = xResult;
            cy = yResult;
        }
    
        public void setButtonState(boolean state) {
            buttonState2 = state;
        }
    }
    

    Then in your activity class

    public class MainActivity extends AppCompatActivity {
    
        private MyCanvas myCanvas;
    
        ...
    
        public void btnCalcularOnClick(View v){
    
            TextView xResultado = (TextView)findViewById(R.id.xResultado);
            TextView yResultado = (TextView)findViewById(R.id.yResultado);
    
            EditText txtX= (EditText)findViewById(R.id.txtX);
            EditText txtY= (EditText)findViewById(R.id.txtY);
    
            //Comeco da Matematica
    
            float x = Float.parseFloat(txtX.getText().toString());
            float y = Float.parseFloat(txtY.getText().toString());
    
            float xResult = 5 * x;
            float yResult = 35 * y;
    
            boolean buttonState = true
    
            myCanvas.setResults(xResult, yResult);
            myCanvas.setButtonState(buttonState);
        }
    }