I’m getting a view from the XML with the code below:
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);
I would like to set a “style” for the button how can I do that in java since a want to use several style for each button I will use.
Generally you can’t change styles programmatically; you can set the look of a screen, or part of a layout, or individual button in your XML layout using themes or styles. Themes can, however, be applied programmatically.
There is also such a thing as a
StateListDrawablewhich lets you define different drawables for each state the yourButtoncan be in, whether focused, selected, pressed, disabled and so on.For example, to get your button to change colour when it’s pressed, you could define an XML file called
res/drawable/my_button.xmldirectory like this:You can then apply this selector to a
Buttonby setting the propertyandroid:background="@drawable/my_button".