Иван Никитин Иван Никитин - 2 months ago 11
Android Question

Button's style Android

Who can help me with style button? How I must change button's style, that I haven't problems with angles, I mean, Why I have default style of button.
this is my code and display problem

Angels1

Angles2

Button style="@style/gradient_button"


style - gradient_button

<style name="button_common">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">14sp</item>
</style>
<style name="gradient_button" parent="@style/button_common">
<item name="android:background">@drawable/button_grad</item>
</style>


and file button_grad

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="false"
android:drawable="@drawable/b59" />
<item
android:state_enabled="true"
android:state_pressed="true"
android:drawable="@drawable/y59" />
<item
android:state_enabled="false"
android:drawable="@drawable/b59" />
</selector>


b59 and y59 these are image with 9-patch.

Answer

You can write the style for button in your drawable folder:

style_button.xml :

<style name="button_common">
 <item name="android:layout_height">wrap_content</item>
 <item name="android:layout_width">wrap_content</item>
 <item name="android:textColor">#ffffff</item>
 <item name="android:textSize">14sp</item>
</style>

selector.xml in your drawable folder:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_pressed="false"
        android:drawable="@drawable/b59" />
    <item
        android:state_enabled="true"
        android:state_pressed="true"
        android:drawable="@drawable/y59" />
    <item
        android:state_enabled="false"
        android:drawable="@drawable/b59" />
</selector>

Apply that style to your button :

<Button
                android:id="@+id/btn"
                style="@style/style_button"
                android:layout_centerVertical="true"
                android:background="@drawable/selector"
                android:drawablePadding="10dp"
                android:text="btnText" />