views:

72

answers:

0

I am trying to implement a alert box to pop up when someone holds their finger down on the screen (onLongPress event). The layout contains a TextView inside a LinearLayout, inside a ScrollView. If the text being displayed is short, everything works fine. But if it is so long that a scroll bar is present, the event won't fire at all. How can I make the onLongPress event always work?

main.xml:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollView01" android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:layout_weight="1">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:layout_weight="1">

 <TextView android:id="@+id/txtTop" android:text="Welcome!"
  android:textSize="15pt" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:layout_weight="1" />

</LinearLayout>

</ScrollView>

PressError.java:

package com.stackoverflowexample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

public class PressError extends Activity implements OnGestureListener {
 static TextView txtTop;
 String thetext = null;
 private GestureDetector gestureScanner;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  gestureScanner = new GestureDetector(this);
  thetext = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. ";
  thetext = thetext + "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, ";
  thetext = thetext + "when an unknown printer took a galley of type and scrambled it to make a type specimen book. ";
  txtTop = (TextView) findViewById(R.id.txtTop);
  txtTop.setText(thetext);

 }


    @Override
    public boolean onTouchEvent(MotionEvent me) {
     return gestureScanner.onTouchEvent(me);
    }

    @Override
    public void onLongPress(MotionEvent e) {

        // prepare the alert box
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

        // set the message to display
        alertbox.setMessage("This is the alertbox!");

        // add a neutral button to the alert box and assign a click listener
        alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

            // click listener on the alert box
            public void onClick(DialogInterface arg0, int arg1) {
                // the button was clicked
                Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
            }
        });

        // show it
        alertbox.show();

    }

 @Override
 public boolean onDown(MotionEvent arg0) {
  // TODO Auto-generated method stub
  return false;
 }

 @Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  // TODO Auto-generated method stub
  return false;
 }

 @Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  // TODO Auto-generated method stub
   return false;
 }

 @Override
 public void onShowPress(MotionEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public boolean onSingleTapUp(MotionEvent e) {
  // TODO Auto-generated method stub
  return false;
 }

}