Development issue/problem:

How can I create a custom form with one click or button in Android?

When I click, I want to avoid touching an empty area.

Help us, please. Thank you so much!

How can I solve this problem?

Solution 1:

Interesting question. I’ve tried a few solutions, and I’ve found that it gives the same result as what you’re trying to achieve. The following solution solves 2 problems:

  1. A non-standard form as you imagined it.
  2. The top right part of the button must not be clickable.

So it’s a three-step solution:

Step 1

Make two shapes.

  • The first simple rectangular shape for a button: shape_button_beer.xml




  • The second form is used as a mask for the upper right part of the button: shape_button_beer_mask.xml. It is a simple circle with solid black paint.



Step 2

In the main format, add a button for the following approach:

  • RelativeLayout is the container for this custom button.
  • The first LinearLayout is a blue button with a beer icon and text in it.
  • The second image is the mask above the blue button. Here’s the dirty trick:
    1. The margins are negative to put the mask in the right place.
    2. We define the identifier so that it can be overwritten when clicked (see step 3).
    3. android:soundEffectsEnabled=false – so that the user does not feel that he has pressed something.


Step 3

In your main activity you define the click events for the button and mask as follows:

LinearLayout customButton = (LinearLayout) findViewById(;
customButton.setOnClickListener(new View).OnClickListener()
public null and void onClick(View arg0)
Toast.makeText(getApplicationContext(), Clicked, Toast.LENGTH_SHORT).show();

// Clicking on the mask does nothing
ImageView doNothing = (ImageView) findViewById(;
doNothing.setOnClickListener(new View.OnClickListener()
public void onClick(View arg0)

Here’s how. I know it’s not a perfect solution, but in the use case you describe, it may help.
I have tested it on my phone and here is what it looks like when you click on the blue zone and nothing happens on the other zones:

I hope that somehow helped.

Solution 2:

Use OnTouch instead of OnClick and check the alpha value of the image used in the button. If it’s not zero, do what you want.
Check the following code,

final bitmap; //Declare bitmap
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.TheImage) ;

onTouch public logical file (View v, event MotionEvent) {

int eventPadTouch = event.getAction();
float iX=event.getX();
float iY=event.getY() ;

switch (eventPadTouch) {

case MotionEvent.ACTION_DOWN:
if (iX>=0 & iY>=0 & iX

Solution 3:

Try this:

and create a circular.xml file in the :

Solution 4:

Using the layer sheet you can create any Vertex
gradient key shape here is an example

Use the radius values -ve to create a key shape as mentioned above.

Solution No 5:

I had a similar problem, but I didn’t want to rely on the code behind the code to examine the pixel value. I needed a simple way (no class overload) to limit the touch event to a part of the figure. Below I use LinearLayout to draw and placed a transparent button (with text) on it. I can adjust the keyboard to position the click zone.

Solution No 6:

The best and simplest solution (as4me) I found here is a subclass button and so it supports the selector. So just draw/add the appropriate pngs for each key state to use the selector, and declare OnClick in xml or add OnClickListener in code, and you’re ready to go.

Solution No 7:

Instead of making all these changes, use the edge layout for the button area, and hide the upper right part with a gadget (circular, like a rounded button) and don’t assign clicks to the listener in this area. This essentially hides the lower frame (i.e. your original button) and hides it with the inactive part.

Solution No 8:

I’ve tried the @Basim Sherif (link) answer and it works fine, but only if the button has the same size as the original image. If the button is stretched upwards, the clickable area is smaller, and if the button is set to a smaller size, the clickable area is larger than the button itself.

The solution is simple: scale the iX and iY values according to the source bitmap.

And this is my modified version of the code:

final bitmap; //Declare bitmap
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.TheImage) ;

onTouch public logical file (View v, event MotionEvent) {

int eventPadTouch = event.getAction();
float iX=event.getX();
float iY=event.getY() ;

// Gets the dimensions used in the display
int realW = this.getWidth();
int realH = this.getHeight() ;

// Gets the current image size
int bitmapW = bitmap.getWidth();
int bitmapH = bitmap.getHeight() ;

// Scale the display coordinates to the real image
float scaledX = iX * bitmapW / realW ;
float scaledY = iY * bitmapH / realH ;

(eventPadTouch) { case
MotionEvent.ACTION_DOWN :
, if (scaledX >= 0 & scaledY >= 0 & scaledX)

Good luck!

custom button android programmatically,android button selector background color,custom button android github,making custom buttons android,android button red,android button background color not working,android button style generator,transparent button with border android,android studio button color not changing,android change button color programmatically,android change button style programmatically,button layout android studio,button shadow android studio,xml style button,how to make circle button in android studio,what is the shape of baton,trapezoid shape android,oval button android,android custom button class,xml button create,make style button android,android button design example,android custom button,how to make custom drawable in android,change shape of button in android,how to make button more attractive in android