8

How can I create a custom button like this?

enter image description here

It should be just clickable area not a real button.

xpepermint
  • 35,055
  • 30
  • 109
  • 163

4 Answers4

26

I use a crapload of irregular shaped buttons on my app, and to change the "hot zone" or "clickable area" of the button, I just use the Bitmap.getPixel() method to check for alpha on the image used. If the method returns 0, then don't perform click event.

Example: (1) Create your button as usual, whichever way you would like. (2) Define a Bitmap and assign to it the same image drawable used for the button. (3) Get the X and Y coordinates of the touch or click action. (4) Pass the coordinates to the .getPixel(x,y) method.

Sample Code:

// ** Declare your Bitmap somewhere **
final Bitmap TheBitmap;       
TheBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.TheImage);

// ** My onTouch code **
public boolean onTouch(View v, MotionEvent event) {

        int eventPadTouch = event.getAction();

        switch (eventPadTouch) {

            case MotionEvent.ACTION_DOWN:
                if (iX>=0 & iY>=0 & iX<TheBitmap.getWidth() & iY<TheBitmap.getHeight()) { // ** Makes sure that X and Y are not less than 0, and no more than the height and width of the image.                
                    if (TheBitmap.getPixel(iX,iY)!=0) {
                        // * A non-alpha area was clicked, do something 
                    }               
                }
                return true;                
        }           
        return false;
}

The event.getX() and event.getY() simply give you the coordinates of where you touched the button.

** The above sample is to guide you in the correct direction. There are some checks to add to the code to assure no errors occur.

JJT
  • 407
  • 5
  • 12
0

ceate using your own canvas or make a image using photoshop like this and then using Bitmap.createScaledBitmap scale it according to dimension of your button and hence you will get this button. using canvas you have to write more code just do this it will work fine

mathlearner
  • 7,509
  • 31
  • 126
  • 189
0

Simply just create an image like that and you can use either ImageView or Button w/o text, and implement an OnClickListener. It just works!

Pete Houston
  • 14,931
  • 6
  • 47
  • 60
-2

save that as a png and and put it in your drawables folder. Then in your xml use something like this

<Button
android:height="wrap_content"
android:width="wrap_content"
android:background="@drawable/your_png"
/>

I am not 100% positive that the corner cut out is going to work properly. That corner area that is gone may end up being clickable. If that is the case and if you don't want it to be then you'll have to slice your picture in half somewhere create two buttons that you can set next to each other to make up that shape and use the same click listener for both. From the users perspective it will still seem like one button.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • "That corner area that is gone may end up being clickabl"... yes that is why I'm asking for the solution. – xpepermint Oct 15 '11 at 14:57
  • Then one option you have is to follow the second bit I said there: Slice your png into multiples that fit together to make the shape you want and put the same click listeners on both pieces. – FoamyGuy Oct 15 '11 at 15:29
  • This not answer his question of having an irregular clickable area; having multiple buttons is just a workaround, not the solution he's looking for. – SoManyGoblins Jun 17 '14 at 14:18
  • @SoManyGoblins since it's been chosen as the accepted answer by OP I can't delete it... Not much I can do at this point. – FoamyGuy Jun 17 '14 at 14:50
  • I am aware that it's old, just justifying my downvote. – SoManyGoblins Jun 17 '14 at 15:51
  • This needs question to be moderated - this clearly is the wrong answer – Nostradamus Jun 07 '20 at 01:08