Gabrielle
Gabrielle

Reputation: 4991

Background for a button in android?

In my app I have set for a button the background to a certain color. I don't like how it looks like.The button is like a rectangle, with straight edges. I want that the button to have rounded corners. How is it possible?

Here is my button in xml :

<Button android:id="@+id/button" android:text="Participer au concours de photos"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginTop="410dip" android:layout_centerHorizontal="true"
         android:background="@drawable/orange1"/>

Thanks...

Upvotes: 2

Views: 500

Answers (2)

Pankaj Kumar
Pankaj Kumar

Reputation: 83028

There are two ways :

  1. Use 9 patch drawables (png image) here is complete tutorial 9 patch drawables: background of a customized Button

  2. Set xml drawable as background of your buttons.

Take a look here Gradient buttons for android.

And to know more about xml drawables :- ANDROID XML DRAWABLES

Upvotes: 1

Mojo Risin
Mojo Risin

Reputation: 8142

Use selector

<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" >
    <shape>
        <gradient
            android:startColor="#24a1dd"
            android:centerColor="#158ee4"
            android:endColor="#37b8ee"
            android:angle="270"
            android:centerY="0.88" />
        <stroke
            android:width="3dp"
            android:color="#00ffffff"
            />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <gradient
            android:endColor="#59bfe6"
            android:centerColor="#36aced"
            android:startColor="#91def7"
            android:angle="270"
            android:centerY="0.88" />
        <stroke
            android:width="3dp"
            android:color="#00ffffff"
             />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item>        
    <shape>
        <gradient
            android:endColor="#e1e1e1"
            android:centerColor="#bdbebd"
            android:startColor="#f6f2f6"
            android:angle="270"
            android:centerY="0.88" />
        <stroke
            android:width="3dp"
            android:color="#00ffffff"
            />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>
</selector>

Something like this should do the work. Just fix the color values. Which is the same as the the answer of this question Standard Android Button with a different color

Upvotes: 3

Related Questions