Issue
I have a trivial XML drawable happy_borders.xml which is to be the background for some EditText.
<EditText
android:background="@drawable/happy_borders
Of course that doesn't work, because on many/most devices it will focus the first one, and then you get nothing as the background.
So you do something like this ... file happy_states.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext_states.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/happy_borders" />
<item
android:state_focused="true"
android:drawable="@drawable/happy_borders" />
<item
android:state_enabled="true"
android:drawable="@drawable/happy_borders" />
</selector>
and then
<EditText
android:background="@drawable/happy_states
No problem but
(1) there are actually an enormous list of android:state_ ...
- what's the deal there? In the selector
is there a way to imply "do this for every state"??
(2) alternately. Inside an EditText. Can you tell it to be "stateless" and never, ever, concern itself with states? ie, use only the one background ever?
Note that in many cases it's better engineering, I think, to just put a separate view behind the EditText; but anyway I want to know how to achieve (1) or (2) above.
Solution
Just define states that require a different drawable and then add a default state at the end:
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext_states.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/happy_borders" />
<item
android:drawable="@drawable/sad_borders" />
</selector>
EDIT: Sorry, didn't quite answer your questions:
1) If you don't define any non-default states, then default one is "do this for every state".
2) I believe no, probably the solution is to set background to null
or specific drawable
.
To be clear this:
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext_states.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/default_borders" />
</selector>
is all that is needed, to have "no states"; to have every state the same.
Answered By - Roman Kolomenskii
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.