I want to change button text color on diffrent states like btn_presses,btn_focus,etc.
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.
Any one know how to do it.Is I am going wrong anywhere in code.
MainActivity.java
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);
}
TitleBarLayout.java
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) {
}
}
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="title_bg_color">#2e4256</color>
<color name="layout_bg_color">#dcdcdc</color>
<color name="state_pressed">#ffffff</color>
<color name="state_selected">#00ff00</color>
<color name="state_focused">#0000ff</color>
</resources>
titlebarlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
<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"/>
<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" />
<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" />
</LinearLayout>
colorstatelist.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="@color/state_pressed"/>
<item android:state_selected="true"
android:color="@color/state_selected"/>
<item android:state_focused="true"
android:color="@color/state_focused"/>
<item android:color="#808080"/>
</selector>
First, get rid of all the XML parsing stuff from your code. All it's adding is some bugs. There are two approaches you could use, and you should use one or the other, not both.
Your layout XML already sets android:textColor
to the state list you want. This is all you need to do. The button text will automatically change colour according to its state. Just remove all the calls to setTextColor
from your Java code: at present, those calls are replacing the state list with a static colour. I suggest you completely remove the setLeft/RightButtonTextColor
methods from your TitleBarLayout.java
, and then let your IDE find all the calls to them, and delete those.
Alternatively, if you want to set the colour from Java code instead of from XML, you just need to call it like this:-
button.setTextColor(getResources().getColorStateList(R.color.colorstatelist));
No XML parsing necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With