Create an application to open any URL inside the application and clicking on any link from
that URl should not open Native browser but that URL should open the same screen.



MainActivity.java


package com.example.assignment24;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    WebView browser;
    EditText link;
    Button btn_visit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        browser = (WebView) findViewById(R.id.webview);
        link = (EditText) findViewById(R.id.editTextLink);
        btn_visit = (Button) findViewById(R.id.btn_view);

        browser.setWebViewClient(new myWebViewClient());

        btn_visit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url =link.getText().toString();

                browser.getSettings().setLoadsImagesAutomatically(true);
                browser.getSettings().setJavaScriptEnabled(true);

                browser.loadUrl(url);
            }
        });
}

    private class myWebViewClient extends WebViewClient {
        @Override
        public  boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}




activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editTextLink"
        android:layout_width="match_parent"
        android:layout_height="86dp"
        android:layout_weight="9"
        android:layout_x="0dp"

        android:layout_y="0dp"
        android:hint="Enter URL" />


    <Button
        android:id="@+id/btn_view"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:layout_weight="1"
        android:layout_x="0dp"
        android:layout_y="78dp"
        android:background="@color/colorPrimary"
        android:text="Click"
        android:textColor="#ffffff"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"

        >


    </LinearLayout>

    <WebView
        android:id="@+id/webview"
        android:layout_width="405dp"
        android:layout_height="588dp"
        android:layout_x="0dp"
        android:layout_y="142dp">


    </WebView>


</AbsoluteLayout>




AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.assignment24">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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




After entering the URL




To download follow this link : Program 24

No comments:

Post a Comment