0

how to change image button with another image button for example : Play and Pause

ALiiLA
  • 55
  • 1
  • 11

2 Answers2

1

You can just change the icon on the button, instead of changing the entire button. To change the icon programmatically you can just do this:

ImageButton btn = (ImageButton)findViewById(R.id.button);
btn.setImageResource(R.drawable.your_image);

If however, you wish to replace the button with a different button, you can create two buttons in the same location using a RelativeLayout, and then hide or show the buttons, based on what you want. To do this:

ImageButton playBtn = (ImageButton)findViewById(R.id.play);
ImageButton pauseBtn = (ImageButton)findViewById(R.id.pause);
playBtn.setVisibility(View.GONE);
pauseBtn.setVisibility(View.VISIBLE);
Rachit
  • 3,173
  • 3
  • 28
  • 45
0

You don't need to change the button, just the text / icon on the button.

Button button = (Button) findViewById(R.id.button_play_pause);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if (playing) {
            playing = false;
            button.setText("PLAY");
        } else {
            playing = true;
            button.setText("PAUSE");
        }
    }
});

Further reading: https://developer.android.com/reference/android/widget/Button.html

Aloso
  • 5,123
  • 4
  • 24
  • 41