States are built as needed with onCreateDrawableState() (http://d.android.com/reference/android/view/View.html#onCreateDrawableState(int)). Your second setState call works because the framework called onCreateDrawableState at some point. It was pure luck it worked at all :)
On Thu, Aug 26, 2010 at 10:51 AM, maxandroid <[email protected]> wrote: > Thankx Romain, > I discovered that and I was posting the answer too :-). The question > now is: why the second setState did what the first didn't? > > Thankx again > > Bye > Max > > On 26 Ago, 18:20, Romain Guy <[email protected]> wrote: >> You are using a custom state that does not map to the built-in >> state_selected. Instead of doing this, just call >> setSelected(true/false) on your textView. There's nothing else to do. >> >> >> >> >> >> On Thu, Aug 26, 2010 at 12:50 AM, maxandroid <[email protected]> wrote: >> > Hi all, >> > I have a strange problem with StateListDrawable or maybe (probably) >> > I'm missing something. I created a test application for it and the >> > same problem occurs. So, this is my StateListDrawable resourse in file >> > test_selection.xml >> >> > <?xml version="1.0" encoding="utf-8"?> >> > <selector xmlns:android="http://schemas.android.com/apk/res/ >> > android"> >> > <item android:state_selected="true"> >> > <shape android:shape="rectangle" android:background="#ff0000"> >> > <corners android:radius="10dp" /> >> > <gradient android:startColor="#ff5555" >> > android:endColor="#ff5555" android:angle="0" /> >> > </shape> >> > </item> >> > <item android:state_selected="false"> >> > <shape android:shape="rectangle" android:background="#eeeeee"> >> > <corners android:radius="10dp" /> >> > <gradient android:startColor="#eeeeee" >> > android:endColor="#eeeeee" android:angle="0" /> >> > </shape> >> > </item> >> > </selector> >> >> > It's a very simple selector that draw a red color for selected state >> > and a white rect for the unselected one. >> >> > My main.xml template is very simple. I simply use a TextView that uses >> > the selection as background. >> >> > <?xml version="1.0" encoding="utf-8"?> >> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ >> > android" >> > android:orientation="vertical" android:layout_width="fill_parent" >> > android:layout_height="fill_parent"> >> > <TextView android:layout_width="fill_parent" >> > android:layout_height="wrap_content" android:text="@string/ >> > hello" >> > android:textSize="30dp" android:id="@+id/test_view_example" >> > android:background="@drawable/test_selection"/> >> > <Button android:layout_width="wrap_content" >> > android:layout_height="wrap_content" android:id="@+id/refresh" >> > android:onClick="updateView" android:text="refresh"></Button> >> > </LinearLayout> >> >> > My Activity code is also very simple. >> >> > public class TestDrawableStateActivity extends Activity { >> >> > private final static int[] SELECTED_STATE = >> > { android.R.attr.state_selected }; >> > private final static int[] UNSELECTED_STATE = {}; >> >> > private TextView textView; >> >> > /** Called when the activity is first created. */ >> > @Override >> > public void onCreate(Bundle savedInstanceState) { >> > super.onCreate(savedInstanceState); >> > setContentView(R.layout.main); >> > textView = (TextView) findViewById(R.id.test_view_example); >> > } >> >> > @Override >> > protected void onResume() { >> > super.onResume(); >> > // Carichiamo la Drawable >> > if(textView.getBackground().setState(SELECTED_STATE)){ >> > textView.invalidate(); >> > } >> > } >> >> > public void updateView(View view) { >> > if(textView.getBackground().setState(SELECTED_STATE)){ >> > textView.invalidate(); >> > }; >> > } >> > } >> >> > When Activity starts I try to set the state of my Drawable (the >> > StateListDrawable) with the value SELECTED. >> > It seems all very simple.... but the problem is that the state is not >> > shown. If, later, I click a button and execute the method updateView() >> > the state changes. >> > Where is my problem? Where am I wrong? >> >> > Thankx a lot >> > Max >> >> > -- >> > You received this message because you are subscribed to the Google >> > Groups "Android Developers" group. >> > To post to this group, send email to [email protected] >> > To unsubscribe from this group, send email to >> > [email protected] >> > For more options, visit this group at >> >http://groups.google.com/group/android-developers?hl=en >> >> -- >> Romain Guy >> Android framework engineer >> [email protected] >> >> Note: please don't send private questions to me, as I don't have time >> to provide private support. All such questions should be posted on >> public forums, where I and others can see and answer them > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Romain Guy Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

