newbie question, touch events handling

Put your problem here if it does not fit any of the other categories.

newbie question, touch events handling

Postby ls-l » Wed Mar 30, 2011 12:16 am

Hello everybody :)

I've got a newbie question concerning handling touch events. My simple class looks like that:


Code: Select all
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 in spite of pressing buttons on the emulator and clicking on the screen, I get no "test1" or "test2" printed in the LogCat... I have no idea what's wrong...
thank you in advance for any hint...
ls-l
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 30, 2011 12:11 am

Top

Re: newbie question, touch events handling

Postby raj_J2ME » Wed Mar 30, 2011 7:38 am

Welcome to the Android Forums. :)

Well, you are not clear about the key events. As far as the button event is concern, you did not add any of the button in your activity, hence there is no point for the event for the button. You have to add the button and then onClickListener to the same and then you will get the button clicked events.

You go through the below mentioned URL, to get more details about the Events Handling.
http://developer.android.com/guide/topics/ui/ui-events.html
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: newbie question, touch events handling

Postby ls-l » Wed Mar 30, 2011 8:48 am

thanks for your message, OK then it would be clear about the click events on buttons. What about the touch events

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



test1 wasn't printed any time, in spite of the code which should set a listener on the current Activity object..

Code: Select all
mLinearLayout.setOnTouchListener(this);


I thought that it's just enough to catch the events? Is it really like that only buttons can do it?
ls-l
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 30, 2011 12:11 am

Re: newbie question, touch events handling

Postby raj_J2ME » Wed Mar 30, 2011 12:05 pm

I guess this method below:

Code: Select all
   @Override
   public boolean onTouchEvent(MotionEvent event) {
      // TODO Auto-generated method stub
      System.out
            .println("KeyEventsHandler.onTouchEvent()------------------->");
      return super.onTouchEvent(event);
   }


Will give you the event if you have touched the screen.
The onTouchEvent() actually will get called by the Activity if none of the views consume the touch event.

And as you say, the onTouch() can be used in any class, as long as:

That class (i.e. Foo) implements the OnTouchListener interface and
That class is a registered listener using view.setOnTouchListener(foo);
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: newbie question, touch events handling

Postby ls-l » Wed Mar 30, 2011 7:24 pm

Thanks for your message. From what you've written looks like the solution to make my code working (printing test1 or test2) requires commenting out:

//mLinearLayout.setOnTouchListener(this);

because it consumes the touch event before.. On the other hand it still doesn't work ;( maybe I will take some ready code and find out how the people make it working...
ls-l
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 30, 2011 12:11 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 30 guests