1
votes

I Found lots of questions for image changing in imageview with another image but I have different problem.

I have to two svg images in xml format in drawable folder. I want to toggle between images on click of imageview. Like if image a.xml is there then on click on it image b.xml should be displayed, to do that I have to fetch the current xml image source of imageview nd set it to the other one.

How to do that ?

MainActivity.java :

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = (ImageView) findViewById(R.id.imageView);

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(v == iv){

                    if(/* How to check current current source of imagevie here  */  ){
/* How to set new xml svg source to imagevie here  */
                    }

                }

            }
        });


    }
}

xml :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context="com.abcd.MainActivity">


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:srcCompat="@android:drawable/alert_dark_frame"
        android:src="@drawable/a"
        android:layout_centerHorizontal="true"

        android:id="@+id/imageView"
        android:layout_alignParentTop="true" />


</FrameLayout>

drawable folder containing two svg sources

1

1 Answers

1
votes

figured out the partial solution because I can set the new svg source to imageview but still can't the current source :

  if(v == iv){

                int k = 10;
                switch (count) {
                    case 1 :

                        iv.setImageResource(R.drawable.a);
                        k = 0 ;
                        break;
                    case 0 :
                        iv.setImageResource(R.drawable.b);
                        k=1 ;
                        break;
                }
                count = k ;

            }