Changes to TextView being ignored after touch event

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Changes to TextView being ignored after touch event

Postby illiniwatcher » Mon Nov 10, 2008 8:43 pm

Folks, I hope you can help me. In the code segment below, for a new project, I'm trying to figure out how to trap a touch event and send the (x,y) coordinate to two TextViews on the screen.

The trouble is, nothing changes on either of them.

Any ideas?

Thanks,
IlliniWatcher

***************************

MYTOUCH1.JAVA:


package my.touch1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;


public class Touch1 extends Activity {

public TextView txt1;
public TextView txt2;

public Float xtouch;
public Float ytouch;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

txt1 = (TextView) findViewById(R.id.TextView01);
txt2 = (TextView) findViewById(R.id.TextView02);

}


public boolean onTouchEvent(MotionEvent event) {

xtouch = event.getX();
ytouch = event.getY();

txt1.setText(xtouch.toString());
txt1.setText("Dummy test");

txt1.invalidate(); // this should have worked

return true;

}


}


****************************

MAIN.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start"></TextView>
<TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start"></TextView>
</LinearLayout>
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Top

Postby MrSnowflake » Tue Nov 11, 2008 11:11 am

THis is what the docs of the onTouchEvent() says:
"Called when a touch screen event was not handled by any of the views under it. This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it.
Parameters
event The touch screen event being processed.
Returns
Return true if you have consumed the event, false if you haven't. The default implementation always returns false."
So I guess, the linlayout handles the touch event.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby illiniwatcher » Tue Nov 11, 2008 6:26 pm

MrSnowflake wrote:THis is what the docs of the onTouchEvent() says:
"Called when a touch screen event was not handled by any of the views under it. This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it.
Parameters
event The touch screen event being processed.
Returns
Return true if you have consumed the event, false if you haven't. The default implementation always returns false."
So I guess, the linlayout handles the touch event.


Nice try, but I managed to figure this out for myself. This is what I wound up coding, through trial & error:



package my.touch1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;


public class Touch1 extends Activity {

public TextView txt1;
public TextView txt2;

public Float xtouch;
public Float ytouch;


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

setContentView(R.layout.main);

txt1 = (TextView) findViewById(R.id.TextView01);
txt2 = (TextView) findViewById(R.id.TextView02);

txt1.setText("x-coord here");
txt2.setText("y-coord here");

}


public boolean dispatchTouchEvent(MotionEvent event) {

xtouch = event.getX();
ytouch = event.getY();

txt1.setText(xtouch.toString());
txt2.setText(ytouch.toString());

return true;

}


}


Apparently, I should have used dispatchTouchEvent rather than onTouchEvent, since this was an Activity rather than a View implementing a TouchListener (which turned out to be unnecessary). Also, the call to "invalidate()" wasn't necessary. There are three methods (for different classes, of course) for touch events - onTouch, onTouchEvent, and dispatchTouchEvent. Each are called under slightly different conditions so those learning Android will need to learn what those distinctions are.

Thanks for the help anyway!
illiniwatcher
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Nov 03, 2008 8:09 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 7 guests