Android: change TextView textColor when parent is oriented

I have a TextView inside a LinearLayout . LinearLayout can receive focus, and I want the textColor TextView change when it does. I thought using a ColorStateList would work, but it would seem that the TextView does not get focus when doing LinearLayout . I know this because I tried this code:

 mTextView.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Log.d(TAG, "Changed TextView focus to: " + hasFocus); } }); 

And nothing is registered. I do not want to use OnFocusChangeListener in LinearLayout to change textColor for TextView , I think it needs to be done from XML. The reason for this is that in another event, I have an ExpandableListView with a custom adapter and custom views, and Android changes the textColor TextView (from light to dark) inside my custom views when the objects are focused.

+6
java android themes focus
source share
2 answers

This is an old post, but since I had the same problem, here is the XML attribute I found for this:

 android:duplicateParentState="true" 

(to be added to the TextView to change its “focused” state when the layout state changes)

+10
source share

You can get the TextView text in the onFocuseChange method for the LinearLayout listener. Something like

 public void onFocusChange(View v, boolean hasFocus) { TextView tv = (TextView)v.findViewById(R.id.myTextView); tv.setTextColor(R.color.foo); } 

Since your LL can host multiple widgets, I think he expected onFocus LL to not spread even if you have one control

+4
source share

All Articles