I want to capture the touch events on Android wear (I am using Samsung Gear Live) to draw the trajectory of touch. I tried to capture onTouch event and onGenericMotionEvent event as the following code, but the event is triggered only one time or sometimes does not happen while I swiping on the screen. I need more touch events (at least 5 to 6 events when I swipe left to right) to draw the trajectory. How I can capture enough touch events on Android Wear screen?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
#Override
public void onLayoutInflated(WatchViewStub stub) {
ViewGroup container = (ViewGroup) findViewById(R.id.container);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
String s = "";
s += "action=" + event.getAction();
s += ", X=" + event.getX();
s += ", Y=" + event.getY();
Log.d(TAG, s);
return false;
}
});
container.setOnGenericMotionListener(new View.OnGenericMotionListener() {
#Override
public boolean onGenericMotion(View view, MotionEvent event) {
String s = "";
s += "action=" + event.getAction();
s += ", X=" + event.getX();
s += ", Y=" + event.getY();
Log.d(TAG, s);
return false;
}
});
}
});
}
Can you try using only:
container.setOnTouchListener(new View.OnTouchListener() {
and return true from there? You need to inform the View you consumed the event.
If you want to debug offline you can get the touch co-ordinates from adb:-
adb shell getevent
Related
I have a Composite containing a number of Text controls. I have attached MouseListeners to each control.
What surprises me is that sometimes, when I click on a control, I get a MouseDown event from its neighbour. The Event position is outside of the control's boundary and I get no event from the other control which I thought I had clicked on.
What can cause this to happen?
SNIPPET
Run. Press Esc to close the MessageBox. Click in field BBBB. Press Esc to close MessageBox. Click in field AAAA. The event is generated from field BBBB.
public class Test
{
public class MyListener implements MouseListener, FocusListener
{
private boolean active;
#Override
public void focusGained(FocusEvent e)
{
System.out.println(e);
message((Text) e.widget, "FocusGained");
}
#Override
public void focusLost(FocusEvent e)
{
System.out.println(e);
}
#Override
public void mouseDoubleClick(MouseEvent e)
{
}
#Override
public void mouseDown(MouseEvent e)
{
System.out.println(e);
}
private void message(final Text t, final String m)
{
if (active == false)
{
active = true;
MessageBox mb = new MessageBox(t.getShell());
mb.setText(m);
mb.setMessage(t.getMessage() + "\n\n" + m);
mb.open();
active = false;
}
}
#Override
public void mouseUp(MouseEvent e)
{
System.out.println(e);
message((Text) e.widget, e.toString());
}
}
private MyListener listener = null;
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
new Test(shell);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public Test(Composite parent)
{
listener = new MyListener();
create(parent);
}
private void create(Composite parent)
{
parent.setLayout(new GridLayout(2, true));
createText(parent, "AAAA");
createText(parent, "BBBB");
parent.layout(true);
}
private Text createText(Composite parent, String message)
{
Text t = new Text(parent, SWT.NONE);
t.setMessage(message);
GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
t.setLayoutData(gd);
t.addFocusListener(listener);
t.addMouseListener(listener);
return t;
}
}
This is indeed strange. When traversing from B to A, the MouseUp event is marked as sent from field B.
If you replace the MessageBox with something non-interrupting i.e. System.out, the mouse event senders are the right ones.
To me, this seems more of a theoretical corner case. Decent applications would not interrupt the users field traversal with a modal window. However, if this is relevant for your, I'd report a bug to SWT.
I managed to get this working by de-coupling the pop-up window from the events. The message method now looks like this:
private void message(final Text t, final String m)
{
//NB active is declared as 'volatile'
if (active == false)
{
active = true;
t.getDisplay().asyncExec(new Runnable()
{
#Override
public void run()
{
MessageBox mb = new MessageBox(t.getShell());
mb.setText(m);
mb.setMessage(t.getMessage() + "\n\n" + m);
mb.open();
active = false;
}
});
}
else
{
System.out.println("Already active: " + t.getMessage());
}
}
This seems to give the events the opportunity to continue uninterrupted by the pop-up window. The pop-up will be activated a short while later. So far it works ok.
I run into a problem where I need to enable my button in the ListView. The weird thing is :
public class CookingStepAdapter extends ArrayAdapter<CookingStep> {
...
private void addButtonToList(Button clock, Button skip){
if (list_clock_button == null) {
list_clock_button = new ArrayList<Button>();
iterate = 0;
}
if (list_skip_button == null)
list_skip_button = new ArrayList<Button>();
list_clock_button.add(clock);
list_skip_button.add(skip);
clock.setEnabled(true);
skip.setEnabled(true);
list_clock_button.get(0).setEnabled(true);
list_clock_button.get(0).setFocusable(true);
list_clock_button.get(0).setFocusableInTouchMode(true);
list_clock_button.get(0).invalidate();
list_skip_button.get(0).setEnabled(true);
list_skip_button.get(0).setFocusable(true);
list_skip_button.get(0).setFocusableInTouchMode(true);
list_skip_button.get(0).postInvalidate();
}
}
When I set enable with the list_clock_button.get(0), it's not working at all. But clock.setEnabled(true); actually worked.
But then I only want the first button of the ListView enabled, that makes the first option more fit in this situation. The second option works, but it made all the buttons enabled, that's not what I want. I did recheck the first button address, and it matched list_clock_button.get(0), why it's not working.
EDIT :
Here's my function getView :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.cooking_steps_and_timer, parent, false);
final Button button = (Button) rowView.findViewById(R.id.button_timer);
final TextView timer = (TextView) rowView.findViewById(R.id.cooking_timer);
final Button skipButton = (Button) rowView.findViewById(R.id.button_skip);
TextView stepContent = (TextView) rowView.findViewById(R.id.cooking_step_content);
final CookingStep step = list.get(position);
String stepOrder = (String) context.getResources().getText(R.string.step_order) + " " + step.getOrder();
String content = "<b>" + stepOrder + ":</b>" + " " + step.getContent() + "\n";
stepContent.setText(Html.fromHtml(content));
if (step.getTimer() == null || step.getTimer() == 0){
timer.setVisibility(View.GONE);
button.setVisibility(View.GONE);
skipButton.setVisibility(View.GONE);
} else {
//myTimer = new CookingTimer(step.getTimer());
timer.setText(step.getMyTimer().toString());
step.setCountDown(new CountDownTimer(step.getTimer() * 60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
step.getMyTimer().tick();
timer.setText(step.getMyTimer().toString());
}
#Override
public void onFinish() {
nextButtonEnable();
}
});
button.setText(R.string.button_available);
skipButton.setText(R.string.skip_button_content);
addButtonToList(button, skipButton);
//button.setEnabled(true);
list_clock_button.get(0).setEnabled(true);
button.requestFocusFromTouch();
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (!button.isPressed()) {
button.setPressed(true);
button.setText(R.string.button_available);
step.getCountDown().start();
} else {
button.setPressed(false);
button.setText(R.string.button_pressed);
step.getCountDown().cancel();
}
}
return true;
}
});
skipButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
step.getCountDown().cancel();
nextButtonEnable();
}
return true;
}
});
}
return rowView;
}
The reason of all buttons getting enabled is you are setting all of them enable yourself. To set any particular button enable you have to check the id or the item position to set that particular button to enable.
Like this for example:-
if(clock.getID == R.id.some_id_in_xml){
clock.setEnabled(true);
}
But before that you have to check the position of the item to select the desired item in the list.
Below is my code. I am having a problem when I call SensorManager.registerListener, my app will crash. Can someone tell me what's going on?
I just follw the web guide to setup SensorManger, Sensor(Accelerometer) and then register the action lintener to detect the montion of accelerometer.
I used API 21 to develop this app.
public class MainActivity extends ActionBarActivity implements SensorEventListener{
private TextView tip;
private SensorManager mSensorManager;
private Sensor mSensor;
private float axisX = 0;
private float axisY = 0 ;
private float axisZ = 0;
#Override
protected void onResume() {
super.onResume();
setUpAcceleratorSensor();
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpAcceleratorSensor();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setUpAcceleratorSensor(){
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
if((mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)) != null);
else
Toast.makeText(this, "No Sensor Device Exist", Toast.LENGTH_LONG).show();
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
Sensor mySensor = event.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
if(event.values[0] != 0 || event.values[1] != 0 || event.values[2] != 0){
axisX = event.values[0];
axisY = event.values[1];
axisZ = event.values[2];
tip.setText("Detect your montion");
}
}
else
Toast.makeText(this, "Cannot Get Sensor Device", Toast.LENGTH_LONG).show();
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
Thanks.
First that I always check when something like this goes wrong, is to check that you have all the correct permissions in the Android Manifest; however, I don't believe that there are any permissions associated with using the position sensors. I would check on this first. That is what comes to mind first, after you post logcat, we will be able to give a more detailed answer.
Try getting the sensor this way
mSensor = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); instead in your setUpAccelerometer() method.
I have a FragmentActivity with a GoogleMap inside. It correctly receives the user location in onLocationChanged, where I try to use the user location to center the map:
#Override
public void onLocationChanged(Location loc)
{
if (centerMap && mMap != null)
{
LatLng location = new LatLng(loc.getLatitude(), loc.getLongitude());
CameraPosition pos = new CameraPosition.Builder().target(location).zoom(12).build();
CameraUpdate cu = CameraUpdateFactory.newCameraPosition(pos);
mMap.animateCamera(cu);
// centerMap = false;
Log.e(getClass().getSimpleName(), "lat = " + location.latitude + " long = " + location.longitude);
}
}
This code actually worked now and then (maybe only once), and only during a debug session where I put a breakpoint inside the if statement. I don't understand what's wrong. The onLocationChanged method gets called regularly, it logs the position it received, that implies it entered the if condition, but the map does not move an inch from the lat=0,long=0 initial position (Africa).
Any clues?
EDIT: I must have something badly broken in my code: even markers do not show up, here is how I add them to the map
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("title")
.draggable(false)
.visible(true));
and the "My location" icon is not showing up either, even if I called
mMap.setMyLocationEnabled(true);
in onCreate(). Trying to exclude martians and such, I've already updated Eclipse, ADT and all the rest to the latest available versions.
For some reason (unknown to me), moving the map initialization from onCreate() into the first invocation of onLocationChange() did the trick. Now my onCreate() is simply
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.map_activity);
}
my onStart() is:
#Override
protected void onStart()
{
super.onStart();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create();
// Use high accuracy
mLocationRequest.setPriority(
LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the update interval to 5 seconds
mLocationRequest.setInterval(UPDATE_INTERVAL);
// Set the fastest update interval to 1 second
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationClient = new LocationClient(this, this, this);
mLocationClient.connect();
}
#Override
public void onConnected(Bundle arg0)
{
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
and my onLocationChange() is
#Override
public void onLocationChanged(Location loc)
{
if (mMap == null)
{
mapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.add(R.id.mapcontainer, mapFragment);
fragmentTransaction.commit();
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.getUiSettings().setAllGesturesEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.setMyLocationEnabled(false);
LatLng location = new LatLng(loc.getLatitude(), loc.getLongitude());
CameraPosition pos = new CameraPosition.Builder().target(location).zoom(12).build();
CameraUpdate cu = CameraUpdateFactory.newCameraPosition(pos);
mMap.animateCamera(cu);
if (mLocationClient.isConnected())
mLocationClient.removeLocationUpdates(this);
mLocationClient.disconnect();
Log.e(getClass().getSimpleName(), "lat = " + location.latitude + " long = " + location.longitude);
}
}
It works, but now I have a different problem (the map seems to ignore touch events). I'm going to create a separate question for that.
First post here, but stackoverflow has solved soooo many problems for me. This one though, I can't seem to figure out. I'm creating an android app that de-increments and TextView value by 1 with every proximity detection. For some reason, when the Activity starts or resumes (power button), it logs 2 proximity hits. I've double-checked to ensure that I'm not close enough to the detector when pushing the power button.
Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exercise);
//start proximity
startProximitySensor(sensorListener());
}
private SensorEventListener sensorListener(){
listener = new SensorEventListener(){
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_PROXIMITY){
String maxRange = String.valueOf(mProximitySensor.getMaximumRange());
if(event.values[0] == Float.parseFloat(maxRange)){
updateTextView();
Log.i(TAG,"Proximity Sensor Reading: "+ String.valueOf(event.values[0]));
}
}
}
};
return listener;
}
private void startProximitySensor(SensorEventListener proximitySensorEventListener){
mSensorManager = (SensorManager)getSystemService(getApplicationContext().SENSOR_SERVICE);
mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if(mProximitySensor == null){
Log.i(TAG,"No proximity sensor found!");
}else{
proximityStarted = true;
Log.i(TAG,"Proximity started");
mSensorManager.registerListener(proximitySensorEventListener,mProximitySensor,SensorManager.SENSOR_DELAY_UI);
}
}
I've managed to get around it by creating a timeStamp in onResume, and comparing that to the SystemTime generated on each sensor change. If they differ for anything less than 1 second, then the TextView won't update. This works, but I'd still like to know if I'm missing something.
Thanks in advance