0

I got a little problem. In the following sample the first TextView has to show the type of MotionEvent-it works fine. The second TextView has to show the coordinates of the MotionEvent-but it doesn't work. I don't know why but maybe its only a small error ? Does anyone has an idea? Thanks for your help! Here is the Code:

package de.androidnewcomer.motionevent;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import static android.R.attr.x;
import static android.R.attr.y;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FrameLayout Spielbereich=(FrameLayout)findViewById(R.id.Spielbereich);
    Spielbereich.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    TextView textView1=(TextView)findViewById(R.id.textView1);
    TextView textView2=(TextView)findViewById(R.id.textView2);
    TextView textView3=(TextView)findViewById(R.id.textView3);
    TextView textView4=(TextView)findViewById(R.id.textView4);
    int x1,x2,y1,y2;
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            x1 = (int)event.getX();
            y1 = (int)event.getY();
            textView1.setText("Action Down");
            textView2.setText(x1,y1);
            return true;
        }
        case MotionEvent.ACTION_UP: {
            x2 = (int)event.getX();
            y2 = (int)event.getY();
            textView3.setText("Action Up");
            textView4.setText(x2,y2);
            return true;
        }
    } return false;
}
}
rcode
  • 49
  • 4

1 Answers1

1

I think u are using setText(...) wrong. In the docs you can See that TextView has the following setText methods:

  • final void setText(int resid) Sets the text to be displayed using a string resource identifier.

  • final void setText(CharSequence text) Sets the text to be displayed.

  • void setText(CharSequence text, TextView.BufferType type) Sets the text to be displayed and the TextView.BufferType.

  • final void setText(int resid, TextView.BufferType type) Sets the text to be displayed using a string resource identifier and the TextView.BufferType.
  • final void setText(char[] text, int start, int len) Sets the TextView to display the specified slice of the specified char array.

You are trying to use a setText(int,int) which is not supported. You should do something like textView2.setText(x1+" "+y1);

Jérôme
  • 1,254
  • 2
  • 20
  • 25
  • Thanks for this fast answer. I will try it like this. But I also try ... textView.setText(x1); and it also doesnt work... – rcode Apr 04 '17 at 06:07
  • `text.setText(x1)` will set the text to be displayed using a **string resource identifier.** So it will not work as you expect. – Jérôme Apr 04 '17 at 06:36
  • I try textView.setText(x1+" "+y1). It was the only error - now it works fine. Thank you very mouch! – rcode Apr 04 '17 at 14:01