Skip to content Skip to sidebar Skip to footer

Pass Value From Activity To Custom View

I need to pass a value from my main Activity to a custom View. In the main activity I have a SensorEventListener so I'm continuosly listening to the light sensor. In the onSensorC

Solution 1:

You could do this:

public CustomView extendsView {
  ...
  privatefloat[] values; //this //setterpublicvoidsetValues(float[] values) {
    this.values = values;
  }

}


publicclassMyActivityextendsActivityimplementsSensorEventListener {

 private CustomView mCustomView;
 ...
    @OverridepublicvoidonSensorChanged(SensorEvent event) {
        float[] values = event.values;
        mCustomView.setValues(values);    //pass the collected values to the view via setter
    }
}

Solution 2:

Without seeing any of your code, the best advice I can give is to create a property in your view, and make it accessible from your Main Activity class. In your method that is checking the sensor, you can simply set the custom View's property that you created to be the value. Not so much passing the value as directly accessing it.

Assuming your value is a float, add something like this to your Custom view class:

publicfloat sensorValue;

Access it from the sensor event listener like this:

CustomView.sensorValue = sensorValue;

Post a Comment for "Pass Value From Activity To Custom View"