Replace Selector Images Programmatically

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 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.

Change selector assigned to tint programmatically

Best way I found so far is to make a new color state list programmatically and assign it to the button, yikes, the goal was to avoid setting visual attributes like colors programmatically ...

        ColorStateList buttonStates = new ColorStateList(
new int[][] {
{ -android.R.attr.state_enabled },
{}
},
new int[] {
Color.RED,
Color.BLUE
}
);

buttonMinus.setImageTintList(buttonStates);

How to replace drawable source of a xml item

On the Android Button class Documentation you get:

If you're not satisfied with the default button style and want to
customize it to match the design of your application, then you can
replace the button's background image with a state list drawable.

You might want to replace the image button background drawable with an StateListDrawable you like in this SO post, assuming that you have downloading both images and have them as drawable objects already

    StateListDrawable states = new StateListDrawable();
//This for pressed true
states.addState(new int[] {android.R.attr.state_pressed},
drawable_image1);
//This for pressed false
states.addState(new int[] { },
drawable_image2);
//Change it on the button
button.setBackgroundDrawable(states);

Programmatically change the src of an img tag

its ok now

function edit()
{
var inputs = document.myform;
for(var i = 0; i < inputs.length; i++) {
inputs[i].disabled = false;
}

var edit_save = document.getElementById("edit-save");

edit_save.src = "../template/save.png";
}

image change in ImageView when its clicked

Try this

btn_selector.xml

<item android:drawable="@drawable/selector_image" android:state_selected="true"></item>
<item android:drawable="@drawable/selector_image" android:state_pressed="true"></item>
<item android:drawable="@drawable/normalImage"></item>

</selector>

Now to your Button

android:background="@drawable/btn_selector"

I don't get it why this isn't working for you, but here is another way of doing it. Suppose you have 3 imageButtons ,img1, img2, img3. Now onClick of imagebutton do this

img1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

img1.setImageResource(R.drawable.ic_a_white);
img2.setImageResource(R.drawable.normal);
img3.setImageResource(R.drawable.normal);
}
});

And then on 2nd button

img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

img2.setImageResource(R.drawable.ic_a_white);
img1.setImageResource(R.drawable.normal);
img3.setImageResource(R.drawable.normal);
}
});

How do I programmatically change the ImageButton src target when a condition is met?

Is it a requirement that it has to be imageResource? You can try using backgroundResource instead.

ImageButton flashButtonOn = (ImageButton) findViewById(R.id.flashButtonOn);
flashButtonOn.setBackgroundResource(R.drawable.on_selector);

Or try

flashButtonOn.setImageResource(R.drawable.replacementGraphic);

Android - ImageView selected state without an additional image?

OK, I got it working. Add

implements OnTouchListener

to the definition of your activity, add

.setOnTouchListener(this);

to all your imageviews that must respond to click with getting brighter, and add:

@Override
public boolean onTouch(View pV, MotionEvent pEvent) {
if(pEvent.getAction() == MotionEvent.ACTION_DOWN){
((ImageView)pV).setAlpha(128);
}
else if(pEvent.getAction() == MotionEvent.ACTION_UP){
((ImageView)pV).setAlpha(255);
}
return true;
}

to your activity. That's all.



Related Topics



Leave a reply



Submit