1

I have the following class:

public class SplashActivity extends Activity implements OnKeyListener, OnTouchListener {
    private LinearLayout mLinearLayout;

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

        mLinearLayout = new LinearLayout(this);
        mLinearLayout.addView(getBgImg());
        mLinearLayout.setOnKeyListener(this);
        mLinearLayout.setOnTouchListener(this);
        mLinearLayout.setFocusable(true);
        setContentView(mLinearLayout);
    }

    private ImageView getBgImg() {
        ImageView imgV = new ImageView(this);
        imgV.setImageResource(R.drawable.splash);
        imgV.setAdjustViewBounds(true);
        imgV.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        return imgV;
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        System.out.println("test2");
        return true;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        System.out.println("test1");
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_S:
            // mDoSat = !mDoSat;
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

Unfortunately, despite pressing buttons on the emulator and clicking on the screen, I get no "test1" or "test2" printed in LogCat.

yarian
  • 5,922
  • 3
  • 34
  • 48
ls-l
  • 11
  • 2

5 Answers5

2

You should use Log.d("TAG", "test1"), d is for debug so you could also use the other ones ex. Log.i , this will be written in your LogCat.

See the Android documentation for more information on Logcat.

yarian
  • 5,922
  • 3
  • 34
  • 48
Henrik
  • 1,147
  • 2
  • 11
  • 22
0

Make use of a GestureDetector within the View.onTouchEvent(MotionEvent ev) method

what, you want sample code??? read the docs, in your View, use a GestureDetector to translate the MotionEvent received by the onTouchEvent method... sheesh :)

Dan
  • 1,002
  • 12
  • 24
0

Sometimes I get the case where logcat doesn't show anything. However, if I run the application in debug once, it usually fixes this issue.

skynet
  • 9,898
  • 5
  • 43
  • 52
Ian
  • 3,500
  • 1
  • 24
  • 25
0

I don't believe the touch events trigger when the child is pressed. You need to add the listeners to the getBgImg() view I think.

sean wagner
  • 423
  • 4
  • 5
0

Overrides: onTouchEvent(...) in Activity public boolean onTouchEvent (MotionEvent event) Since: API Level 1 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.

As you can see, onTouchEvent is called when a touch screen event was not handled by any of the views under it.

So, I'm suggesting that use onClickListener, and return false in the onTouchEvent.

theWook
  • 843
  • 9
  • 22