dhiku
dhiku

Reputation: 1858

How to implement webview in a listview in Android?

I am trying to implement a webview inside a listview. But not able to figure it out.

Sorry for the basic question. This is what my code looks like so far.

public class MainActivity extends Activity {

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

    ListView lv = (ListView)findViewById(R.id.listView2);
    ListAdapter adapter = new ListAdapter(this);

    lv.setAdapter(adapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

and this is my xml part:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">

<ListView
    android:layout_height="fill_parent"
    android:id="@+id/listView2"
    android:layout_width="fill_parent"/>
</LinearLayout>

Upvotes: 1

Views: 2727

Answers (2)

Lassi Kinnunen
Lassi Kinnunen

Reputation: 416

put the webview inside a linearlayout or similar, that when initialized has a fixed size. then if you want you can later figure out the size of the content and resize the webview.

as the state in 2016. with the chrome webview component they can work just fine and several apps do just that.

you just need to take care of them properly. set the state and content and all that, size them up properly.

"just don't do that" is not a good answer when it is possible and preferable. the webview component with it's "drawbacks" of multithreaded rendering etc offers several advantages to just android styling views, like css and possibly loading it from server allowing style alterations without redeployment.

also as a pro tip, don't load from network, just dump the data into the webview directly from a memory cache. you can also preload couple of webviews up/down in the list - or cache as bitmaps after rendering if it's just for display.

if you can keep javascript disabled in the webview then all the better.

Upvotes: 1

Snicolas
Snicolas

Reputation: 38168

As explained by @Kevin, Webviews are probably the most heavy weight component in all the Android platform and this is completly the opposite of what people usually expect from a listview row component : being light weight and fast, the very secret of smooth list display.

Try an alternative design. Usually, I would really discourage people from making hybrid apps, but I don't want to enter in a religious war. But if you really want a webview, why don't you put your list inside the html page you wanna display. Already having one instance of webview behaving in the right way, and perform well, on all Android devices and versions can be challenging. That's gonna be much easier than the other way around.

Upvotes: 3

Related Questions