Tuesday, 6 March 2018

Webview in android

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application.
 First Add Permissions in Android : -
  <uses-permission android:name="android.permission.INTERNET" />  
JavaFile 
MainActivity.java: -  
package safal.bhatia.webview;
 import android.content.Intent;
 import android.net.Uri;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
import android.webkit.WebView;
 import android.widget.TextView;  
public class MainActivity extends AppCompatActivity
{
WebView web_view;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);  
web_view = (WebView) findViewById(R.id.web_view); web_view.loadUrl("http://www.javatpoint.info"); web_view.setHorizontalScrollBarEnabled(false); //When we use java Script web_view.getSettings().setJavaScriptEnabled(true);
}
 }
 Xml File activity_main.xml: -
  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
 tools:context="safal.bhatia.webview.MainActivity">
 <WebView android:layout_width="match_parent"
 android:id="@+id/web_view"
 android:layout_height="match_parent"/>
</LinearLayout>