tags:

views:

71

answers:

1

Hey, I have, for the time being, a custom view with a 9-patch image as a border.

That custom view is placed three times in a LinearLayout, so it looks like this:

+------------------------+
|  CustomView            |
+------------------------+
|  CustomView            |
+------------------------+
|  CustomView            |
+------------------------+

I have attached a click event listener to the View, so it is clickable. But then I click it, I cant see that I am clicking it - there is no change in color.

So, Im thought that I'd attach a "onPress" listener, and then change the background of the view but I couldnt find such a listener.

So, the question is - how do I create the behaviour on the View so I can see that it is being pressed? this is normally done in Android with a green background to indicate that it is now being pressed.

Regards

+3  A: 

You could set the OnClickListener for the view. That will be called when the view is clicked. But for something as simple as changing the background when a view is clicked you should use a stateful drawable. They work like this, you make 3 9-patch images.

  1. is the normal background like what you have now.
  2. is what the background should look like when the user selects the view with the track-ball / d-pad
  3. is what the view should look like when the user clicks on it

Then you create an new xml file in your drawable folder. It should look like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
<item
    android:state_focused="true"
    android:state_pressed="true"
    android:drawable="@drawable/background_pressed" />
<item
    android:state_focused="true"
    android:state_pressed="false"
    android:drawable="@drawable/background_focused" />
<item
    android:state_focused="false"
    android:state_pressed="true"
    android:drawable="@drawable/background_pressed" />
<item
    android:drawable="@drawable/background_normal" />

Then, when you set the background of your view set it to the xml file.

CaseyB
Thx for that =) So, I was sort of close to the solution. Instead of trying to find a "onPress"-thing, i just create 3 different images and put them in the Selector-XML-file and then attach the Selector-XML-file as the background? I'll try that =) thx!
Ted
Hmm, on [ android:drawable="@drawable/background_pressed" ] I cannot find my drawables. If I try to replace background_pressed with my image, it doesnt show up.Am I missing something?
Ted
Hmm, it didnt work it seems... Its just as normal - no changing of the background...
Ted
Im guessing it has something to do with the fact that I dont get the suggestions when specifying android:drawable="..."
Ted
Did you add the XML in Eclipse or just into the filesystem? If you added it in the filesystem you need to refresh the eclipse folder. As for not finding the drawable, you need to make sure that the name you enter in the xml is exactly the same as the name of the image, minus the extension. And you need to be sure to set the view to be clickable and focusable.
CaseyB