0

I am trying to make it so when you click this "Play" bitmap it will run other code the thing is i don't know how to check for it.

I tried drawing it as

playX = WIDTH / 2;
        playY = HEIGHT / 2;
        Bitmap playButton = BitmapFactory.decodeResource(getResources(), R.drawable.play_button);
        playWidth = playX + playButton.getScaledWidth(canvas);
        playHeight = playY + playButton.getScaledHeight(canvas);

Then a touch Event

@Override
public boolean onTouchEvent(MotionEvent event){
    if(event.getX() > playX && event.getX() < playWidth){
        if(event.getX() > playY && event.getY() < playHeight){
            System.out.println("Button Pushed");
        }
    }}

but it doesn't seem to work i dont know if there is a better way to run code when a bitmap is clicked? (I am only using the java class not xml)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

1

Actually you can just use imageView to hold the Play image and set onClickListener function to have a clickable image.

Something like this:

ImageView imageView = findViewById(R.id.image_view_icon);
imageView.setImageResource(R.drawable.play_button);
imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    System.out.println("Button Pushed");
                }
            });

Edit:

As mentioned in comments that you are using Canvas drawing, try this instead:

@Override
public boolean onTouchEvent(MotionEvent event) 
{
    int x=(int)event.getX();
    int y=(int)event.getY();
    if (drawable.getBounds().contains(x,y)  &&
        event.getAction()==MotionEvent.ACTION_DOWN) {
        System.out.println("Button Pushed");
        return true;
    }
    return false;
}

Please refer to this for details How to make a bitmap using canvas clickable?

Hope this is what you want:)

Community
  • 1
  • 1
Jiyeh
  • 5,187
  • 2
  • 30
  • 31
  • So i put this in the MotionEvent method? and there is no "R.id.image_view_icon" ? – Kingbluesapphire Nov 22 '16 at 02:48
  • You mean you don't have a xml ImageView for this? Where did you set your image? – Jiyeh Nov 22 '16 at 02:54
  • Its in the res folder in a "drawable-nodpi" folder. – Kingbluesapphire Nov 22 '16 at 02:56
  • What is the view that you said "a bitmap is clicked", can you edit your question by putting some code of that? – Jiyeh Nov 22 '16 at 03:05
  • Sorry i'm new to Andriod, I don't know what you mean, i draw the bitmap to the screen and i want when that bitmap the "R.drawable.play_button" is clicked on it run certain code – Kingbluesapphire Nov 22 '16 at 03:58
  • Im not using the xml file only using the .java to draw to the screen – Kingbluesapphire Nov 22 '16 at 06:02
  • Are you sure you are using .java file? Can you post the file? .xml file is used to construct the UI in Android. Check this official doc https://developer.android.com/guide/topics/ui/declaring-layout.html. – Jiyeh Nov 22 '16 at 07:40
  • All i have is It drawing different bitmaps to the screen, would I have to make my buttons in the xml? Would they render over my other pictures? Oh and it's just a bunch of Cavnas' drawText of drawBitmap methods – Kingbluesapphire Nov 22 '16 at 10:39
  • Forgot to say this is for a 2D game not form or anything like that not sure if that changes anything – Kingbluesapphire Nov 22 '16 at 10:47
  • Oh dear you are using Canvas, please refer to edited answer above and the link as I am not good in Canvas. – Jiyeh Nov 22 '16 at 11:01
  • What is drawable.getBounds()? I don't have anything named drawable (I tried R.drawable and doesn't seem to have getBounds. Sorry if i am missing something obvious – Kingbluesapphire Nov 22 '16 at 22:47
  • Try this to get your drawable, `Drawable drawable = getResources().getDrawable(R.drawable.background_border_grey);` – Jiyeh Nov 23 '16 at 02:39
  • Sorry to keep bother you but that doesn't seem to work, this is my code [ http://pastebin.com/3a6jL2ww ] am i doing something wrong? The only thing i am guessing is it can't get the bounds because the drawable isn't connected to the bitmap being drawn but I'm not sure. – Kingbluesapphire Nov 23 '16 at 03:52
  • It prints out "I/System.out: Drawable bounds: Rect(0, 0 - 0, 0)" am i doing something wrong i tried using your code exactly? – Kingbluesapphire Nov 23 '16 at 04:37
  • Can you try with the accepted answer in http://stackoverflow.com/questions/18826808/how-to-make-a-bitmap-using-canvas-clickable? – Jiyeh Nov 23 '16 at 04:47