how to change image button with another image button for example : Play and Pause
Asked
Active
Viewed 34 times
2 Answers
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
-
i want to change the fuction of button also – ALiiLA Feb 20 '17 at 19:44
-
You don't need to change the function! Just use an if statement to see in which state the button is and react accordingly. – Aloso Feb 20 '17 at 23:17