StartingGroovy
StartingGroovy

Reputation: 2860

How does one update a TextView's text in Android?

I have a View in Android that has an onTouchEvent which stores the x and y values. In my Activity I have two TextViews that I initially set to 0 but would like to update them every time the View is clicked.

Can someone explain how to do this?

Edit

My Activity display 3 different Views and one of the Views has an onTouchEvent to see what the x and y position was if it was clicked.

public class MyActivity extend Activity {
    //LinearLayouts created to hold the Views


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //add buttons 
        for(Button b : myButtons) {
            buttonLayout.addView(b);
        }

        clickLayout.addView(clickView);    //ClickView is where I use the onTouchEvent and want to get the values from

        for(TextView tv : myTextViews) {
            //This is where I would like to set the text
            //It needs to update every time the ClickView's onTouchEvent happens
            textLayout.addView(tv);
        }
    }
}

If my ClickView source is necessary, let me know and I'll throw it up there. However, it just gets the X and Y.

The option that Dan S mentioned (passing textviews) is an option, however I was hoping there was a way my Activity could get ahold of the ClickView's onTouchEvent.

If there isn't an alternative way to those mentioned, I'll go ahead and mark this as solved since those aforementioned options do work, just slightly complicate the way I was working with it.

Upvotes: 1

Views: 231

Answers (2)

Squonk
Squonk

Reputation: 48871

Do something like the following...

public class MyActivity extends Activity
    implements View.OnTouchListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ...

        // Set clickView's OnTouchListener to be handled
        // by the onTouch(...) method of this Activity
        clickView.setOnTouchListener(this);
    }

    @Override
    public onTouch(View v, MotionEvent event) {
        // The View parameter v will be the View that
        // was touched if you have more than one to handle
    }
}

Upvotes: 1

Dan S
Dan S

Reputation: 9189

in onTouchEvent use this pseudo code

textViewX.setText(String.valueOf(motionEvent.getX()));
textViewY.setText(String.valueOf(motionEvent.getY()));

Upvotes: 1

Related Questions