Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selector inherit

Is there a way to inherit from already known selectors in Android ?

I would like to extend an EditText and added an custom state, so far i understood doing it using the onCreateDrawableState() Method. When an selector comes into play is there an easy way to use the default selectors and just add mine instead of defining them again ?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.my.package">

<item android:state_enabled="false" android:drawable="@drawable/login_textfield_bg_error" />
<item android:state_window_focused="false" android:drawable="@drawable/login_textfield_bg_error">
<item android:state_pressed="true" android:drawable="@drawable/login_textfield_bg_error" />
<item android:state_selected="true" android:drawable="@drawable/login_textfield_bg_error" />
<item app:errorBackground="@drawable/login_textfield_bg_error" />
</selector>
like image 425
Kitesurfer Avatar asked Dec 01 '11 14:12

Kitesurfer


1 Answers

I might be misunderstanding but maybe you can just plain delegate to them?

So in your case you have a custom state, so if you define only the cases where your custom state applies can't you do this:

<selector xmlns:android="..." xmlns:app="...">
  <item app:custom_state="true" android:drawable="@drawable/the_one_care_about"/>
  <item android:drawable="@android:drawable/editbox_background"/>
</selector>

So this basically states, for states where my custom state is true, show my custom background... however for all other states, behave the same as this selector. This selector just so happens to have instructions for other states so follow them too. So there is no redefining and since the states are evaluated in top to bottom order, you technically don't have to redefine anything, you are just stating you only want to define a subset of states and delegate to this other drawable (which happens to be another selector) for all other content. Does this help?

like image 187
Greg Giacovelli Avatar answered Sep 29 '22 20:09

Greg Giacovelli