0

<item android:drawable="@drawable/chiduole_big" android:state_pressed="true"/> <item android:drawable="@drawable/chiduole"/> 

this is @drawable/image_selector

xml code

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image_selector" /> 
5
  • What do you want to do exactly? btw do you have this? android:clickable="true" Commented Mar 17, 2015 at 11:28
  • when i click the imageview ,it will change its background pic, add this code android :clickabel="true" is also not worked Commented Mar 17, 2015 at 11:30
  • android:background="@drawable/image_selector" not work too.... Commented Mar 17, 2015 at 11:32
  • @ErvinZhang, Have you tried googling "android selector"? I'm sure, you must have got a lot of answers. This is one of the reasons why you got downvoted. Anyways, check out my answer here. Commented Mar 17, 2015 at 12:51
  • It's better you describe your problem with some more details; and then, readers can get it better and will help you easier and faster :) Commented Mar 17, 2015 at 13:07

2 Answers 2

2

Your code is good. You just should add android:clickable="true and android:focusable="true" in your ImageView or combine it with your code like LinearLayout.setClickable(true);.

And maybe your image_selector should look like as follows:

<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/chiduole_big" android:state_focused="true" /> <item android:drawable="@drawable/chiduole_big" android:state_pressed="true" /> <item android:drawable="@drawable/@drawable/chiduole" /> </selector> 

EDIT

I saw what you've done wrong. You've declared android:onClick="true"; what goes inside the onClick is a method not a boolean. So, you can do something like this:

<ImageView android:onClick="MyMethod" android:focusable="true" android:clickable="true" android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/image_selector" /> 

And then, in Java you should use the following :

ImageView iv = (ImageView) findViewById(R.id.iv1); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyMethod(v); } }); public void MyMethod(View v) { // Your Code } 
Sign up to request clarification or add additional context in comments.

3 Comments

<ImageView android:onClick="true" android:focusable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/image_selector" />
not worked log:03-17 19:35:28.498: E/AndroidRuntime(11240): Caused by: java.lang.NoSuchMethodException: true [class android.view.View]
Post your code in your question please, and the Logcat error :)
0

try putting android:clickable="true" in the ImageView

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.