webview code for android studio

 main Activity.java 

Add the following line to your manifest file 2//

<uses-permission android:name="android.permission.INTERNET" /> 

import android.support.v7.app.AppCompatActivity; 

import android.os.Bundle; 

import android.view.View; 

import android.webkit.WebSettings; 

import android.webkit.WebView; 

import android.webkit.WebViewClient; 

import android.widget.Button; 

import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 

    EditText editText; 

  Button goBTN;   WebView webView; 

 @Override    

protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main); 

     editText = (EditText) findViewById(R.id.editText);        

 goBTN = (Button) findViewById(R.id.button);         

webView = (WebView) findViewById(R.id.webView);         webView.loadUrl("https://www.google.com");         

webView.setWebViewClient(new WebViewClient());         

WebSettings webSettings = webView.getSettings();        

webSettings.setJavaScriptEnabled(true);         

goBTN.setOnClickListener(new View.OnClickListener() {           

 @Override             

public void onClick(View v) {                 

String url = editText.getText().toString();                 

webView.loadUrl("https://"+url);        

   }   

     }); 

    } }

main.xml

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout    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:layout_height="match_parent" 

    android:id="@+id/relLayout"   

  android:padding="16dp"     

android:background="@color/white"     >     

<EditText       android:id="@+id/editText"        

 android:layout_alignParentTop="true"         

android:layout_width="match_parent"        

 android:layout_height="wrap_content"         

android:textColor="@color/black"        

 android:hint="Enter URL"/>     

<Button      android:id="@+id/button"        

android:layout_centerHorizontal="true"        

 android:layout_width="wrap_content"       

 android:layout_height="wrap_content"       

 android:text="GO"         

android:layout_below="@id/editText"/>     

<WebView        

 android:id="@+id/webView"        

 android:layout_width="match_parent"        

android:layout_height="match_parent"         

android:layout_marginTop="10dp"        

android:layout_below="@id/button">

</WebView> 

</RelativeLayout>


Post a Comment

Previous Post Next Post

Contact Form