1

In my android project i have taken 2 image button(start and stop). when i pressed START IMAGE BUTTON then it converted to STOP IMAGE BUTTON BUT again when i pressed STOP IMAGE BUTTON it does not converted into START BUTTON.so let me know where is the problem.

  mImageButton.setOnFocusChangeListener(
  new OnFocusChangeListener()
  {
  @Override
  public void onFocusChange(View v, boolean hasFocus) {
  // TODO Auto-generated method stub
  if (hasFocus==true)
  {
      mImageButton.setImageResource(R.drawable.stop);
  }
 else
 {
  mImageButton.setImageResource(R.drawable.play);
 }
  }
 });

 mImageButton.setOnClickListener(
 new OnClickListener() {
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 mImageButton.setImageResource(R.drawable.stop);
   }
   });     
 }
}
  • 1
    check www.c-sharpcorner.com/UploadFile/2fd686/android-buttons-background2/ you should be using `selectors` for what you are trying to do here – Naveen Apr 17 '13 at 05:39
  • In your onClick, you're setting it to stop. You need to flip it back to start explicitly if you want that. – Gabe Sechan Apr 17 '13 at 05:40
  • @Naveen yes should be using selectors with ToggleButton for this scenario – jaga Apr 17 '13 at 05:42
  • sir ,when i pressed start image button it successfully shows stop image button.this is OK. but when i again pressed that stop image button it does not shows start image button.so what shall i do for reverse action??? – user2189622 Apr 17 '13 at 05:44
  • @user2189622 you can simply use a `Boolean` to do what you actually need – Naveen Apr 17 '13 at 05:46

5 Answers5

1

In Android onFocusChange is called before onCLick. Whatever you set in the onFocusChange method is ignored, as the onClick will be called next, and it changes the image bak to R.drawable.stop.

You shouldn't use onFocusChange at all for what you are trying to do. Only use onClick instead.

private boolean pCurrentlyPlaying = false;

...

mImageButton.setOnClickListener(
    new OnClickListener() {
       @Override
       public void onClick(View v) 
       {
           int image = pCurrentlyPlaying ? R.drawable.stop : R.drawable.play;
           mImageButton.setImageResource(image);
           pCurrentlyPlaying = !pCurrentlyPlaying ;
       }
    });    

You can also use the ToggleButton, here is an example of how to define it in XML.

Community
  • 1
  • 1
Tomasz
  • 385
  • 5
  • 10
0

You are toggling the button images in onFocusChangeListener instead of onClickListener. ToggleButton seems to be the right fit for the scenario you are describing.

jaga
  • 753
  • 11
  • 18
0

the correct way is to use Selectors

create the following ImgBtnSelector.xml in drawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
     android:state_pressed="true"
     android:drawable="@drawable/custom_info_bubble1" />
    <item 
     android:state_pressed="false"
     android:drawable="@drawable/custom_info_bubble" />
</selector>

In our layout xml set Image Button background as this selector.

Here a a link on selectors

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
0

Try This Code.. May Be Help You.

 boolean isStart = true ;
 boolean isStop = false;
 mImageButton.setOnClickListener(
 new OnClickListener() {
 @Override
 public void onClick(View v) {
// TODO Auto-generated method stub
 if(isStart){
 isStart =false ;
 isStop = true ;
 mImageButton.setImageResource(R.drawable.play);
 }else{
 isStart = true ;
isStop = false;
mImageButton.setImageResource(R.drawable.stop);
 }
}
});     
}
}
Umesh Lakhani
  • 2,382
  • 1
  • 14
  • 10
  • now it shows warning and error. isstart-The value of the local variable isStart is not used. isstart-Cannot refer to a non-final variable isStart inside an inner class defined in a different method. – user2189622 Apr 17 '13 at 06:04
0
Boolean isStart=true;//This is global

mImageButton.setOnClickListener(
 new OnClickListener() {
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
if(isStart)
 mImageButton.setImageResource(R.drawable.stop);
else
 mImageButton.setImageResource(R.drawable.start);

isStart=!isStart;
   }
   });     
 }
}

This is what you need loose the onfocusChanged part

Naveen
  • 1,703
  • 13
  • 22