Reputation: 110550
I have created my own button which inherits android's RelativeLayout. My question is how can I apply android' button style (i.e. background drawable, padding) to my own button?
Thank you.
Upvotes: 2
Views: 2314
Reputation: 323
in your drawable folder - create this xml say custom_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#E77A26" />
<stroke
android:width="1dp"
android:color="#E77A26" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#70c656"
android:endColor="#53933f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#53933f" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
And in your main layout -
<Button
android:id="@+id/connect"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/custom_button"
android:layout_marginRight="10dp"
android:text="@string/connect" />
if you want to put styles to your text in that button - add a style resource in your strings.xml - like below
<style name="buttonText">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#ffffff</item>
<item name="android:gravity">center</item>
<item name="android:textSize">15dp</item>
<item name="android:textStyle">bold</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">2</item>
</style>
You can use this style in your button as below,
style="@style/buttonText"
Thats it...
Upvotes: 2
Reputation: 23596
As i got you. You might be like to implement like this. Also see this For Example.
There are style define for the Button and you can use that style to your button when ever you want.
Hope that's what you want. If not then let me know.
Enjoy. :)
Upvotes: 0
Reputation: 9117
For clarification, why would you inherit relative layout for creating a custom button? If you just need to change the appearance of the button, you could do it with specifying a selector drawable.
If at all, there is a requirement for inheriting RelativeLayout, then your code will be a little more, where you will need to handle the states of your button. If that's done properly, you can attach a background selector drawable as blessenm has mentioned.
Upvotes: 0
Reputation: 31779
Im not completely sure what you are asking. But if you are just looking for the default button background just use
android:background="@android:drawable/btn_default_normal"
Padding might need to be manually done.
This link gives the list of android resources.
Upvotes: 0