Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:39:23+00:00 2026-06-15T16:39:23+00:00

My application makes use of the support package to implement Fragment s on 2.2

  • 0

My application makes use of the support package to implement Fragments on 2.2 upwards.

I have a View subclass (let’s call it ZoomableView) that makes use of ScaleGestureDetector to detect pinch scaling events. This is done in the familiar way; that is, within onTouchEvent(), the MotionEvents are passed to the detector using mScaleDetector.onTouchEvent(event). A SimpleOnScaleGestureListener is used to receive the scale events.

This ZoomableView class is always displayed as the sole View within a Fragment (let’s call it ZoomableFragment). There are two use cases for it within the application. In one use case, the layout only consists of the ZoomableFragment, containing the ZoomableView. In the second use case, the ZoomableFragment will be displayed alongside another Fragment. This other Fragment happens to be housing a MapView, but the use of MapView isn’t relevant to the problem (the problem still occurs if that second Fragment just contains an ordinary View).

The actual problem is that when testing on a pre-Jellybean device (specifically, a Froyo phone), when using the Activity that shows the twin-Fragment layout, the ZoomableFragment‘s ZoomableView ScaleGestureDetector doesn’t work. When I say ‘doesn’t work’, I can see in the debugger that it processes the MotionEvents, but none of the methods in the SimpleOnScaleGestureListener are called. However, when using the Activity that contains only the ZoomableFragment, the scale detection does work. If I try the application on my JellyBean devices, the twin-Fragment situation works absolutely fine; that is, the scale gestures are received.

I’ve searched around and the only issue I can find is where people have implemented OnScaleGestureListener and prevented it from working by returning false from onScaleBegin(). That’s not my issue here.

So in summary, a ScaleGestureDetector doesn’t call its listener when it should do when used within a View on a multiple support Fragment layout on Froyo (Android 2.2) and I assume up to and including ICS. If however the Fragment is the only one on the screen, the detector works fine. Furthermore, the problem doesn’t exist whatsoever on my 4.1+ Jellybean devices.

EDIT 1 In my twin-Fragment layout, the ZoomableFragment has always been placed on the right or bottom depending on orientation. An interesting observation I just made is that if I flip the layout such that the ZoomableFragment is at the top (rather than being the lower fragment) in the portrait orientation, or is on the left in horizontal orientation, then the scale detector works! I therefore suspect that this is something to do with the relation between the results of the MotionEvent getRawX() / getX() (and similar for Y) methods, when Fragments are used. But when I flip the layout like this, pinch zooming then doesn’t work for the adjacent MapView Fragment! So, in summary, on Froyo it seems that ScaleGestureDetector is working only for a View contained in a Fragment that has a corner at 0,0 of the screen.

EDIT 2 I’ve now answered this question myself with a simple solution I’ve come to after inspecting the source for ScaleGestureDetector on grepcode. Because the source even as far as ICS made use of getRawX() / getRawY(), I’ve updated this question to state as far as ICS being affected (I believe), rather than just Froyo as I first thought.

EDIT 3 I’ve done a simple test to completely eliminate anything to do with Fragments or anything else in my application. I took the simple Google MapView example project and simply changed the layout so that most of the screen is taken up by a TextView (weight 0.9) and a tiny MapView sits at the bottom with a weight of just 0.1. On my Froyo device, it will no longer pinch zoom. On my JB device, it does. So it seems I’m not going nuts, and I have come across a problem with the GestureScaleDetector on ICS and below (I think – certainly Froyo, anyway) for which the answer gives a fix.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:39:24+00:00Added an answer on June 15, 2026 at 4:39 pm

    From inspecting the ScaleGestureDetector source code on grepcode, I see that Jellybean versions of ScaleGestureDetector do not make use of getRawX() and getRawY() methods of the MotionEvent. However, the earlier versions (Froyo, ICS etc) do make use of getRawX() and getRawY() for the purpose of slop calculation. This is where the problem is, because it means that the scale detector won’t work if the target View doesn’t have its top left near 0,0.

    I got the ScaleGestureDetector to work on my custom ZoomView, even with it being placed on the right or bottom, by simply doing this within onTouchEvent():

    event.setLocation(event.getRawX(), event.getRawY());    
    mScaleDetector.onTouchEvent(event);
    

    That causes adjustment of the MotionEvent‘s internal offset variables such that the getX/Y and getRawX/Y methods now return exactly the same value. This prevents the slop calculation failing. The scale detector of course works with relative values and so it doesn’t matter about the absolute values of X / Y. If you have further code that relies on the absolute values, you could do what I did to restore them after:

        float originalY = event.getY();
        float originalX = event.getX();     
        event.setLocation(event.getRawX(), event.getRawY());
        mScaleDetector.onTouchEvent(event);
        event.setLocation(originalX, originalY);
    

    Furthermore, if I want to place my Fragment that contains a MapView on the bottom or right, I am already using a custom subclass of MapView anyway and so I just added this:

    public boolean onTouchEvent(MotionEvent event) {
    
        // Work around required for ScaleGestureDetector.
        // Before calling the scale detector, perform a work-around that is needed for earlier APIs (I suspect
        // ICS and below, judging from inspection of ScaleGestureDetector) to make the MotionEvent give the 
        // same values for getY / getRawY and getX / getRawX. Wildly different values, caused by the View
        // being nowhere near the screen origin (i.e. the View is on the right or the bottom) means the 
        // detector doesn't work. 
    
        event.setLocation(event.getRawX() - getLeft(), event.getRawY() - getTop());
        return super.onTouchEvent(event);
    }
    

    Now, my MapView will pinch scale properly too, no matter where its Fragment is placed. (By the way – totally OT to this question, but in case anyone is wondering, I placed a MapView into a Fragment successfully using the LocalActivityManager solution as given on StackOverflow). EDIT: Actually, something is not 100% right: The map doesn’t zoom around the center of the pinch gesture. I think it’s to do with the ActionBar / notification bar height not being compensated for in the getTop().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have taken over support of a VB .Net application that makes use of
I have C# application that makes use of some C libaries(which I have written
I have an application that makes use of frequently updated lists. So for example,
I have a WinForms application that makes use of a TaskDialog library that leverages
Scenario: my application makes use of uibinder. I have a splitlayout panel with: a
I have a Sinatra application running on Heroku which makes use of Dalli to
Our PHP application makes use of json_encode($myObject) a lot, in conjunction with the mustache
My Rails application makes use of Time.zone so that each user can set their
I am making an application which makes use of context menus and has selection.
I developed a C# application that makes use of Congex vision library (VPro). My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.