Question

Basically i got one array which has 2 image as if for now but eventually will have more. Images from array switcher every few seconds. i'm stuck with onClick method. Ultimate goal when user clicks on that image new Activity class opens up. Each image has its own Activity class so when user clicks on it, it should be directed to that particular class.

I did got some help yesterday

public void onClick(View v) {
       Intent n = null;
       int id = v.getId();
       switch (id){
           case R.id.someId:
                n = new Intent(getActivity(), FragMent1.class);
                break;
           case R.id.someOtherId:
                n = new Intent(getActivity(), FragMent2.class);
                break;
        }

i can set first case statement to be "R.id.textSwitcher" but second is the problem. Because i want images animates to at one place, new R.id... means new location also new "mSwitcher1.setOnClickListener(new View.OnClickListener()"

public class Animation extends Fragment implements AnimationListener {

    public HomeAnimation(){}
    private static boolean flag = false;
     Animation anim;
     private Handler handler;
     public ImageSwitcher mSwitcher,mSwitcher1;

    int textToShow[] = {R.drawable.dabangg, R.drawable.car };

    int messageCount = textToShow.length;
    int currentIndex = -1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.imgswitch, container, false);
        mSwitcher = (ImageSwitcher) v.findViewById(R.id.imageSwitcher1);
        mSwitcher.setFactory(mFactory);
        anim = AnimationUtils.loadAnimation(getActivity(),R.anim.fade_in);

        mSwitcher.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent n = null;
                 int id = v.getId();
                // Fragment fragment = null;
                //Bundle args = new Bundle();
                switch (v.getId()){
                case 0:
                    n = new Intent(getActivity(), Fragment1.class);
                    getActivity().startActivity(n);
                    break;
                case 1:
                    n = new Intent(getActivity(), Fragment2.class);
                    getActivity().startActivity(n);
                    break;
                }
            }
        });
        // set animation listener
        anim.setAnimationListener(this);
        return v;    
    }

public void updateTextView() {

    int maxIndex = textToShow.length;
    Random random = new Random();
    int generatedIndex = random.nextInt(maxIndex);
    mSwitcher.setImageResource(textToShow[generatedIndex]);

}

Here is a what i updated in case if anyone needs it

public void updateTextView() {

        int maxIndex = textToShow.length;
        Random random = new Random();
        final int generatedIndex = random.nextInt(maxIndex);
        mSwitcher.setImageResource(textToShow[generatedIndex]);
        mSwitcher.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent n = null;

                 switch(generatedIndex){
                 case 0:
                     n = new Intent(getActivity(), Fragment1.class);
                       getActivity().startActivity(n);
                       break;

                 case 1:
                      n = new Intent(getActivity(), Fragment2.class);
                       getActivity().startActivity(n);
                       break;
                 case 2:
                       n = new Intent(getActivity(), Fragment3.class);
                       getActivity().startActivity(n);
                       break;

                }
            }
         });
Was it helpful?

Solution

I don't have time to test this right now and I'm not completely sure I understand where your problem is but try this and see if it is what you want. I will put comments in the code

mSwitcher.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent n = null;
             int id = v.getId();
            // Fragment fragment = null;
            //Bundle args = new Bundle();
            switch (v.getId()){
            case 0:
              /* 
                 below should give you the index for the displayed image
                 you can compare this to the ids in your array using the index
              */
               if (v.getDisplayedChild() == 0) { // if 1st image in array do this
                   n = new Intent(getActivity(), Fragment1.class);
                   getActivity().startActivity(n);
               }
               if (v.getDisplayedChild() == 1) { // if 2nd image in array do this
                   n = new Intent(getActivity(), Fragment2.class);
                   getActivity().startActivity(n);
               }
              break;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top