views:

185

answers:

1

Hi everyone,

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>
A: 

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.)

Yoni Samlan
Thx for reply.I see your point, but the problem is that my Activity doesn't register click and touch events at all. It never enters onTouch() and onClick() functions(i checked that while in debug mode).I don't need Activity.onTouch() implemented at all, i just wanted to check does the event get handled there. And it doesn't.
Milos Pesic
Your keyboard_letters view is invisible but clickable; it seems possible that it's overlapping your buttons and preventing their clicks from firing. Try removing it and the touch event handler and see if your buttons fire then?Only other thing I can think of is, there's no source given for NoteView2, which maybe is setting its touchable area inappropriately and keeping other views from getting focus.(More generally, try starting from the minimum layout and activity of just your two buttons and two button click listeners, and add views/methods in one at a time until you find the culprit).
Yoni Samlan
Thx once again for your help. I will build the layout from scratch and post the results here.
Milos Pesic
I forgot to post results. The problem was in custom extended View NoteView2(the problem you stated as possible) for which i called bringToFront() upon inflating from XML. In that scenario the NoteView2 covered up whole screen and blocked onClick events for other views on the screen. Thx for your help!
Milos Pesic