WebView Example

WebView Example in Android

MainActivity CLASS

package com.example.malik.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

Button button;
WebView webView;
EditText editText;

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

setFunctions();

}

private void setFunctions() {
button = (Button) findViewById(R.id.bURL);
editText = (EditText) findViewById(R.id.etUrl);
webView = (WebView) findViewById(R.id.webView);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = editText.getText().toString();
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(url);
}
});
}
}

 

MainActivity XML

<?xml version=”1.0″ encoding=”utf-8″?>
<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”
android:orientation=”vertical”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.MainActivity”>
<Button
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Open URL”
android:id=”@+id/bURL”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true” />

<EditText
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@+id/etUrl”
android:layout_below=”@+id/bURL”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:text=”http://www.google.com” />

<WebView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@+id/webView”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”false”
android:layout_alignParentBottom=”true”
android:layout_below=”@+id/etUrl” />
</RelativeLayout>

 

Manifest FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.malik.test”>

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

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

 

OutPut :

 

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.