0

I have a ListView and created a xml file for ListView items.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_item_selector_back"
    android:minHeight="?attr/listPreferredItemHeight"
    android:orientation="vertical">

    <CheckedTextView
        android:id="@+id/list_item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:textColor="@color/list_item_selector_fore" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/list_item_author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/list_item_selector_fore" />

        <!-- and many more TextView elements -->

    </LinearLayout>
</LinearLayout>

list_item_selector_fore.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/white" android:state_pressed="true" />
    <item android:color="@android:color/white" android:state_activated="true" />
    <item android:color="@android:color/white" android:state_focused="true" />
    <item android:color="@android:color/black" />
</selector>

As you can see, I defined the background once in the root LinearLayout. But I had to set android:textColor="@color/list_item_selector_fore" for every TextView. I tried it with foreground, but nothing happened. How can I get this smarter?

HOW TO LOOK: two list view items

Matt
  • 13,948
  • 6
  • 44
  • 68
EV221
  • 33
  • 1
  • 7

2 Answers2

0

If I understand the question correctly you want to change the colour of the text inside a TextView. From your XML it seems you want it to be onClick therefore you can do this:

TextView textView = (TextView) findViewById(R.id.myTextView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setTextColor('set text color here');
            }
        });

EDIT:

If you have these items inside of a ListView I would suggest that the easiest way for you to achieve what you want is to set an OnItemClickListenerand then change the textColor based on the OnItemClick event, which may look something like this;

ListView listView = (ListView) findViewById(R.id.listView);
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView textView = (TextView) view.findViewById(R.id.rowItemTextView);
            textView.setTextColor('color goes here');
        }
    });

Hopefully this short snippet will guide you in the right direction to go.

Max
  • 155
  • 2
  • 13
  • Yes, but I have several TextViews. I don't want to get each of them manually. And I want to change the colors in xml. The advantage of this method is that the color state is saved on scrolling. – EV221 Jun 27 '16 at 12:24
  • I see you've now edited to add a picture of what you mean, I'll edit my answer~ – Max Jun 27 '16 at 12:27
  • The problem is, that the color is disappearing when I scroll because of ListView's recycling. But if I use this style or XML stuff, the background color is still there if I scroll back. – EV221 Jun 27 '16 at 12:48
0

If you want do it for all TextView, the best way should be to subclass TextView

public class CustomeTextView extends TextView{

    public CustomeTextView(Context context) {
        super(context);
        this.setTextColor(R.color.white);  
    }
}
fangzhzh
  • 2,172
  • 21
  • 25