How to programmatically set style attribute in a view
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 StateListDrawable
which lets you define different drawables for each state the your Button
can 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.xml
directory like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/btn_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/btn_normal" />
</selector>
You can then apply this selector to a Button
by setting the property android:background="@drawable/my_button"
.
How to set attribute programmatically
You can try this:
TypedValue typedValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.ColorTextNotActive, typedValue, true);
@ColorInt int color = typedValue.data;
et_name.setTextColor(color);
How do you programmatically set an attribute?
setattr(x, attr, 'magic')
For help on it:
>>> help(setattr)
Help on built-in function setattr in module __builtin__:
setattr(...)
setattr(object, name, value)
Set a named attribute on an object; setattr(x, 'y', v) is equivalent to
``x.y = v''.
However, you should note that you can't do that to a "pure" instance of object
. But it is likely you have a simple subclass of object where it will work fine. I would strongly urge the O.P. to never make instances of object like that.
Is it possible to programmatically set instance attributes?
I would use **kwargs
and setattr
:
class Foo(object):
def load_values(self, **kwargs):
for key, value in kwargs.iteritems():
setattr(self, key, value)
foo = Foo()
foo.load_values(egg='bacon', sausages='spam')
# or
foo.load_values(**{'egg': 'bacon', 'sausages': 'spam'})
print(foo.egg) # bacon
print(foo.sausages) # spam
How to set a custom attribute on a view programmatically
It seems that android does not support enum attributes for this kind of work. You must have to work with boolean attributes
<attr name="validation_state_none" format="boolean"/>
<attr name="validation_state_error" format="boolean"/>
...
The most common example of what you trying to do is like focusable, editable attributes. And it's handled with atomic booleans attributes in android source
Another solution that might help you (Not tested) is to create your own drawable programmatically. Take a look at DrawableContainer and StateListDrawable
how to set the Style Attribute Programmatically in Android?
Dynamic style change is not currently supported. You must set the style before the view is create (in xml).
Related Topics
How to Install Python Packages [Ssl: Tlsv1_Alert_Protocol_Version]
Adding Python to Path on Windows
What's the U Prefix in a Python String
Return, Return None, and No Return At All
How to Iterate Over Files in a Given Directory
Saving Utf-8 Texts With Json.Dumps as Utf8, Not as \U Escape Sequence
Unicodedecodeerror When Reading CSV File in Pandas With Python
Changing Default Encoding of Python
Filter Dataframe Rows If Value in Column Is in a Set List of Values