Question

I am trying to write an app for android and I have some trouble. I want to record x and y coordinates in webview. Webview works great, but then I am trying to record coordinates nothing happens. At least i think so. When I trying to print coordinates program prints nothing.

So, it is my code :

package com.gm;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {


WebView web;

private Button rec, end, play, stop;

private int x,y,c; 
protected static SharedPreferences prefs;
protected boolean clicked = false;

List<String> list = new ArrayList<String>(c);

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

    web = (WebView) findViewById(R.id.webview01);       
    web.setWebViewClient(new myWebClient());
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("http://tob.lt");

    rec = (Button) this.findViewById(R.id.rec);
    rec.setOnClickListener(this);

    end = (Button) this.findViewById(R.id.end);
    end.setOnClickListener(this);

    play = (Button) this.findViewById(R.id.play);
    play.setOnClickListener(this);

    stop = (Button) this.findViewById(R.id.stop);
    stop.setOnClickListener(this);
 } 

 public class myWebClient extends WebViewClient
 {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url);

        return true;

    }

 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
   if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
      web.goBack();
      return true;
 }
 return super.onKeyDown(keyCode, event);
 }


@Override
public boolean onTouchEvent (MotionEvent event){
   if(clicked){
   x = (int)event.getX();
   y = (int)event.getY();
     switch (event.getAction()) {
       case MotionEvent.ACTION_DOWN:
       case MotionEvent.ACTION_MOVE:
       case MotionEvent.ACTION_UP:
     }
     System.out.println("out x"+x);
     System.out.println("out y"+y);
    }
return false;
};


@Override
public void onClick(View v) {
    if (v == rec){      
        clicked = true;         
    }
    else if (v == end){
        clicked = false;            
    }
    else if (v == play){}       
    else if (v == stop){}       

}

}

can anyone tell if I correctly use onTouchEvent method? And what i did wrong? Thanks for helping.

P.S. Sorry for my bad English.

Was it helpful?

Solution

System.out.println() will not work the way you expect it to in an Android app (there is no console to print to, per se). It looks like the onTouchEvent method is getting called correctly, but you have to "print" the x and y coordinates differently so that you actually see the output.

To see the output, try either writing to logcat

Log.d("MainActivity", "x = " + x + ", y = " + y);

Or displaying a Toast

Toast.makeText(this, "x = " + x + ", y = " + y, Toast.LENGTH_SHORT).show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top