Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid imagebutton change on every click
    primarykey
    data
    text
    <p>I want my imagebutton's to change from one image to another image every time its clicked. In my code this only works once, the image changes and the button will not change back onClick. </p> <p>Can someone help?</p> <pre><code>package com.wepromoteme.brapp; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupPlayPauseButton1(); } private void setupPlayPauseButton1() { ImageButton btn1 = (ImageButton) findViewById(R.id.play_pause1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ImageButton btn1 = (ImageButton) findViewById(R.id.play_pause1); btn1.setImageResource(R.drawable.play); } }); } } </code></pre> <p>Thanks</p> <p>@shree202</p> <p>This is the new code:</p> <pre><code>public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupPlayPauseButton1(); } private void setupPlayPauseButton1() { final ImageButton btn1 = (ImageButton) findViewById(R.id.play_pause1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int i=0; if(i%2==0) { btn1.setImageResource(R.drawable.play); } else { btn1.setImageResource(R.drawable.pause); } } }); } } </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload