1

Android Button Change Image change for Click event using XML Selector. like this.

StartButtonSelector.XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/start_button_pushed" />
    <item android:drawable="@drawable/start_button" />
</selector>

and Layout XML File like this.

main_activity.xml

 <ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/startButton"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/start_btn_selector"/>

Now. i'm plan to use Picasso Image Library for Image caching. to avoid Out.of.Memory Error. So this is my scenario. how to do this. Thanks in Advance.

Natheem Yousuf
  • 143
  • 2
  • 2
  • 8
  • For that you can refere this link : http://stackoverflow.com/questions/21158425/picasso-load-drawable-resources-from-their-uri For caching Glide is better than Picasso. I have refered Glide for that. – Mavya Soni Dec 20 '16 at 05:45
  • not Working Picasso.with(this).load(R.drawable.start_btn_selector).into(startBtn); – Natheem Yousuf Dec 20 '16 at 07:30
  • Have you try this : http://stackoverflow.com/questions/27010349/how-to-update-the-selectorstatelistdrawable-images-using-picasso – Mavya Soni Dec 20 '16 at 08:13

1 Answers1

0
ImageView imageView = (ImageView)findViewById(R.id. startButton) 
Picasso.with(context).load(R.drawable.start_button).into(imageView);

imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
               // Button pressed state
               Picasso.with(context).load(R.drawable.start_button_pushed).into(imageView);
        }else if (motionEvent.getAction()==MotionEvent.ACTION_UP){
               //  Button released state
               Picasso.with(context).load(R.drawable.start_button).into(imageView);
        }
        return true;
    }
});
Binil
  • 454
  • 3
  • 11