2

I am creating a dynamically Imageview.

ImageView btnSend = new ImageView (this); 

I need to put a layout that contains the states of the images, this is the xml:

<? xml version = "1.0" encoding = "utf-8"?> <selector xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: state_focused = "true" android: state_pressed = "false" android: drawable = "@ drawable/button_state3" /> <item android: state_focused = "true" android: state_pressed = "true" android: drawable = "@ drawable/button_state2" /> <item android: state_focused = "false" android: state_pressed = "true" android: drawable = "@ drawable/button_state2" /> <item android: drawable = "@ drawable/button_state1" /> </ selector> 

I tested with setBackgroundResource property, but does not work.

how do I assign this layout to Imageview?

Thanks in advance.

2 Answers 2

3

What you have created in this XML (BTW remove the spaces after @) is a StateListDrawable not a Layout as you mentioned. Consequently, use ImageView.setImageResource(R.drawable.mydrawable) to set it.

Sign up to request clarification or add additional context in comments.

2 Comments

And make sure it's in your drawable folder, not your layout folder.
StateListDrawable fix my problem, thanks dtmilano!! I leave the code in the post below for users who have the same problem
2

This is the code that works...

ImageView btnEliminar = new ImageView (this); StateListDrawable drawable = new StateListDrawable(); Drawable normal = getResources().getDrawable(R.drawable.image_normal); Drawable selected = getResources().getDrawable(R.drawable.image_selected); Drawable pressed = getResources().getDrawable(R.drawable.image_pressed); drawable.addState(new int[] { android.R.attr.state_pressed}, pressed); drawable.addState(new int[] { android.R.attr.state_focused}, selected); drawable.addState(new int[] { android.R.attr.state_enabled}, normal); btnEliminar.setImageDrawable(drawable); 

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.