Reputation: 335
I am trying to change text color of a TextView inside a listview item.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
ViewGroup selectedGroup = (ViewGroup)view;
((TextView)selectedGroup.getChildAt(4)).setTextColor(Color.parseColor("#336699"));
String mID =String.valueOf(((TextView) selectedGroup.getChildAt(4)).getText());
}
});
But nothing happens, I can get text value of the textview, though. What might be wrong in this code?
This is, by the way, my listView layout.
<?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"
android:padding="4dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#336699"
android:id="@+id/questionTitle"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#444444"
android:id="@+id/questionDate"
android:textSize="12sp"/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="@+id/questionContent" android:value=""/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="@+id/questionSenderContact" android:value=""/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="@+id/messageID" android:value=""/>
</LinearLayout>
EDIT: I was getting wrong textview. Index of it was not 4, but 0. Sorry for a question caused by lack of attention.
Upvotes: 0
Views: 1033
Reputation: 271
you will have to set textview as tag to the clickable button as:
...
view.setOnClickListener(this);
view.setTag(textView);
...
public void onclick(View v){
...
TextView textView = (TextView)v.getTag();
textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
...
}
Upvotes: 0
Reputation: 1812
Create a new StateListDrawable like you did before but with black as default color and white when pressed.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/black" />
<item android:state_focused="true" android:color="@color/black" />
<item android:state_pressed="true" android:color="@color/black" />
<item android:color="@color/white" />
</selector>
Now for in the TextView change the text color to the new drawable:
android:textColor="@color/list_item_text"
More on StateListDrawables: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Upvotes: 0
Reputation: 1925
try this code:
public View row;
your_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v,
int position, long id) {
if (row != null) {
row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};
Upvotes: 1