Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnTouchListener on Activity never calls

Tags:

android

I used this code, but when i click on activity at runtime, it never hits in OnTouch() method. Can someone guide me what i am doing wrong? Should i need to setcontentview of this activity? Actually i want the coordinates of activity where user touch during execution.

public class TouchTestAppActivity extends Activity implements OnTouchListener 
{ 

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

    @Override
 public boolean onTouch(View arg0, MotionEvent arg1) 
       { 
        // TODO Auto-generated method stub 
        String test = "hello";  
       } 
} 
like image 846
Muhammad Imran Avatar asked Jul 06 '11 04:07

Muhammad Imran


2 Answers

UPDATE:

You need to do this :

  • In your XML layout file, you need an ID for the root view: android:id="@+id/myView"
  • In youer onCreate() method, write this:

    LinearView v= (LinearView) findViewById(R.id.myView);
    v.setOnTouchListener(this);
    

Assuming that your root view is a LinearView

like image 188
iTurki Avatar answered Nov 15 '22 21:11

iTurki


You should the onTouchListener to relevant GUI components.

For example:

   public void onCreate(Bundle savedInstanceState) 
   { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.touch); 
       TextView someView = (TextView)findViewById(R.id.some_view_from_layout_xml);
       someView.setOnTouchListener(this); // "this" is the activity which is also OnTouchListener
   } 
like image 30
MByD Avatar answered Nov 15 '22 20:11

MByD