Question

I hve searched many times but I did not find what i need.

What i need is :

when a user just click the button I want it changed the background image.(user does not need press to the button I need just one click and change the image )

what i did is :

in selector:

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

and in button xml :

  <Button
        android:id="@+id/btn_wb"
        android:layout_width="50dp"
        android:layout_height="50dp"  
        android:background="@drawable/web_selector" />

but this works only a user hold his hand to the button, but I need it works just one click.

thanks in advance

Was it helpful?

Solution

A selector uses the Color State List Resource. I'm not sure there is, in the list of states, the expected behaviour. However, you can achieve this with a click listener event instead of using a selector:

// init a boolean
private boolean isPressed = false; 

// click listener event 
btn_wb.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // change the background once 
        if(!isPressed) {
            v.setBackgroundResource(R.drawable.web_wl);
            isPressed = true;
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top