2
\$\begingroup\$

In my android game, there is a surfaceview which has 36 bitmaps drawn on it. I want to change the bitmaps when they are touched. Is there a way implement touch or click event to the bitmaps ?

Adding touch event to surfaceview and then to calculate the bitmap touched will be very tedious since there are 36 bitmaps.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

If i correctly understood you, then I believe, you can add a click listener to a bitmap just like to a jpg image or other format.

In my example the image_star is a png file.

ImageView img = (ImageView) findViewById(R.id.imageView1);

img.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //some code
        }
    });

The xml :

    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/imageButton1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="62dp"
    android:src="@drawable/image_star" 
    />

The result of this would be lower. The picture with a star in the middle is a clickable image, the ImageView img from my code. The upper button is an ImageButton.

enter image description here

Also there is a way to make it clickable only through the xml code, but im not sure how that is done, because i dont like that way.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ Welcome to GD:SE, This is a very short answer and we generally try to provide as much concise information as needed to solve the question - can you add some example code to show it's implementation? \$\endgroup\$ Jul 22, 2014 at 11:55
  • \$\begingroup\$ @Blue I have added some more to my answer! \$\endgroup\$
    – lxknvlk
    Jul 22, 2014 at 18:01
  • \$\begingroup\$ This is much better! I encourage you to give more answers where you can and look into older, highly upvoted answers to see what to aim for! \$\endgroup\$ Jul 23, 2014 at 8:31
  • \$\begingroup\$ hey i am drawing button bitmap, is it any way to set click method to them as i hadn't mentioned any imageView or button in my xml code \$\endgroup\$ May 1, 2015 at 8:53
0
\$\begingroup\$

a Bitmap itself cannot be clickable on Android. You have to draw each Bitmap to an ImageView or an ImageButton, and listen to that view's touch events.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .