user11111822
user11111822

Reputation:

how can I change imageview picture when I pressed

I have two pictures a & b .Currently, my imageView has image a.
I wanna change the image to b when pressed. how can I do? this is my code

 <ImageView
        android:id="@+id/prepage"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        app:srcCompat="@drawable/aki_pre"
        android:onClick="@drawable/aki_preh"/>


private  ImageView.OnClickListener pagepre=new ImageView.OnClickListener(){
    @Override
    public void onClick(View v) {

        String result = dbQA.executeQuery(num+"");
        try{
            JSONArray jsonArray = new JSONArray(result);
            for(int i = 0; i < jsonArray.length(); i++) 
            {
                JSONObject jsonData = jsonArray.getJSONObject(i);
                topic=jsonData.getString("Q1");
                Q1.setText(topic);                 
            }
        }
        catch(Exception e){}
    }
};

Upvotes: 1

Views: 55

Answers (2)

Cyrille Con Morales
Cyrille Con Morales

Reputation: 957

You can try this as simple

// when you click this demo image

image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
    image.setImageResource(R.drawable.secondimage);
    //add some codes 
 }
}

Upvotes: 0

Faisal
Faisal

Reputation: 725

try this:

image.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            image.setImageResource(R.drawable.b);
            break;
        case MotionEvent.ACTION_UP:
            image.setImageResource(R.drawable.a);
            break;
        }
        return true;
    }
});

Upvotes: 3

Related Questions