2

I am totaly new in Android development.

I try to change the FontColor and the Background of ListViewItem if it`s selected or pressed. Changing the Background is absolutely no Problem by using following selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_pressed="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:state_focused="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:state_selected="true"
         android:drawable="@drawable/background_pressed"></item>

   <item android:drawable="@android:color/transparent"></item>

</selector>

But how can I change the Background and the FontColor?

Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
Sandman
  • 55
  • 7

2 Answers2

2

Changing the TextView's font color is done in the same way as the listitem's backgroundcolor: create a StateListDrawable and set the TextView's android:textColor to your custom drawable.

For an example, see Change ListViews text color on click. Combine this with what you already have for the listitem's background.

If you want the colors to fade into eachother, set (e.g.) android:exitFadeDuration="@android:integer/config_mediumAnimTime" in the <selector>-tag.

Community
  • 1
  • 1
Reinier
  • 3,836
  • 1
  • 27
  • 28
0

Try this

    @Override
public void onCreate(Bundle savedInstanceState) {
    TextView txtName;
    txtName.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView textView = (TextView) v;
            textView.setBackgroundColor(Color.RED);
            textView.setTextColor(Color.YELLOW);
        }
    });
}
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
  • Minh Binh can u please tell me how can i change textcolor on hover in case of textview while my textview is inside row – Erum Jun 19 '14 at 15:38