Search code examples
androidviewflipper

Animation with view flipper to browse web views


I would like do an animation to browse web views, for this, I use viewflipper, when I made the animation with layouts it worked, but with webview, it does not work, I need help from experienced people, here is my code (I copy just the part where there is the problem) :

This is the code with layout, it works well:

   vf = (ViewFlipper) findViewById(R.id.details);
   LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   int layouts[] = new int[] {R.layout.layout_page1, R.layout.layout_page2, R.layout.layout_page3};

    for (int i=0; i<layouts.length; i++) {
        View vw = vi.inflate(layouts[i], null);     
        vf.addView(vw);
    }

and here is the code with WebView does not work:

    vf = (ViewFlipper) findViewById(R.id.details);
    WebView MyWebView[] =new WebView[2];

    MyWebView[0] = (WebView) findViewById(R.id.mproof);
    MyWebView[0].setWebViewClient(new WebViewClient());
    MyWebView[0].getSettings().setJavaScriptEnabled(true);
    MyWebView[0].loadUrl("http://www.google.com");

    MyWebView[1] = (WebView) findViewById(R.id.example);
    MyWebView[1].setWebViewClient(new WebViewClient());
    MyWebView[1].getSettings().setJavaScriptEnabled(true);
    WebView[1].loadUrl("http://www.hotmail.com");  

    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //int layouts[] = new int[] {R.layout.layout_page1, R.layout.layout_page2, R.layout.layout_page3};

    for (int i=0; i<2; i++) {
        vf.addView(MyWebView[i]);
    }

The debugger stop just after "vf.addView(MyWebView[i]);"


Solution

  • Adding two webviews from within the same layout xml a the view flipper would cause some problems.

    In the first example you gave you're inflating the views from separate layout files, in the second example you are using findViewById() to get the views, meaning they are in the same layout xml. The problem is most likely the fact that the 2 webviews already have a parent.

    Put the webview into its own layout and inflate it.

    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    MyWebView[0] = (WebView)vi.inflate(R.layout.webview,null);
    MyWebView[1] = (WebView)vi.inflate(R.layout.webview,null);
    
    for (int i=0; i<2; i++) {
        vf.addView(MyWebView[i]);
    }
    

    EDIT Even taking into account your XML file, the problem is the same thing, that the 2 webviews already have a parent. Which is the ViewFlipper, you are then trying to add them again to the ViewFlipper within the for loop.

    Removing this should fix your problem:

    for (int i=0; i<2; i++) {
        vf.addView(MyWebView[i]);
    }
    

    Or use the first solution with an separate xml file for the webview.