0

I defined two attributes for button style like

<!-- attrs.xml-->
<declare-styleable name="MyCustomButton">
    <attr name="rectangleCustomButtonStatus1" format="reference" />
    <attr name="rectangleCustomButtonStatus2" format="reference" />
</declare-styleable>

<!-- styles.xml -->
 <style name="AppTheme">
       <item name="rectangleCustomButtonStatus1">@style/GoldButtonStyle</item>
       <item name="rectangleCustomButtonStatus2">@style/WhiteButtonStyle</item>
       ....
 </style>

GoldButtonStyle and WhiteButtonStyle i defined in style.xml itself.

now i am applying above defined attribute syle in xml like

 <Button
  style="?attr/rectangleCustomButtonStatus1"
  ..... />

I am using Recyclerview, Recyclerview item has one button. Button text will be changed based on status like pending, completed. I want to set attribute style like if status is pending then use rectangleCustomButtonStatus1 and if status is completed then use rectangleCustomButtonStatus2.

So how can i give style attribute programmatically? same thing i have to do for recyclerview itemview's item. so how can i achieve this?

Sanjay Prajapati
  • 241
  • 3
  • 19
  • if you want to pass style programatically then use this in your logic: R.style.rectangleCustomButtonStatus1 – Umair Dec 28 '17 at 12:54
  • have you tried @Umair ?? because i tried every possible solution. and at the end i use two button for temporary solution. but i want it with only one button. – Sanjay Prajapati Dec 28 '17 at 13:15
  • I have used this approach in dialogs. But i am sure it will work on buttons too. what have you tried so far ? can you post your adapter code ? – Umair Dec 28 '17 at 13:17
  • One more thing that R.style.rectangleCustomButtonStatus1 is not style its attribute which i defined in attrs.xml. so you can't refer with R.style – Sanjay Prajapati Dec 28 '17 at 13:23
  • yes you are right, I am just giving you the idea that how it is done. By the way if you want to change basic design of your button you can do that with if else condition too in your code. Because styles can be applied to a View at initialization phase. – Umair Dec 28 '17 at 13:25
  • ya i can achieve with giving background propery but i have to do attribute only. because i have to give multiple color support for app. So for that i just remain to define different color AppTheme. – Sanjay Prajapati Dec 28 '17 at 13:39

0 Answers0