views:

1907

answers:

3

hi. I guess this is kind of an odd question but i have tried setting onClicklistener on an ImageView and it has worked. But the problem is that the user cannot sense the click. I mean if some of u have worked on other mobile environs(like apple iphone) then wen we click on a Image in other environs then it gives an effect on the image so that the user can understand that the image has been clicked.

I have tried setting alpha using "setalpha" method but it doesnt work. Though the same thing is working fine on onFocusListener implementation. Can some1 suggest a different way to modify the image on click...

I am new to android so havent learnt the nuances of simple animation also... if there is any simple animation i can use for the same then plzz plzzz let me know...

Thanx...

A: 

Not sure if it works, but have you tried setSelected() http://developer.android.com/reference/android/widget/ImageView.html#setSelected(boolean)

sadboy
thanx sadboy.... i checked the documentation... trying it out now...
JaVadid
nope... sorry sadboy... though i don want to make u even sadder but the technique didnt work... thanx anyway...
JaVadid
+2  A: 

You'll want to use a drawable that contains different images for the different states you want to support. Here's an example:

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

Name this file img.xml or something and put it in your drawable directory, then set your ImageView's image to img.xml. @drawable/img_at_rest is the original image you're trying to use, while @drawable/img_pressed and @drawable/img_focused are the images to use for their respective states. You can also use solid colors instead of images if it's appropriate for your use case.

Mike
wow... this is something which i can use... but can i just tweek the code so that the image doesnt change but the alpha of the image is changed???
JaVadid
Hmm, I'm not sure. The xml interfaces for the android.graphics.drawable package are not well documented. See if you can find a way to specify an alpha transparency, or perhaps nest an alpha color over your image within an <item> specification.
Mike
well Mike my problem is that the image is not from resource but from the internet... hence changing images i guess wont help... Thanx 4 such a quick response...
JaVadid
A: 
   <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"&gt;

<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.5"
android:duration = "300">
</alpha>
<scale
android:fromXScale = "1"
android:toXScale = "0.9" 
android:fromYScale = "1"
android:toYScale = "0.9" 
android:pivotX="50%"
android:pivotY="50%" 
android:duration = "50">
</scale>
</set>

hi friends don know if this the correct method but defining an animation as mentioned above did the trick... now we just have to give

public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(Context, R.anim.image_click));
//Your other coding if present
}

in the OnClick method and the change will be visible...

Just try this... thanx all of u 4 ur help....

JaVadid
glad you found something that works!
Mike
ya.. all thanx to u and others who shared their ideas... hope the method i posted will be useful for some1 else too...
JaVadid