maria
maria

Reputation: 53

Touch event doesn't work on an ImageView

I am trying to add an image button when a touch event occurred on an image. Image is displayed using ImageView. But nothing happens when I touch on the image. Codes attached here

Quick.java

 ImageView myImageView = (ImageView)findViewById(R.id.imgView);
   Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.india_map);
   myImageView.setImageBitmap(bitmap);

    myImageView.setOnTouchListener(new OnTouchListener()
 {

     @Override
     public boolean onTouch(View v, MotionEvent event)
     {
         ImageButton  imgbutton=(ImageButton)findViewById(R.id.imageButton1);
         Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.image5);  
         imgbutton.setImageBitmap(image); 
         imgbutton.setVisibility(View.VISIBLE);
                 return false;
     }   

});

activity_quick.xml

 <LinearLayout
            android:id="@+id/tab2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="5dp" >

         <ImageView
             android:id="@+id/imgView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:contentDescription="@string/desc"
            android:src="@drawable/india_map"/>
          <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/image5"
                android:contentDescription="@string/desc"
                android:visibility="invisible" />

                </LinearLayout>

Please help me to solve this problem

Upvotes: 0

Views: 2007

Answers (4)

JiTHiN
JiTHiN

Reputation: 6588

Use FrameLayout to put a view over the other.

Try the following:

<FrameLayout
            android:id="@+id/tab2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="5dp" >

         <ImageView
             android:id="@+id/imgView"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:scaleType="fitXY"
             android:contentDescription="@string/desc"
             android:src="@drawable/india_map"/>

          <ImageButton
             android:id="@+id/imageButton1"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:src="@drawable/image5"
             android:contentDescription="@string/desc"
             android:visibility="gone" />

 </FrameLayout>

No need to set the Image bitmap in java code. You've already specified them in xml.

Upvotes: 1

Chansuk
Chansuk

Reputation: 792

Is there any special reason to use TouchListener? (ex> for handling pinch or swipe gesture)

If what you consider is just 'handling click event of image', I think it's better & easy to use OnClickListener instead of OnTouchListener. For example...

    View iv = findViewById(R.id.imgView));
    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageButton  imgbutton=(ImageButton)findViewById(R.id.imageButton1);
            Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.image5);  
            imgbutton.setImageBitmap(image); 
            imgbutton.setVisibility(View.VISIBLE);
    });

And one other thing you may consider is always it's not good idea to decode a bitmap in a main UI thread, so how about consider use another thread or AsyncTask to handle bitmap.

Upvotes: 0

NightSkyCode
NightSkyCode

Reputation: 1141

Set your imageview as clickable. In the xml use android:clickable = "true"

Upvotes: 0

dipali
dipali

Reputation: 11188

 myImageView.setOnTouchListener(new OnTouchListener()
 {

     @Override
     public boolean onTouch(View v, MotionEvent event)
     {
         ImageButton  imgbutton=(ImageButton)findViewById(R.id.imageButton1);
         Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.image5);  
imgbutton.setImageBitmap(null);        
 imgbutton.setImageBitmap(image); 
         imgbutton.setVisibility(View.VISIBLE);
                 return false;
     }   

});

first of all you have set image bitmap null and then set other bitmap.

Upvotes: 0

Related Questions