views:

5231

answers:

4

I want a simple TextView to behave the way simple_list_item_1 in a ListView does. Here's the XML:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:gravity="center" android:focusable="true"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:background="@android:drawable/list_selector_background" />

Everything works except for the text color that (expectedly) doesn't change in focused state. How do I make it change to textAppearanceLargeInverse?

TIA.

A: 

Have you tried setOnFocusChangeListener? Within the handler, you could change the text appearance.

For instance:

TextView text = (TextView)findViewById(R.id.text);
text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            ((TextView)v).setXXXX();
        } else {
            ((TextView)v).setXXXX();
        }
    }
});

You can then apply whatever changes you want when it's focused or not. You can also use the ViewTreeObserver to listen for Global focus changes.

For instance:

View all = findViewById(R.id.id_of_top_level_view_on_layout);
ViewTreeObserver vto = all.getViewTreeObserver();
vto.addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {
    public void onGlobalFocusChanged(
        View oldFocus, View newFocus) {
        // xxxx
    }
});

I hope this helps or gives you ideas.

lilbyrdie
I've gone this way initially but text color didn't change for some reason.
alex
+5  A: 

And selector is the answer here as well. Search for bright_text_dark_focused.xml in the sources, add to your project under res/color dir and then refer from the TextView as android:textColor="@color/bright_text_dark_focused".

alex
+1  A: 

In case anyone is looking for the xml file, it's here: http://developer.android.com/resources/samples/Home/res/color/bright_text_dark_focused.html

swinefeaster
+2  A: 

Eu consegui, fazendo vários testes até que um deu certo, dessa forma: res/drawable-hdpi/button_dark_text.xml

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
     <item android:state_pressed="true"
           android:color="#000000" /> <!-- pressed -->
     <item android:state_focused="true"
           android:color="#000000" /> <!-- focused -->
     <item android:color="#FFFFFF" /> <!-- default -->
 </selector>

res/layout/view.xml

<?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"
   >
    <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="EXIT"
              android:textColor="@drawable/button_dark_text" />
</LinearLayout>
Klaus Balduino
Sorry, translating:I got by doing several tests until one worked, so:
Klaus Balduino