Customize button in Android
hi!!
You can change the appearance of button in android like make the corners of button rounded etc…
button_shape.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#EAEAEA"/> <corners android:bottomLeftRadius="8dip" android:topRightRadius="8dip" android:topLeftRadius="1dip" android:bottomRightRadius="1dip" /> </shape>
here you can change the radius of corners
you can use this button_shape in your button’s code as
android:background=”@drawable/button_shape”
here’s the code
main.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello Android from NetBeans"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nishant Nair" android:padding="5dip" android:layout_gravity="center" android:background="@drawable/button_shape" /> </LinearLayout>
you can add an image to your button by using drawable
android:drawableTop=”@drawable/pdf”
in your button’s xml code
Enjoy.. π !!!!!!!
Hi friend,
Thnx for such tutorial.
Nice one.
thanks π
Hello Nishant,
itΒ΄s a nice tutorial π
I still have a question: Is it also possible to make such buttons without xml, only using java code?
Yup,
you can do it.
Sugoi neeeh!!! :DDDD
I’ll definitely try this!!
Arigato! π
π
I’m looking for someway to change the timepicker to heightpicker. I want the look of timepicker to select ft and inches. I think the timepicker UI is perfect for this but struggling to change it for my needs. Can you help?
Hi!
I have not tried timepicker yet, so i can’t comment on that.
Sorry.
Let me know if you need any other help, I will try timepicker later on.
Hey buddy, thanx for sharing this… i was wondering if you could write something about adding a shadow to a button…
Hi,
Instead of radius can we clip it horizontally on the topLeft and the bottomLeft to look like a Android back button?
A back button like the one in your link is what I’m looking for too. So far I accomplished this by using a 9patch png image. but would prefer to use a regular custom button to accomplish this.
@Nishant nair: Nice tutorial man. It helped me a lot.
@Imran: Good tip. It is useful in creating back button in our view.
Thanks.
Hello Nishant,
Great article…
I want to develop Knob type circular button, can you have pointers how to do it in android?
Thanks,
JRC
Nice Tutorial …
This approach is way simpler than working with the Canvas object in Java. Thank you for the easy tutorial!
Where would I place my button_shape.xml file?
in your drawable folder
Hello Nishant,
How to make it Clickable……where should we mention the states for the Button…..
Bottom left and right is like the opposite! The left side at the bottom is called “Bottom Right”! Why is this?
That’s a good question. It seems like this was fixed in the newer android versions.
Give me an error …
“Premature end of file”
Please help
I never got such error. Can you check it again, closing all tags.
Hi Nisant. It builds and runs but gives the force close message. Do you know why this can be? What am I missing?
Here’s what I did in Netbeans:
1. Create drawable folder in res folder.
2. Create button_shape.xml file in the drawable folder.
3. Copy first given code snippet to button_shape.xml
3. Replaced code in main.xml with second given code snippet.
4. Run.
Thanks a million.
Hey Hennie, did you checked your logcat ?.
Awesome..Thanks buddy.
Thank u very much.
Thanks a lot man!! IΒ΄ve been looking for this for quite a while π
Thanks a lot
Hi
It is very nice and useful topic
Thanks
sir mujhe android linear layout me button ko charo tarf se round krna hai
how
Fast and direct!
Thanks! You Rock!
Good writeup, I am normal visitor of ones blog, maintain up the excellent operate, and It’s going to be a regular visitor for a lengthy time. bdbkegdkbeeb
Hey I am so excited I found your website, I really found you afccdefegdkf