I am using google MapView in my application and set the zoom level to 2 so that the world map with continent names are visible. Now I want to disable horizontal scrolling. Can any one share some idea on how to do it ?
Just got an idea. Using MotionEvent.AXIS_* flags.
#Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_AXIS_...) {
// ...
}
return sper.onTouchEvent(event);
}
Look at AXIS_HSCROLL, AXIS_VSCROLL, AXIS_X, AXIS_Y, AXIS_Z: Documentation
Related
I have been working for a day at this and still can't figure it out. I am making an Android app for an Android Wear device and I can't figure out how to detect the rotary wheel on the device to get scroll events. I know that by default the rotary wheel gets assigned to ScrollViews and Listviews, etc. and I've got that to work, but I can't get it to work on a simple NumberPicker. As suggested by Google API's, I've used the OnGenericMotionListener and while it does not throw any errors, it still doesn't pick up anything.
I would like the NumberPicker to scroll with the rotary wheel. My code is as follows. Nothing in logcat to show any errors:
MyActivity.this.setContentView(R.layout.number_picker);
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker1);
numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
numberPicker.setMaxValue(100);
numberPicker.setMinValue(0);
numberPicker.setWrapSelectorWheel(true);
numberPicker.setOnValueChangedListener( new NumberPicker.
OnValueChangeListener() {
public void onValueChange(NumberPicker picker, int
oldVal, int newVal) {
}
});
numberPicker.setOnGenericMotionListener(new OnGenericMotionListener() {
#Override
public boolean onGenericMotion(View v, MotionEvent event) {
Log.i(TAG, "Received scroll event:" + event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL:
}
return false;
}
});
I think you're looking for this:
Add custom rotary input scrolling:
If your scrollable view doesn't natively support rotary input
scrolling, or if you want to do something other than scrolling in
response to rotary input events (zoom in/out, turn dials, etc.), you
can use the RotaryEncoder methods in the Wearable Support Library.
The following code snippet shows how to use RotaryEncoder to add
custom scrolling in your app's view:
myView.setOnGenericMotionListener(new View.OnGenericMotionListener() {
#Override
public boolean onGenericMotion(View v, MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_SCROLL && RotaryEncoder.isFromRotaryEncoder(ev)) {
// Don't forget the negation here
float delta = -RotaryEncoder.getRotaryAxisValue(ev) * RotaryEncoder.getScaledScrollFactor(
getContext());
// Swap these axes if you want to do horizontal scrolling instead
scrollBy(0, Math.round(delta));
return true;
}
return false;
}
});
Anyway you can check the full guide about rotary events in android
wear.
The documentation it's a little confusing but it's all there, pay attention to the next paragraph:
Rotary input events are only sent to the focused view. These events
do not bubble up the view hierarchy. If there is no focused view, or
if the focused view returns false from View.onGenericMotionEvent,
then (and only then) the event is sent to
Activity.onGenericMotionEvent.
Basically all you have to do is override onGenericMotionEvent on your Activity like this:
#Override
public boolean onGenericMotion(View v, MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_SCROLL && RotaryEncoder.isFromRotaryEncoder(ev)) {
// Don't forget the negation here
float delta = -RotaryEncoder.getRotaryAxisValue(ev) * RotaryEncoder.getScaledScrollFactor(
getContext());
// Swap these axes if you want to do horizontal scrolling instead
scrollBy(0, Math.round(delta));
return true;
}
return false;
}
And that's it your Activity will receive all the rotary input events.
I am using onTouch method for doing some works. At first step, I want to show currentX value. However, my code does not work at all. How I resolve this issue?
public boolean onTouch(View view, MotionEvent event) {
int currentX = (int) event.getX();
if(event.getAction() == MotionEvent.ACTION_MOVE) {
Log.d("mesage:",currentX+"");
}
return true;
}
check out this link:
Detecting Common Gestures
You have there snippet like this:
View myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// your stuff
return true;
}
});
do you register your listener for a view? is it separated listener or implemented in Activity or other? is it MotionEventCompat or built in? we need more code...
but you said that you need to recognize "fingers to the right side". look in my link for gestures and "Detecting All Supported Gestures" or "Detecting a Subset of Supported Gestures". onFling method is what you want (right/left you can regonize by velocityX>0 or <0)
edit:
in your activity_main you should have more views. you also may set id for main viewGroup (LinearLayout/RelativeLayout?), which is in whole Activity (Screen) and get this view in findViewById, then setOnTouchLister for this particular view. but like I said above probably you need Gesture
I have a ListView. Inside the cells, I have a custom view. (You can draw in it.)
When drawing, I turn off scrolling of the list ..
theListView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
if ( STATE.weAreDrawoning )
{
return true;
// so, do not forward and hence do not scroll the list
}
else
{
return false;
}
}
return false;
}
});
That's fine. But strangely, up-down touching in the custom View, is still passed on to the list and makes it scroll.
public class AmazingCustomView extends View
{
blah
#Override
public boolean onTouchEvent(MotionEvent event)
{
blah
return true;
}
}
notice in the custom view onTouchEvent is returning true (I also tried false! :) )
but the motion events appear to be still passed on .. what gives??
Is there another "on .. something" I'm missing in the custom view? Sorry, new to Android and lame. Thanks!
PS, I tried turning on "clickable" on the xml of the custom view, didn't help :O
--
Worse ...
I've just realised ALL controls in the ListView, say buttons, still "pass on scrolling"
I fear the system I use above for turning off scrolling is just no good. :/
important...
For anyone googling to here. The only real way I've found to turn off scrolling on an android listView ...
danosipov.com/?p=604
(don't forget to separately turn off your pull-to-refresh)
You may need to override onInterceptTouchEvent. Its an odd function, documentation here: http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29
How can i disable the panning/zooming functionality of a MapView (not the zoom controls, i want a wholly static map)?
I've also noticed that touching the map doesn't seem to trigger the MapView onClickListener, could anyone elaborate why?
For version 2 of the Google Maps API for Android, this is the way:
map.getUiSettings().setScrollGesturesEnabled(false);
This is gauranteed to work
mapView.getMap().getUiSettings().setAllGesturesEnabled(false);
Use android:clickable="false" in your layout file.
Please use Overlay to get the tap event on the map
http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/Overlay.html
I had the same question and the following solution is the best one I could find and that fulfilled my requirements:
mapView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getPointerCount() > 1) {
return true;
}
return false;
}
});
As posted by Alexander Stolz here:
How to disable pinch in Android MapView
And here is the reason:
It does not disable clicking on the mapView completely - it only grabs & prevents two-finger gestures (for zooming you need two fingers) - tapping on the map (for instance on Overlays) still works.
this is the right way
#Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
switch (ev.getAction()&MotionEvent.ACTION_MASK)
{
case (MotionEvent.ACTION_DOWN):
{
// do what you want
// you may scroll map where you want
// don't use 'break', the same in case pointer events;
//break;
return true;
}
}
// 'super' go to the mapView procedures and scroll map in own algorithm
return super.dispatchTouchEvent(ev);
}
on your onMapReady method, add these code lines
gMap.getUiSettings().setZoomGesturesEnabled(false);
to disable all the guesures
gMap.getUiSettings().setAllGesturesEnabled(false);
happy coding :)
I am a new user of google maps API in android OS. I have made a small application in which i am using google maps. I want to add a functionality that when i double click (multi touch) on a map the map should zoom in. IS there anybody who has an idea how to do this or if it is possible please provid a code example.
Thanks in advance.
BR,
SilentCoders
I've done something like this in an app using maps api. Although I did this in an overlay item, the principle should be the same.
You could try using TouchListener and GestureDetector to detect the touch events and such.
Note that this is not all actual working code, you need to adopt it so it fits into your implementation.
...
class MyDetector extends SimpleOnGestureListener {
#Override
public boolean onDoubleTap(MotionEvent event) {
mapView.getController().zoomInFixing((int) event.getX(), (int) event.getY());
return super.onDoubleTap(
}
}
// maybe do this in your init or something
GestureDetector gDetector = new GestureDetector(new MyDetector());
mapView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return gDetector.onTouchEvent(event);
}
});
Something like that "should" work :)