1

In my app, I want the background image to change when tapping on the image (this works perfectly), but now I was thinking of maybe having it to change automatically without tapping the image.

Here is my code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    ((ViewSwitcher) rootView.findViewById(R.id.switcher)).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ViewSwitcher switcher = (ViewSwitcher) v;

            if (switcher.getDisplayedChild() == 0) {
                switcher.showNext();
            } else {
                switcher.showPrevious();
            }
        }
    });

    return rootView;
}
}

How can I make it to change automatically?

I have tried various suggestions, but nothing seems to work or it just displays the first image.

Arnaud
  • 7,259
  • 10
  • 50
  • 71
Allrounder
  • 685
  • 2
  • 9
  • 20

2 Answers2

0

Did you try to use timer and after some time call function on main thread to change your image?

Community
  • 1
  • 1
Pęgaz
  • 46
  • 2
0

Use a handler and runnable instead and then call handler.postdelayed(....)

Arnaud
  • 7,259
  • 10
  • 50
  • 71
Mann
  • 661
  • 5
  • 9