Search code examples
androidandroid-webviewviewflipper

Adding multiple webviews to a flipper dynamically in android


I have some urls of ad images in an arraylist of string type. I need to load these urls into web views and display it in a flipper one after the other.

ArrayList<String>homeads=new ArrayList<String>();
int count=homeads.size();//gives count as 2. So I need to flip these two urls in webview in a flipper.
    WebView webv = new WebView(getActivity().getApplicationContext());
    webv.getSettings().setJavaScriptEnabled(true);



    for(int i=0;i<count;i++)
    {
        String url=homeads.get(i);
        webv.loadUrl(url);



    }
    flipper.addView(webv);
    flipperStart();

private void flipperStart()
{
    slide_in = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.slide_in_animation);
    slide_out = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.slide_out_animation);
    flipper.setInAnimation(slide_in);
    flipper.setOutAnimation(slide_out);
    flipper.setAutoStart(true);
    flipper.setFlipInterval(4000);
    flipper.startFlipping();
}

Here the count gives 2, which means there are 2 urls which has to be shown in a webview and flip each one alternatively. but when I do like above, only 1 url is shown in flipper. Can some one please help me out??


Solution

  • Move webview creation and adding webview to flipper into for loop.

    for(int i=0;i<count;i++) {
        WebView webv = new WebView(getActivity().getApplicationContext());
        webv.getSettings().setJavaScriptEnabled(true);
        String url=homeads.get(i);
        webv.loadUrl(url);
        flipper.addView(webv);
    }
    flipperStart();