Search code examples
javaandroidtextviewros

How to change Textview text in Activity from class that extends RelativeLayout


In my MainActivity I have one TextView. Now, inside other class that extend Relative Layout, I have implemented one MessageListener that (using onNewMessage method) recieve every second some values. I want to put this values inside my TextView that are in MainActivity. This is a simply example of my class:

public class VirtualJoystickView extends RelativeLayout implements
    MessageListener<nav_msgs.Odometry>{

 public VirtualJoystickView(Context context) {
    super(context);
    initVirtualJoystick(context);

    topicName = "/pepper/cmd_vel";
  }

  public VirtualJoystickView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initVirtualJoystick(context);
    topicName = "/pepper/cmd_vel";
  }

  public VirtualJoystickView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    topicName = "/pepper/cmd_vel";
  }

  @Override
  public void onNewMessage(final nav_msgs.Odometry message) {
    //I need to put this value inside TextView that are in MainActivity 
    double w = message.getPose().getPose().getOrientation().getW();
    double x = message.getPose().getPose().getOrientation().getX();
    double y = message.getPose().getPose().getOrientation().getZ();
    double z = message.getPose().getPose().getOrientation().getY();
}

Thanks for help.


Solution

  • If you call this Class in your MainActivity class, so you can add to your constructor another value which will be your Textview

    TextView textview;
    
    public VirtualJoystickView(Context context, TextView textview) {
    super(context);
    initVirtualJoystick(context);
    
    topicName = "/pepper/cmd_vel";
    
    this.textview = textview;
    }
    

    Now you can change it's text by using

    textview.setText("some text");