Question

I have 3-4 activities in the application and all of them have some event listeners that work nicely. However only on one activity i simply can't get the event handling to work. I tried the solution from this thread:http://www.anddev.org/view-layout-resource-problems-f27/ontouch-not-called-t16578.html It doesn't work for me. I tried to manually set OnClickListeners for ImageViews from java code, android:onClick from XML.

It seems that some other component handles all the events, or my activity doesn't have some permission to handle events. Should I put something in the AndroidMainfest.xml for my activity that enables handling events?

Hope someone has the idea what should i try, here's the code:

Activity:

package com.renegade.begining;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;


public class NotesOnStaff extends Activity{
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.notes_on_staff);


   /*     NoteView2 note=(NoteView2)findViewById(R.id.note_red);


        Bitmap bm=BitmapFactory.decodeResource(getResources(), R.drawable.note_red);
        note.setImage(bm);
     */   



    }


    public void onClickEventBtn1(View v) {
      // TODO Auto-generated method stub
       ImageView keyboard_letter=(ImageView)findViewById(R.id.keyboard_letters);
       keyboard_letter.setVisibility(View.INVISIBLE);
   }

    public void onClickEventBtn2(View v) {
      // TODO Auto-generated method stub
       ImageView keyboard_letter=(ImageView)findViewById(R.id.keyboard_letters);
       keyboard_letter.setVisibility(View.VISIBLE);
   }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
       switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
         ImageView keyboard_letter=(ImageView)findViewById(R.id.keyboard_letters);
           keyboard_letter.setVisibility(View.VISIBLE);
         return true;
      case MotionEvent.ACTION_UP:
        return false; // something else
                default:
                        return false;// all others
      }
    }


}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/notes_on_staff_layout"
  android:background="@drawable/background_ver"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">


            <ImageView
              android:id="@+id/notes_keyboard"
           android:src="@drawable/keyboard"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:layout_alignParentBottom="true"
             android:layout_centerHorizontal="true"
           android:layout_marginBottom="20dip"   

         android:clickable="true" android:focusable="true"/>

      <ImageView
           android:id="@+id/btn_hide"
           android:src="@drawable/btnhide"
           android:layout_alignParentLeft="true"
           android:layout_marginTop="40dip"
           android:layout_marginLeft="10dip"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
            android:onClick="onClickEventBtn1"
         android:clickable="true" android:focusable="true"/>

      <ImageView 
         android:id="@+id/btn_show"
          android:layout_alignBottom="@id/btn_hide"
          android:layout_toRightOf="@id/btn_hide"
         android:src="@drawable/btnshow" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"
         android:onClick="onClickEventBtn2"
          android:clickable="true" android:focusable="true"/>   



      <ImageView

           android:id="@+id/staff"
           android:src="@drawable/staff"
           android:layout_below="@id/btn_hide"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="0dip"
           android:layout_above="@id/notes_keyboard"


         />   


      <com.renegade.begining.NoteView2 
        android:id="@+id/note_red"
      android:layout_width="wrap_content"
       android:layout_height="wrap_content"


           />      


      <ImageView 
         android:id="@+id/keyboard_letters"
          android:layout_alignLeft="@+id/notes_keyboard"
          android:layout_alignBottom="@+id/notes_keyboard"
          android:layout_marginBottom="10dip"
          android:visibility="invisible"
         android:src="@drawable/keyboard_letters" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"
          android:clickable="true" android:focusable="true"/>   


</RelativeLayout>
Was it helpful?

Solution

Your EventListener is absorbing all DOWN events. Try having it return false instead of true so subviews get a chance to react as well, as I think for onClick to fire properly the child view needs to register a continuous DOWN then an UP (just an UP isn't enough; think about sliding your finger off of a button onto another one then releasing; neither fires as that's an indication that the user wants to cancel.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top