Reputation: 67
I change the color with this code, but the color changes only when i quit from the application and run it again. How can I resolve to change immediately the color?
adapter2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, list) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view
.findViewById(android.R.id.text1);
text.setTextColor(Color.parseColor(color));
return view;
}
};
Upvotes: 1
Views: 3352
Reputation: 1442
Put this file in res/color folder as textselector_list.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true" android:color="#FFFFFFFF"/>
<!-- pressed -->
<item android:state_focused="true" android:color="#FFFFFFFF"/>
<!-- focused -->
<item android:state_selected="true" android:color="#FFFFFFFF"/>
<!-- selected -->
<item android:color="#FF888888"/>
<!-- default -->
And set this as android:textColor="@color/textselector_list"
to the listView
item i.e. your TextView
or Button
.
Upvotes: 4
Reputation: 47817
Create res/color/button_text.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="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
This layout XML
will apply the color list to a View
:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/button_text" />
And if you want to apply State Color
Dynamically then
ColorStateList myColorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed}, //1
new int[]{-android.R.attr.state_focused}, //2
new int[]{android.R.attr.state_selected, android.R.attr.state_pressed} //3
},
new int[] {
Color.RED, //1
Color.WHITE, //2
Color.BLUE //3
}
);
title_txt.setTextColor(myColorStateList);
For more information go to this: http://developer.android.com/guide/topics/resources/color-list-resource.html
Upvotes: 4