Vitor Villar
Vitor Villar

Reputation: 1925

Android - Put button style into theme

i created this drawable to use in my buttons in my application:

btnprimary.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btnprimary_disabled" android:state_enabled="false" />
    <item android:drawable="@drawable/btnprimary_pressed" android:state_enabled="true" android:state_pressed="true" />
    <item android:drawable="@drawable/btnprimary_focused" android:state_enabled="true" android:state_focused="true" />
    <item android:drawable="@drawable/btnprimary_enabled" android:state_enabled="true" />
</selector>

Every state has a drawable too, so, for example, this is the file for btnprimary_enabled

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="5dp"
        />
    <gradient
        android:angle="90"
        android:startColor="#335BB8"
        android:endColor="#6397FF"
        android:type="linear"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <stroke
        android:width="1dp"
        android:color="#878787"
        />
</shape>

So, i can use this in a button like this, using the background property:

<Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btnprimary"
        android:text="@string/login"
        android:textColor="#FFFFFF"
        android:gravity="center"/>

And i have my theme on styles.xml, but is empty now. Like this:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>

My Question is, can i put those configuration that i created on drawable into my theme, and all buttons has this style, without need to add the background property?

Upvotes: 1

Views: 664

Answers (1)

Ajay Shrestha
Ajay Shrestha

Reputation: 2455

Have you tried this code ? In your style file, define the button style

<style name="ApplicationStyle" parent="android:Theme">
<item name="android:buttonStyle">@style/yourButton</item>
</style>

Upvotes: 2

Related Questions