Martin
Martin

Reputation: 133

how do I put a button on an image

I need to put buttons in different places on an image, its a floorplan and I want to use the image for navigation to each of the rooms.

I have an idea that I need two images the top one holds the floorplan and the bottom one holds the buttons - which are really different coloured areas. I want to show the user the top image but get touches from the bottom picture. If I can get the colour of the button/area the user has clicked then I can tell what room they want to go to and I'll be able to have my buttons any shape I like.

I found the idea but I can't find any tutorials or instruction to help me. I'm currently looking at GL ES 1.0 but I'm not sure if its the right way to do it.

Any help would be very much appreciatted

thanks

Upvotes: 0

Views: 147

Answers (2)

Thommy
Thommy

Reputation: 5417

You can set an OnTouchListener to your Image:

image.setOnTouchListener(new View.OnTouchListener() {

 @Override
 public boolean onTouch(View v, MotionEvent event) {
 event.getX();
 event.getY();
 return false;
      }
});

With getX and getY you will get the coordinates of the picture which has been pressed. Now you just need a list of the areas which respond to the "clicks".
I suggest you do that with a list of Rect-Objects. Every Rect-Object represents a clickable rectangle inside your picture. A Rect object provides the method contains(x, y) to test if the coordinates delivered from the onTouchListener are inside it.

Upvotes: 3

sealz
sealz

Reputation: 5408

Sorry I answered before I fully read the question. My solution is a simpler way but will not allow you to form and shape them as well as your idea. Let me play around and maybe I can update this answer. Good Luck

If I understand you correctly you can place the buttons over the image and then set them to invisible. This way they can't be seen but can still be pressed.

View b = findViewById(R.id.button); 
b.setVisibility(View.GONE);
//I can't remember off the top of my head but it might be
b.setVisibility(View.INVISIBLE)

And incase my code is off here is a backup Link

Upvotes: 1

Related Questions