Monday, April 29, 2013

Monday, April 1, 2013

How to add Webview as a ListView item in android

Hello friends, :)

This is very easy process.
But when we add webviews as a row item in ListView that time Webview showing wrong url, Its a issue with custom ListView but for that we use holder to hold view.

But problem as it is with listview bcz at the time of loading webpag webview height increases as per webpages.

So for that issue. I write very efficient code. 
Its output look like WebView showing as listview row item.
I will explain as follows. Please check it.

Step 1.  main_relLay.xml
For adding scroll view and linear layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="fill_parent"
        android:layout_height="450dp" >

        <LinearLayout
            android:id="@+id/main_relLay"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#0B7A3B"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

Description: ScrollView use for listview effect. Linear Layout have a dynamic content (WebView) of rows.

Step 2: row.xml
For ListView row item

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

    <RelativeLayout
        android:id="@+id/row_relLay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#F70925" >

        <ProgressBar
            android:id="@+id/progressbar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="visible" />

        <WebView
            android:id="@+id/row_webView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:visibility="gone" >
        </WebView>
    </RelativeLayout>

</LinearLayout>

Description: Create another xml for listview row items.

Java file :
Using LayoutInflater load view of row.xml for listview row.
And add this row.xml in main.xml linear layout its avaialable in scroll view, This is main linear layout provide listview effect.

Step A: LinearLayout relLay = (LinearLayout) findViewById(R.id.main_relLay);
Step B: LayoutInflater inflater = (LayoutInflater) getApplication()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Step C: View v_child = inflater.inflate(R.layout.row, null);
Step D:  relLay.addView(v_child);



For more information or help regarding that,
Contact:
Mail ID : ranjitvcsc@gmail.com