This question already has an answer here:
I want to change the style of a button dynamically, i.e. in Java code, something like:
((Button)findViewById(id)).setStyle("@styles/foo")
<resources>
<style name="foo">
<item name="android:adjustViewBounds">true</item>
<item name="android:maxHeight">100px</item>
<item name="android:maxWidth">200px</item>
</style>
</resources>
I have not seen nothing like setStyle, so:
do I have to change every single property or I can change the whole style?
Check out this question: How to programmatically setting style attribute in a view
The easiest way I found to circumvent this obvious flaw was to make to buttons. Make one of them
Visibility.gone
. Then simply changeVisibility
from the other one togone
and activate the first one byVisibility.visible
.I don't really like that solution, but it's faster and saner than the alternatives I found so far.
To assign a style like this
to a button dynamically you need to use both setBackgroundResource() and setTextAppearance() functions. E.g.:
where
is a name of .xml file which describes a selector for your button.
You might also want to take a look at 9 patch images. These are very useful for adjusting the image size and text tied to your widget or button based on the current device running your application.
9 patch
9 patch draw tool