Android:How to Update the Selector(Statelistdrawable) Programmatically

Android : How to update the selector(StateListDrawable) programmatically

You need to use the negative value of the needed state.
E.g.:

states.addState(new int[] {-android.R.attr.state_enabled},R.drawable.btn_disabled);

Notice the "-" sign before android.R.attr.state_enabled.

Replace selector images programmatically

As far as I've been able to find (I've tried doing something similar myself), there's no way to modify a single state after the StateListDrawable has already been defined. You can, however, define a NEW one through code:

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
getResources().getDrawable(R.drawable.normal));
imageView.setImageDrawable(states);

And you could just keep two of them on hand, or create a different one as you need it.

Android: How to create a StateListDrawable programmatically

if your drawables are just bitmaps, you could draw them programmatically, for now it should help, however I wonder what is the problem with InsetDrawable usage here, basically use prepared BitmapDrawables that are drawn programatically, you would need to modify your method to accept bitmaps b

        Bitmap bc1 = Bitmap.createBitmap(b.getWidth() + ICON_OFFSET, b.getHeight() + ICON_OFFSET, Bitmap.Config.ARGB_8888);
Canvas c1 = new Canvas(bc1);
c1.drawBitmap(b, 0, 0, null);
Bitmap bc2 = Bitmap.createBitmap(b.getWidth() + ICON_OFFSET, b.getHeight() + ICON_OFFSET, Bitmap.Config.ARGB_8888);
Canvas c2 = new Canvas(bc2);
c2.drawBitmap(b, ICON_OFFSET, ICON_OFFSET, null);

mIcon = new StateListDrawable();
mIcon.addState(new int[] { android.R.attr.state_pressed }, new BitmapDrawable(bc2));
mIcon.addState(StateSet.WILD_CARD, new BitmapDrawable(bc1));

How to update the selector(StateListDrawable) images using picasso

Thanks to Maddy final code looks like this:

        final StateListDrawable stateListDrawable = new StateListDrawable();
final Picasso picasso = Picasso.with(this.context);
// selected and checked state
target_selected = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable drawImage = new BitmapDrawable(context.getResources(), bitmap);
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, drawImage);
stateListDrawable.addState(new int[]{android.R.attr.state_activated}, drawImage);
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};
picasso.load(context.getString(R.string.server_address_http) + dItem.getIconSelected())
.into(target_selected);
target_normal = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable drawImage = new BitmapDrawable(context.getResources(), bitmap);
stateListDrawable.addState(StateSet.WILD_CARD, drawImage);
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};
picasso.load(context.getString(R.string.server_address_http) + dItem.getIconNormal())
.into(target_normal);
drawerHolder.icon.setImageDrawable(stateListDrawable);

Selector Drawable programmatically

Ok, here is, what i came up with using the linked topic.

public StateListDrawable getSelectorDrawable(int color) {
StateListDrawable out = new StateListDrawable();
out.addState(new int[] { android.R.attr.state_pressed }, createNormalDrawable(color));
out.addState(StateSet.WILD_CARD, createStrokeDrawable(color));
return out;
}

public GradientDrawable createNormalDrawable(int color) {
GradientDrawable out = new GradientDrawable();
out.setColor(color);
return out;
}

public GradientDrawable createStrokeDrawable(int color) {
GradientDrawable out = new GradientDrawable();
out.setStroke(1, color);
return out;
}

Drawable.setState() How do I control the specific state of the drawable?

Got it. A comment from here helped: Android : How to update the selector(StateListDrawable) programmatically

So Drawable.setState() takes an array in integers. These integers represent the state of the drawable. You can pass any ints you want. Once the ints pass in match a an "item" from the state list drawables the drawable draws in that state.

It makes more sense in code:

int[] state = new int[] {android.R.attr.state_window_focused, android.R.attr.state_focused};
minThumb.setState(state);

Notice that my state list drawable has both the state_pressed="true" and android:state_window_focused="true". So I have to pass both of those int values to setState. When I want to clear it, I just minThumb.setState(new int[]{});



Related Topics



Leave a reply



Submit