[Solved] how to change the image on click of imagebutton?


That’s why you are using a selector for your button background and the image changes depending on the state of the button. If it is pressed the image will be “on” and in its normal state (no pressed and no focused) the image will be “off”.

EDIT:

public class MainActivity extends AppCompatActivity {

ImageButton btn;
boolean isPressed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = (ImageButton) findViewById(R.id.btn);
    btn.setBackgroundResource(R.drawable.normal);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isPressed){
                v.setBackgroundResource(R.drawable.normal);
            }else{
                v.setBackgroundResource(R.drawable.pressed);
            }
            isPressed = !isPressed; // reverse
        }
    });


}


  <ImageButton
    android:id="@+id/btn"
    android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

4

solved how to change the image on click of imagebutton?