Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer data between Views in a Layout

Tags:

java

android

view

I haven't beeen able to find the answer to the following question:

How do I get data from View A to View B, with View A and View B in the same LinearLayout? Is this even possible? Do I need to start working with threads?

I haven't been able to get the correct search phrase I guess, I'm probably not the first person that wants to do this, but I can't find it :(

Below is what I use now to create the views. In the TargetTrainer (which extends View) I'm letting the user give some input, and I'd like to be able to give feedback to the user in the TextView. How would I for instance show the coordinates of the onTouchEvent of TargetTrainer in the TextView?

Below is a clipped/simplified version of my program.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    LinearLayout linear;
    linear = new LinearLayout(this);

    linear.setOrientation(LinearLayout.VERTICAL);
    TextView text = new TextView(this);
    text.setText("Test");
    linear.addView(text);

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

    TargetTrainer t = new TargetTrainer(this, width, height);
    linear.addView(t);
    setContentView(linear);

}
like image 708
Jelle Veraa Avatar asked Feb 03 '26 21:02

Jelle Veraa


1 Answers

As I can see from the snippet, you already pass Context in the constructor new TargetTrainer(this, width, height). Assuming that the code you provided is from activity called BaseActivity create reference to BaseActivity in the TargetTrainer constructor and call the update method from TargetTrainer.

public TargetTrainer extends View {

    ....
    BaseActivity mBaseActivity = null;


    public MyView(Context context, int width, int height) {
    ....
    mBaseActivity = (BaseACtivity)context;
    ....        
    }

    ....

    private void update(String text)
    {
        mBaseActivity.updateTextView(text);
    }
}

In BaseActivity create updateTextView:

public void updateTextView(String updateText){ 
    text.setText(updateText);
}
like image 126
youri Avatar answered Feb 05 '26 13:02

youri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!