Michel Sarrailh
Michel Sarrailh

Reputation: 9

Android : webview not displayed on the defined activity

I want to display a webview and a listview on the same activity. If i display a short html code in the webview, it's ok, half the window being dedicated to the webview. But if i load an url in the webview, the part of the screen dédicated to the web view is empty, the content of the webview being displayed on a new activity. Can you help me ?

The .xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/wikipediaCompo"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

    <ListView
        android:id="@+id/list_oeuvres"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

The java code :

public class Display2 extends Activity {

    public ListView mListOeuvres;
    public WebView wikipediaCompo;

    public String[] infoOeuvres = new String[6];
    public int i=0;
    public String debug = "debug";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_oeuvres);

        mListOeuvres = (ListView) findViewById(R.id.list_oeuvres);
        try {
            InputStream is = getAssets().open("oeuvres.txt");
            Reader reader = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(reader);

            String line;
            while ((line = br.readLine()) != null) {
                //Log.e("code",line);
                String[] RowData = line.split("\\*");
                String nom = RowData[0];
                String refYouTube = RowData[1];
                String date = RowData[2];
                String duree = RowData[3];
                String interpretes = RowData[4];
                infoOeuvres[i] = nom +", "+date+" "+duree+" ("+interpretes+")";
                i++;
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        i=i-1;

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(Display2.this,
                android.R.layout.simple_list_item_1, infoOeuvres);

        String url="https://en.wikipedia.org/wiki/Ferdinando_Bertoni";

        wikipediaCompo = (WebView) findViewById(R.id.wikipediaCompo);

        // Configure la webview pour l'utilisation du javascript
        WebSettings webSettings = wikipediaCompo.getSettings();
        //WebSettings.setJavaScriptEnabled(true);

        // Permet l'ouverture des fenêtres
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        // Autorise le stockage DOM (Document Object Model)
        webSettings.setDomStorageEnabled(true);

        // Load the url
        wikipediaCompo.loadUrl(url);
        // Test with html code
        /*String temp = "<html><body>" + "<p align=\"justify\">"
                + "TEST" + "</p> "
                + "</body></html>";
        wikipediaCompo.loadData(temp, "text/html", "utf-8");*/

        mListOeuvres.setAdapter(adapter);
        mListOeuvres.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String pos = String.valueOf(parent.getItemAtPosition(position));

                final GlobalClass globalVariable = (GlobalClass) getApplicationContext();

                //Set name and email in global/application context
                globalVariable.setNoOeuvre(position);

                Intent i = new Intent(Display2.this, Display3.class);
                startActivity(i);
            }
        });
    }
}

Upvotes: 0

Views: 78

Answers (1)

MKovac
MKovac

Reputation: 174

Add this code after wikipediaCompo.loadUrl(url);

wikipediaCompo.setWebViewClient(new WebViewClient()
{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
           return false;
        }
 });

Upvotes: 1

Related Questions