1

I have defined two styles in Style.xml, I want to change the style programmatically for certain conditions, Min SDK version 21

<style name="CheckOutButtonStyle" parent="@style/Widget.AppCompat.Button.Colored">
        <item name="android:layout_height">60dp</item>
        <item name="android:fontFamily">sans-serif-medium</item>
        <item name="android:textSize">16sp</item>
        <item name="android:padding">0dp</item>
        <item name="android:textColor">@color/white_color</item>
        <item name="android:layout_margin">@dimen/general_spacing</item>
    </style>


    <style name="ScanNextItemSecondaryButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:fontFamily">sans-serif-medium</item>
        <item name="android:stateListAnimator">@null
        </item> <!-- remove the shadow, per UI request -->
        <item name="android:textSize">16sp</item>
        <item name="android:padding">0dp</item>
        <item name="android:minHeight">0dp</item>
        <item name="android:minWidth">0dp</item>
        <item name="android:background">@null</item>
        <item name="android:textColor">@color/colorSecondaryAccent</item>
        <item name="android:layout_marginBottom">@dimen/general_spacing</item>
    </style
Sam
  • 6,215
  • 9
  • 71
  • 90
  • Have you used the search functionality? [programatically set style](https://stackoverflow.com/questions/2016249/how-to-programmatically-set-style-attribute-in-a-view) – sansa Feb 09 '19 at 03:53
  • view.setTextAppearence(R.style.CheckOutButtonStyle) will modify only text related attributes i.e. fontfamily, textsize and textcolor. Others can not be set by this method using style. But as you know they can be set programmitically by other means. – amitava Feb 09 '19 at 10:29

1 Answers1

-1
1. Create xml in drawable. 
\res\drawable\back_button_answer.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <corners android:radius="10dip" />
   <!-- background -->

   <gradient
                android:startColor="#D6D7D6"
                android:centerColor="#E2E2E2"
                android:centerY="0.75"
                android:endColor="#D6D7D6"
                android:angle="270"
        />

   <stroke android:width="2dip" android:color="#fff"/>
</shape>


2. Button btn.setBackgroundResource(R.drawable.back_button_answer);
TOUSIF
  • 285
  • 5
  • 4