Reputation: 4748
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.
Upvotes: 2
Views: 5731
Reputation: 4748
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);
Upvotes: 2
Reputation: 69188
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.
Upvotes: 3