Shashank Setty
Shashank Setty

Reputation: 138

ImageButton with rounded corner using xml only?

I'm trying to make a ImageButton like this

this is the type of button I'm trying to replicate

however, I can't seem to find a way to do it using xml only. I tried custom libraries, they don't seem to work perfectly. is there no other way? what can I do instead of using ImageButton?

Upvotes: 2

Views: 1234

Answers (3)

yadunath.narayanan
yadunath.narayanan

Reputation: 285

You can use cardview with card corner radius

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="4dp"/>

And add imagebutton inside cardview

Upvotes: 0

Rajat Mittal
Rajat Mittal

Reputation: 423

Create an XML file inside drawable.

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

    <corners
            android:radius="28dp"
    />

    <solid
        android:color="#00BADE"
/>
</shape>

and then inside your imageButton or whatever.

    android:background="@drawable/buttonShape"

Upvotes: 0

MetaSnarf
MetaSnarf

Reputation: 6187

Here is an xml code.

1.Create a xml file in your drawable folder like button.xml and paste the following markup:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dp" />
            <stroke android:width="1dp" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92"  />            
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <solid android:color="#58857e"/>       
        </shape>
    </item>  
    <item >
       <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />            
       </shape>
    </item>
</selector>

2.Now use this drawable for the background of your view. If the view is button then something like this:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textColor="#ffffff"
    android:background="@drawable/mybutton"
    android:text="Buttons" />

Credits to this answer

Upvotes: 1

Related Questions