Search code examples
androidandroid-buttonandroid-themeandroid-styles

Change theme for button programmatically


I can able to set theme to button using android:theme attribute in xml. Is there any equivalent programmatical code?

<Button
        android:id="@+id/btnPayment"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="Add Payment"
        android:theme="@style/ButtonPeter" // theme..
        />

style.xml

<style name="ButtonPeter" parent="Widget.AppCompat.Button">
        <item name="colorButtonNormal">@color/fbutton_color_peter_river</item>
        <item name="android:textColor">@android:color/white</item>

    </style>

Can I able to set theme dynamically using something like this btnPayment.setTheme(R.style.ButtonPeter) ?

I search a lot but all posts are related to set style or create dynamic button and apply style to it but I dont want to do that .I want to set theme to button

How to achieve this?


Solution

  • I think you are confused with style and theme. From this document, it says: theme for the whole activity while style is for view.

    A style is a collection of properties that specify the look and format for a View or window.

    A theme is a style applied to an entire Activity or application, rather than an individual View.

    For this code:

    android:theme="@style/ButtonPeter" // theme is used but only valid style attributes for button will be applied
    

    Hope this help.