aarelovich
aarelovich

Reputation: 5576

Android: Touch Events not Working

This is my Main activity code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Display display = getWindowManager().getDefaultDisplay();        

    //Setting the FileBrowser FrameLayout Settings.
    FrameLayout fileBrowserLayout =  (FrameLayout) findViewById(R.id.flFBrowser);
    FrameLayout previewLayout = (FrameLayout) findViewById(R.id.flPreview);
    TextView instructions = (TextView)findViewById(R.id.tvInstructions);

    fileBrowserLayout.getLayoutParams().width = display.getWidth()*WidthFileBrowser/100;
    previewLayout.getLayoutParams().width = display.getWidth()*WidthPreview/100;
    previewLayout.getLayoutParams().height = display.getHeight()*PreviewHeight/100;
    instructions.getLayoutParams().width = display.getWidth()*WidthPreview/100;
    instructions.getLayoutParams().height = display.getHeight()*InstructionsHeight/100;
    instructions.setText(Instructions);
    instructions.setTextSize(InstructionTextSize);
    instructions.setTextColor(InstructionTextColor);
    instructions.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
    instructions.setGravity(Gravity.CENTER_HORIZONTAL);

    //Attempting to set the Fragment        
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();       
    FileBrowser fb = new FileBrowser();
    preview = new Preview();
    ft.add(R.id.flFBrowser,fb);
    ft.add(R.id.flPreview, preview);
    ft.commit();

}

Then this is my code for Preview Fragment

public class Preview extends Fragment implements ViewFactory,OnTouchListener{

@Override
public boolean onTouch(View v, MotionEvent event) {
    System.err.println("Something touched me!");
    return true;
}
}

There is more intialization code on the onActivityStarted but it has to do with intializing an imageswitcher.

However I can't figure out why my touch event is not registering.

Can anyone help me, please?

Upvotes: 0

Views: 11392

Answers (2)

qix
qix

Reputation: 7952

In my case, I had children that were intercepting touch events. I wanted to attach a recognizer to a parent layout, so to do that, I needed to intercept the touch events (http://developer.android.com/training/gestures/viewgroup.html#intercept). I had to subclass the layout and overriding onInterceptTouchEvent() to pass the event to any touch listener I may attach to my layout. I implemented my listener and detector in a fragment, with the listener forwarding the event to the detector.

Upvotes: 0

Noel
Noel

Reputation: 7410

The problem is that you're not actually registering your onTouchListener that is implemented in your Preview class. I don't know and I don't think (based on the API) you can register a touch listener for Fragments but you can for any of the views you have.

Try registering the onTouchListener for one of your FrameLayouts. You can do this after initializing your preview object:

preview = new Preview();
previewLayout.setOnTouchListener(preview);

That should do the trick.

Upvotes: 1

Related Questions