2

I'm not able to figure out a really stupid issue about TextView!! I have a simple Layout with a Button and a TextView. This is the Layout:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Button1" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:focusable="true"
    android:clickable="true"
    android:text="TextView1"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

When the Button has focus and i press the "down arrow" in Emulator the TextView doesn't get the focus (it doesn't turn blue!!) Why? Please help me!!

user1709805
  • 2,028
  • 3
  • 19
  • 26

2 Answers2

4

Despite the TextView doesn't turn blue, it is getting focus.

To see the focus/unfocus happening, you can, for instance, define a color change in the text. This way, these events are perceptive to the user.

Create a .xml file and put into /res/color/ In my case, i named the file as "color_text_view.xml".

  <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_focused="true" android:color="#00FF00"/>
      <item android:color="#FF00FF"/>
    </selector>

And add the android:textColor="@color/color_text_view" to the textview.

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:focusable="true"
android:clickable="true"
android:text="TextView1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/color_text_view"/>

Hope this help!

Renato Lochetti
  • 4,558
  • 3
  • 32
  • 49
  • Thank you Renato.. You help me very much (trick you have just explanied will very usefull to me in the future).. but if i want to change the background of the TextView? I have try to replace textColor with background but i get a Runtime Error.. Is there a way to do this with this trick? – user1709805 Nov 05 '12 at 11:12
  • I'm not sure if this works, but take a look: http://stackoverflow.com/questions/2217753/changing-background-color-of-listview-items-on-android – Renato Lochetti Nov 05 '12 at 11:21
  • If I find somethig else, I let you know. – Renato Lochetti Nov 05 '12 at 11:22
  • Ok. In the SO post you have linked in the previous comment there is the solution and it works fine!! thank u! – user1709805 Nov 05 '12 at 12:37
0

see If you want to set your Text as focus able then your code is true. your text are in focus mode but color cant' be change .

if you change color then it will set directly because you had set focus able in XML. so when app launch TextView is already in focused mode so that color will be change.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85