title: How to use colorstatelist in android
body: <p>I want to change button text color on diffrent states like btn_presses,btn_focus,etc.</p>
  
  <p>For that i use colorstatelist.xml and I refrenced it in button text in titlebarlayout.xml.  But still I cant able to change text color of button.</p>
  
  <p>Any one know how to do it.Is I am going wrong anywhere in code.</p>
  
  <p>MainActivity.java   </p>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener {
  
          EditText emailEdit, passwordEdit;
          Button loginButton;
          String email, password;
          TitleBarLayout titlebarLayout;
          String r;
          String rr;
  
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.login_activity);
  
              emailEdit = (EditText) findViewById(R.id.etEmail);
              passwordEdit = (EditText) findViewById(R.id.etPassword);
  
              loginButton = (Button) findViewById(R.id.loginButton);
              loginButton.setOnClickListener(this);
  
              // titlebarLayout=(TitleBarLayout) findViewById(R.id.titlebar);
              titlebarLayout = new TitleBarLayout(MainActivity.this);
              titlebarLayout.setLeftButtonText("");
              titlebarLayout.setRightButtonText("Logout");
              titlebarLayout.setTitle("iProtect");
              //titlebarLayout.setLeftButtonSize(50,50);
              //titlebarLayout.setRightButtonSize(100,50);
              titlebarLayout.setLeftButtonBackgroundColor(Color.rgb(34,49,64));
              titlebarLayout.setRightButtonBackgroundColor(Color.rgb(34,49,64));
              titlebarLayout.setLeftButtonTextColor(Color.rgb(255,255,255));
              titlebarLayout.setRightButtonTextColor(Color.rgb(255,255,255));
  
  
              XmlResourceParser parser =getResources().getXml(R.color.colorstatelist);
  
                  ColorStateList colorStateList;
                  try {
                      colorStateList = ColorStateList.createFromXml(getResources(), parser);
  
                      titlebarLayout.setLeftButtonTextColor(colorStateList);
                  } catch (XmlPullParserException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
      //      ColorStateList colorlist=new ColorStateList( new int[][] { new int[] { android.R.attr.state_focused }, new int[0], }, new int[] { Color.rgb(0, 0, 255), Color.BLACK, } );
      //      titlebarLayout.setLeftButtonTextColor(colorlist);
  
  
              OnClickListener listener = new OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                      if (v.getId() == R.id.left_button) {
  
                      } else if (v.getId() == R.id.right_button) {
  
                      }
                  }
              };
              titlebarLayout.setLeftButtonOnClickListener(listener);
              titlebarLayout.setRightButtonOnClickListener(listener);
          }
  </code></pre>
  
  <p>TitleBarLayout.java</p>
  
  <pre><code>public class TitleBarLayout {
  
      private Activity activityRef;
      private View contentView;
      private Button leftButton, rightButton;
      TextView titletext;
  
      public TitleBarLayout(Activity a) {
          Log.i("TitleBar Layout", "Inside constructor");
          activityRef = a;
          inflateViewsFromXml();
          setListenersOnViews();
          setValuesOnViews();
      }
  
      private void setValuesOnViews() {
          leftButton.setText("");
          rightButton.setText("");
      }
  
      private void setListenersOnViews() {
          leftButton.setOnClickListener(listener);
          rightButton.setOnClickListener(listener);
      }
  
      private final OnClickListener listener = (new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              activityRef.finish();
          }
      });
  
      private void inflateViewsFromXml() {
          contentView = activityRef.findViewById(R.id.titlebar);
          rightButton = (Button) contentView.findViewById(R.id.right_button);
          leftButton = (Button) contentView.findViewById(R.id.left_button);
          titletext = (TextView) contentView.findViewById(R.id.title_textview);
      }
  
      public void setLeftButtonText(int resID) {
          leftButton.setText(resID);
      }
  
      public void setLeftButtonText(String text) {
          leftButton.setText(text);
      }
  
      public void setLeftButtonOnClickListener(View.OnClickListener listener) {
          leftButton.setOnClickListener(listener);
      }
  
      public void setRightButtonText(int resID) {
          rightButton.setText(resID);
      }
  
      public void setRightButtonText(String text) {
          rightButton.setText(text);
      }
  
      public void setRightButtonOnClickListener(View.OnClickListener listener) {
          rightButton.setOnClickListener(listener);
      }
  
      public void setTitle(int resID) {
          titletext.setText("" + resID);
      }
  
      public void setTitle(String text) {
          titletext.setText(text);
      }
  
      public void setLeftButtonSize(int width, int height) {
          Log.i("Button" ,"Width"+width);
          leftButton.setWidth(width);
          leftButton.setHeight(height);
      }
  
      public void setRightButtonSize(int width, int height) {
  
          Log.i("Button" ,"Width"+width);
          rightButton.setWidth(width);
          rightButton.setHeight(height);
      }
  
      public void setLeftButtonBackgroundResource(int backgroundResource) {
  
      }
  
      public void setRightButtonBackgroundResource(int backgroundResource) {
  
      }
  
      public void setLeftButtonBackgroundColor(int backgroundColor) {
          Log.i("Button" ,"COLOR"+backgroundColor);
          leftButton.setBackgroundColor(backgroundColor);
      }
  
      public void setRightButtonBackgroundColor(int backgroundColor) {
          Log.i("Button" ,"COLOR"+backgroundColor);
          rightButton.setBackgroundColor(backgroundColor);
      }
  
      public void setLeftButtonTextColor(int textcolor) {
          Log.i("Button" ,"COLOR"+textcolor);
          leftButton.setTextColor(textcolor);
      }
  
      public void setRightButtonTextColor(int textcolor) {
          Log.i("Button" ,"COLOR"+textcolor);
          rightButton.setTextColor(textcolor);
      }
  
      public void setLeftButtonTextColor(ColorStateList colorStateList) {
          leftButton.setTextColor(colorStateList);
  
      }
  
      public void setRightButtonTextColor(ColorStateList colorStateList) {
  
      }
  }
  </code></pre>
  
  <p>color.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
  
      &lt;color name="title_bg_color"&gt;#2e4256&lt;/color&gt;
      &lt;color name="layout_bg_color"&gt;#dcdcdc&lt;/color&gt;
      &lt;color name="state_pressed"&gt;#ffffff&lt;/color&gt;
      &lt;color name="state_selected"&gt;#00ff00&lt;/color&gt;
      &lt;color name="state_focused"&gt;#0000ff&lt;/color&gt;
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>titlebarlayout.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="@dimen/titlebar_height"
      android:background="@color/title_bg_color"
      android:gravity="center_vertical"
      android:orientation="horizontal" &gt;
  
      &lt;Button
          android:id="@+id/left_button"
          android:layout_width="@dimen/titlebar_button_width"
          android:layout_height="@dimen/titlebar_button_height"
          android:text=""
          android:clickable="true"
          android:textColor="@color/colorstatelist"
          android:background="@drawable/button_shape_drawable"
          android:layout_marginLeft="10dp"/&gt;
  
      &lt;TextView
          android:id="@+id/title_textview"
          android:layout_width="0dip"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:gravity="center"
          android:text=""
          android:textColor="@android:color/white"
          android:textStyle="bold" /&gt;
  
      &lt;Button
          android:id="@+id/right_button"
          android:layout_width="@dimen/titlebar_button_width"
          android:layout_height="@dimen/titlebar_button_height"
          android:text=""
          android:clickable="true"
          android:layout_marginRight="10dp"
          android:background="@drawable/button_shape_drawable"
          android:textColor="@color/colorstatelist" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>colorstatelist.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item android:state_pressed="true"
            android:color="@color/state_pressed"/&gt;
      &lt;item android:state_selected="true"
            android:color="@color/state_selected"/&gt;
      &lt;item android:state_focused="true"
            android:color="@color/state_focused"/&gt;
      &lt;item android:color="#808080"/&gt;
  &lt;/selector&gt;
  </code></pre>