Friday, March 4, 2011

Adding Gradient Effects to Android Button?

Hi  All, Let us How to add gradient effects to default Android Buttons

Step 1: Create a Xml under drawable folder, i had created a XML file called custombutton

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#ff5500"
android:endColor="#999999"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#999999" />
<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="#ff5500"
android:startColor="#999999"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#999999" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>

<item>
<shape
android:shape="rectangle">

<gradient android:type="radial" android:gradientRadius="50"
android:startColor="#999999" android:endColor="#000000" />
<!-- <corners-->
<!-- android:radius="10dp" />-->
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
</item>
</selector>

Step2: Apply this to your Button by Just calling this file to the Background  of Button , as i had shown below

<Button android:text=" Back" android:id="@+id/back" android:background="@drawable/custombuttonnew"
android:cursorVisible="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true" />

That's it



4 comments:

  1. Thank you for the useful post. Is it possible to load the xml file from the Java code. I tried something like

    acButton = (Button) findViewById(R.id.environ_ac);
    Drawable d = Drawable.createFromPath(“/mnt/sdcard/Deployment/blue_button.xml”);
    acButton.setBackgroundDrawable(d);

    but it didn’t work

    ReplyDelete
  2. I'm not sure where you are getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for wonderful info I was looking for this information for my mission.
    My webpage :: Read Full Article

    ReplyDelete
  3. I was able to find good advice from your blog articles.
    Also visit my web page ... cheap sets for sale & chess boards

    ReplyDelete
  4. Very nice depth visit
    Also see my webpage: check it out

    ReplyDelete

Notification in Android Using AlarmManager, BoradCastReceiver & Services

    Our aim is to show notification message to user in a specific time.     Say For example , I have planned to show Notification ...