简体   繁体   中英

how to create custom shaped buttons in android

I knew this question is a duplicate but i couldnt figure out the solution for creating custom shaped buttons say circular,oval etc.

Doubt1: My objective is to perform an action only when the area inside the shape is clicked, nothing should be performed just outside the shape( say only circular area has to be atttached with a listener)

Doubt 2: Can i use png pictures of the shapes as backgrounds or should I use only shapes to draw them. Pls give me a simple solution...

Thanks in advance..

You can use ImageButtons and set oval or circular shaped images as background. I hope it helps

try this link it will help you to create custom button. http://angrytools.com/android/button/

all you have to do is save the button.xml file in your drawable folder and then set your button background as

android:background="@drawable/button.xml"

You should use ShapeDrawable for the job. If you made a circle button for example, nothing will happen if you clicked outside the circle area.

For example inside your drawable folder, make xml file circle_button:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#9F2200"/>
    <stroke android:width="2sp" android:color="#fff" />
</shape>

and in your button, set background="@drawable/circle_button"

As @MSaudi said ShapeDrawable will give you shaped buttons.

If you wanted to use PNG's that is more difficult.

I would approach it by extending the Button class and overriding the onTouchEvent method to only register a click when inside the shape required (possibly by checking if the view is transparent at the click location).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM