Android refresh StateListDrawable problem

Posted by Max on Stack Overflow See other posts from Stack Overflow or by Max
Published on 2010-08-25T07:57:32Z Indexed on 2010/12/23 17:54 UTC
Read the original article Hit count: 270

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about android

Related posts about view