How to load data in webview in android

watch_later Wednesday, August 17, 2016
comment 1 Comment
We can load Html data or Normal data or any kind of url or display file from sdcard in webview in android.Webview turns our appliction into web application .It is very easy to impliment

Loading Url in webview 

see this example

https://techcampuz.blogspot.in/2016/08/how-to-load-url-using-webview-in-android.html

Loading Html data or Normal data in webview

Create a project
activity_main.xml
<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">
    <Button
    android:id="@+id/btnopen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:padding="10dp"
    android:textSize="25sp"
    android:text="Load Data"/>
<WebView
    android:id="@+id/webView"
    android:layout_below="@+id/btnopen"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.webviewexample"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class MainActivity extends Activity {
private WebView webView;
Button btnopen;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
btnopen=(Button)findViewById(R.id.btnopen);
WebSettings webSettings = webView.getSettings();
  webSettings.setBuiltInZoomControls(true);
  webView.getSettings().setDisplayZoomControls(false);
  webView.setWebViewClient(new WebViewClient());
  webView. getSettings().setSupportZoom(true);
  webView.getSettings().setAllowFileAccess(true);
  webView.getSettings().setJavaScriptEnabled(true);
 webView.getSettings().setDomStorageEnabled(true);
 btnopen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String datadisplay = "<html><body><center>Hi,This example explain                                                                                     about<br></br><b> how to Load html data in                                                                                         webview<b/><br></br></center></body></html>";
        String tabledisplay = "<center><table border=1>" +
                             "<tr>" +
                             "<td><b>Ram</b></td>" +
                             "<td><b>Ramudu</b></td>" +
                             "</tr>" +
                             "<tr>" +
                             "<td><b>samba</b></td>" +
                             "<td><b>Ravi</b></td>" +
                             "</tr>" +
                             "</table></center>";
               webView.loadData(datadisplay+"\n"+tabledisplay, "text/html; charset=utf-8",null);
}
});
   }
}

Finally run your application

OUTPUT:
After running application click on Load Data button then it will load the data in webview 



avatar

Casino & Racetrack - Columbus, OH - GoyangFC
This hotel is within 2 mi 더킹바카라 (3 트리플 슬롯 km) of the 승인전화없는 사이트 main convention center on 토토랜드같은사이트 Hwy. 1. The resort is located 1.7 mi (5 km) from the wall street bets Columbus Circle Casino

delete February 26, 2022 at 3:42 AM



sentiment_satisfied Emoticon