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

  • Home
  • SEARCH
  • 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 8784019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:58:38+00:00 2026-06-13T20:58:38+00:00

My Question: Is there a work-around for using an onTouchEvent in a FragmentActivity? My

  • 0

My Question: Is there a work-around for using an onTouchEvent in a FragmentActivity?

My Situation: I’m writing a simple flashlight app and I have a FragmentActivity where a user can swipe between colors (the whole screen is just one color. This app is for personal improvement). I want to be able to do a onLongPress event for options, a onDoubleTap event for hiding the navigation bar, and most importantly a touch event to control brightness of the app (I want to let the user scroll up and down for brightness changes, any ideas here are welcome! is the onFling event good for this?)

What I’ve tried: I have a GestureDetector with my implementation of an SimpleOnGestureListener. I override the onTouchEvent in the FragmentActivity, but I have just found out that this method is never called in a FragmentActivity (I also tested this with a Log.print in the method and it’s never called). Is there a work-around? Thanks!

Salient Code:

MainActivity:

public class MainActivity extends FragmentActivity {

  private GestureDetector gestureDetector;
  private FragmentPagerAdapter mAdapter;

  /**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    gestureDetector = new GestureDetector(getApplicationContext(), new MyGestureListener(getWindow()));

    mAdapter = new ColorFragmentAdapter(getSupportFragmentManager(), getDefaultColorList());
    ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
    pager.setAdapter(mAdapter);
    pager.setCurrentItem(Math.round(mAdapter.getCount() / 2)); //Set current fragment to middle fragment
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    print("Sending touch event to gestureDetector.");
    //This line is never run.
    return gestureDetector.onTouchEvent(event);
  }

  /**
   * Prints the given object to the log with the debug priority with the tag FLASHLIGHT
   *
   * @param object
   */
  public static void print(Object object) {
    Log.println(Log.DEBUG, "FLASHLIGHT", object.toString());
  }
}

MyGestureListener :

public class MyGestureListener extends SimpleOnGestureListener {

  private final Window window;

  public MyGestureListener(Window window) {
    super();
    this.window = window;
  }

  @Override
  public void onLongPress(MotionEvent e) {
    //onLongPress code
  }

  @Override
  public boolean onDoubleTap(MotionEvent e) {
    //onDoubleTap code
  }

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    //onFling cod
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  >
  <android.support.v4.view.ViewPager
        android:id="@+android:id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>

I don’t think you’d need anything else to answer the question, but let me know if you do.

  • 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-13T20:58:39+00:00Added an answer on June 13, 2026 at 8:58 pm

    So the solution to this is relatively easy. The problem is that the ViewPager is eating the touch event. So what I did was write my own implementation of a ViewPager and overrided the onTouchEvent(MotionEvent event) method by calling super.onTouchEvent(event) and then passing the event to the gestureDetector within the class.

    Relevant Code:

    MainActivity

    public class MainActivity extends FragmentActivity {
    
      private FragmentPagerAdapter mAdapter;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        mAdapter = new FragmentAdapter(getSupportFragmentManager());
        MyViewPager pager = (MyViewPager) findViewById(R.id.viewpager);
        pager.setGestureDetector(new GestureDetector(this, new MyGestureListener(this)));
        pager.setAdapter(mAdapter);
        pager.setCurrentItem(Math.round(mAdapter.getCount() / 2));
      }
    }
    

    MyViewPager

    public class MyViewPager extends ViewPager {
    
      private GestureDetector gestureDetector;
    
      public MyViewPager(Context context) {
        super(context);
      }
    
      public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      @Override
      public boolean onTouchEvent(MotionEvent e) {
        return super.onTouchEvent(e) && gestureDetector.onTouchEvent(e);
      }
    }
    

    MyGestureListener

    public class MyGestureListener extends SimpleOnGestureListener {
    
      private final Activity activity;
    
      public MyGestureListener(Activity activity) {
        super();
        this.activity = activity;
      }
    
      @Override
      public boolean onDoubleTap(MotionEvent e) {
        //...
      }
    
      @Override
      public boolean onSingleTapConfirmed(MotionEvent e) {
        //...
      }
    
    //... etc...
    
    }
    

    layout_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
    >
      <com.kentcdodds.flashlight.MyViewPager
        android:id="@+android:id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
            />
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a job interview tomorrow and I'm trying to answer this question: There
Simple question: Is there a class or interface that encapulates the getting of a
Me again! I have a question re. inserting into a sql table using LINQ.
I have what is probably a simple question here about Castle Windsor, which I
I'm trying to work around a webview issue on Android devices using Select drop
Question 1: I have some XML-based layouts which work fine on phones. But when
I'm writing my first Grails app and I've reversed my existing Postgresql schema using
I've been working around a problem I have when using LINQ to Entities when
I have a class that is using Spring AOP framework in my web app
So, I have a dialog based application using pure WinAPI. There is a main

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.